注明:本文檔是使用Jena2.6.4,數據庫為MySQL,數據庫驅動版本為mysql-connector-java-5.1.13-bin.jar。
1 Jena的數據庫接口
Jena提供了將RDF數據存入關系數據庫的接口,Model、Resource、Query等接口可以用於訪問和維護數據庫里的RDF數據。在處理數據時,應用程序不必直接操作數據(而
是通過Jena的API),也不必知道數據庫的模式。Jena提供了支持MySQL、HSQLDB、PostgreSQ、Oracle和Microsoft SQL Server的程序接口。有些第三方提供其他數據庫接
口的支持。可以參考Jena數據庫文檔獲得數據庫版本以及對應的JDBC驅動說明。
2 Jena的數據庫模式
關系數據庫存儲RDF數據的一般模式是“三元組”,表有三列(主體、謂詞、客體)每個RDF陳述(sataement)占用一行。有時候,添加第四列以表示客體是字符常量還是
URI。Jena 2采用一種denormalized的三元組存儲方法,是存儲空間和訪問時間的一種權衡方法(a space-time trade-off)。Jena使用兩類七個表存儲本體,第一類是
asserted statements,第二類reified statements。
Statement Tables 陳述表:
1) Asserted Statement Table (Jena_GiTj_Stmt):存儲本體數據
2) Reified Statement Table (Jena_GiTj_Reif):經過處理的本體數據。System Tables 系統表:存儲元數據和陳述表中使用的較長的文字或者資源
3) System Statement Table (Jena_Sys_Stmt):存儲系統元數據
4) Long Literals Table (Jena_Long_Lit):存儲陳述表中不便於直接存儲的長字符創常量(Literals)
5) Long Resources Table (Jena_Long_URI):存儲陳述表中不便於直接存儲的長資源URI
6) Prefixes Table (Jena_Prefix):存儲URI的前綴。前綴只存儲一次,節省空間。
7) Graph Table (Jena_Graph):存儲每一個用戶圖的名字和唯一標志符。
8) Lock Table (Jena_Mutex):一個沒有內容的表。如果該表存在,在一定時間段里數據庫被鎖定。
可以參照\\Jena-2.6.4\doc\DB\layout.html獲取各個表的詳細信息。
3 創建本體的持久模型
Jena同時支持內存模型和數據庫模型。一般來講,創建內存模型只需要調用Jena的一些接口,但創建數據庫模型,或者打開先前創建的模型,要求一些具體的步驟。
任何數據庫的持久模型通過以下步驟創建:
1) 加載數據庫JDBC驅動
2) 創建數據庫連接
3) 為數據庫創建一個ModelMaker
4) 為本體創建一個模型
4 將本體持久化存入MySQL中
1) 其中數據庫的配置文件為:
1 jdbc.drivers=com.mysql.jdbc.Driver 2 jdbc.url=jdbc\:mysql\://localhost\:3306/ontologies?useUnicode\=true&characterEncoding\=UTF-8 3 jdbc.username=root 4 jdbc.password=root
2) 實例類
1 import java.io.File; 2 import java.io.FileInputStream; 3 import java.io.FileNotFoundException; 4 import java.io.IOException; 5 import java.io.InputStreamReader; 6 import java.io.UnsupportedEncodingException; 7 import java.sql.SQLException; 8 9 import com.hp.hpl.jena.db.DBConnection; 10 import com.hp.hpl.jena.db.IDBConnection; 11 import com.hp.hpl.jena.db.RDFRDBException; 12 import com.hp.hpl.jena.ontology.OntModel; 13 import com.hp.hpl.jena.ontology.OntModelSpec; 14 import com.hp.hpl.jena.rdf.model.Model; 15 import com.hp.hpl.jena.rdf.model.ModelFactory; 16 import com.hp.hpl.jena.rdf.model.ModelMaker; 17 18 import edu.hrbeu.ontology.util.getDBPropeties; 19 20 /** 21 * @purpose 本體數據庫功能 22 * @author zhaohongjie 23 * 24 */ 25 public class OntologyDBImpl implements IOntologyDB { 26 27 /** 28 * 數據庫連接對象 29 */ 30 private IDBConnection conn = null; 31 /** 32 * 文件輸入流對象 33 */ 34 private InputStreamReader in = null; 35 36 /** 37 * 獲取數據連接 38 * @return 39 */ 40 private IDBConnection getDBConn() { 41 42 getDBPropeties getdb = new getDBPropeties(); 43 44 try { 45 this.conn = new DBConnection(getdb.getUrl(), getdb.getUser(), getdb.getPassword(), "MySQL"); 46 Class.forName(getdb.getClassDrive()); 47 } catch (RDFRDBException e) { 48 System.out.println("Exceptions occur..."); 49 } catch (ClassNotFoundException e) { 50 System.out.println("ClassNotFoundException, Driver is not available..."); 51 } 52 53 return this.conn; 54 } 55 56 /** 57 * 從數據流獲取本體 58 * @param filePath 59 */ 60 public InputStreamReader getFileStream(String filePath) { 61 62 FileInputStream inputSreamfile = null; 63 64 try { 65 File file = new File(filePath); //"./Expert.owl" 66 inputSreamfile = new FileInputStream(file); 67 } catch (FileNotFoundException e) { 68 e.printStackTrace(); 69 System.out.println("Ontology File is not available..."); 70 } 71 72 try { 73 this.in = new InputStreamReader(inputSreamfile, "UTF-8"); 74 } catch (UnsupportedEncodingException e) { 75 e.printStackTrace(); 76 } 77 78 return this.in; 79 } 80 81 /** 82 * 將本體存入數據庫 83 * @param ontoName 84 */ 85 public void toMySQL(String ontoName) { 86 ModelMaker maker = ModelFactory.createModelRDBMaker(getDBConn()); 87 Model defModel = maker.createModel(ontoName); //"expert" 88 defModel.read(in,null); 89 defModel.commit(); 90 closeDBResource(); 91 } 92 93 /** 94 * OntModelSpec 95 * @param maker 96 * @return 97 */ 98 private OntModelSpec getModelSpec(ModelMaker maker) { 99 100 OntModelSpec spec = new OntModelSpec(OntModelSpec.OWL_MEM); 101 spec.setImportModelMaker(maker); 102 return spec; 103 104 } 105 106 /** 107 * 返回本體 108 * @param ontoName 109 * @return 110 */ 111 private OntModel getModelFromDB(String ontoName) { 112 113 ModelMaker maker = ModelFactory.createModelRDBMaker(getDBConn()); 114 Model base = maker.getModel(ontoName); 115 OntModel newmodel = ModelFactory.createOntologyModel(getModelSpec(maker), base); 116 return newmodel; 117 118 } 119 120 /** 121 * 獲取本體對象 122 * @param ontoName 123 */ 124 public OntModel fromMySQL(String ontoName) { 125 126 OntModel model = getModelFromDB(ontoName); 127 return model; 128 129 } 130 131 /** 132 * 關閉占用資源 133 */ 134 private void closeDBResource() { 135 closeFileStream(); 136 closeDBConn(); 137 } 138 139 /** 140 * 關閉數據流 141 */ 142 private void closeFileStream() { 143 try { 144 this.in.close(); 145 } catch (IOException e) { 146 e.printStackTrace(); 147 } 148 } 149 150 /** 151 * 關閉數據庫連接 152 */ 153 public void closeDBConn() { 154 try { 155 if (this.conn != null) { 156 this.conn.close(); 157 } 158 } catch (SQLException sqlEx) { 159 sqlEx.printStackTrace(); 160 } 161 } 162 163 }
3) mian函數
