rdf mysql持久化l_Jena 利用数据库保存,持久化本体
1 Jena的數(shù)據(jù)庫接口
Jena提供了將RDF數(shù)據(jù)存入關(guān)系數(shù)據(jù)庫的接口,Model、Resource、Query等接口可以用于訪問和維護(hù)數(shù)據(jù)庫里的RDF數(shù)據(jù)。在處理數(shù)據(jù)時(shí),應(yīng)用程序不必直接操作數(shù)據(jù)(而
是通過Jena的API),也不必知道數(shù)據(jù)庫的模式。Jena提供了支持MySQL、HSQLDB、PostgreSQ、Oracle和Microsoft SQL Server的程序接口。有些第三方提供其他數(shù)據(jù)庫接
口的支持。可以參考Jena數(shù)據(jù)庫文檔獲得數(shù)據(jù)庫版本以及對應(yīng)的JDBC驅(qū)動(dòng)說明。
2 Jena的數(shù)據(jù)庫模式
關(guān)系數(shù)據(jù)庫存儲(chǔ)RDF數(shù)據(jù)的一般模式是“三元組”,表有三列(主體、謂詞、客體)每個(gè)RDF陳述(sataement)占用一行。有時(shí)候,添加第四列以表示客體是字符常量還是
URI。Jena 2采用一種denormalized的三元組存儲(chǔ)方法,是存儲(chǔ)空間和訪問時(shí)間的一種權(quán)衡方法(a space-time trade-off)。Jena使用兩類七個(gè)表存儲(chǔ)本體,第一類是
asserted statements,第二類reified statements。
Statement Tables 陳述表:
1) ? ? ? ? Asserted Statement Table (Jena_GiTj_Stmt):存儲(chǔ)本體數(shù)據(jù)
2) ? ? ? ? Reified Statement Table (Jena_GiTj_Reif):經(jīng)過處理的本體數(shù)據(jù)。System Tables 系統(tǒng)表:存儲(chǔ)元數(shù)據(jù)和陳述表中使用的較長的文字或者資源
3) ? ? ? ? System Statement Table (Jena_Sys_Stmt):存儲(chǔ)系統(tǒng)元數(shù)據(jù)
4) ? ? ? ? Long Literals Table (Jena_Long_Lit):存儲(chǔ)陳述表中不便于直接存儲(chǔ)的長字符創(chuàng)常量(Literals)
5) ? ? ? ? Long Resources Table (Jena_Long_URI):存儲(chǔ)陳述表中不便于直接存儲(chǔ)的長資源URI
6) ? ? ? ? Prefixes Table (Jena_Prefix):存儲(chǔ)URI的前綴。前綴只存儲(chǔ)一次,節(jié)省空間。
7) ? ? ? ? Graph Table (Jena_Graph):存儲(chǔ)每一個(gè)用戶圖的名字和唯一標(biāo)志符。
8) ? ? ? ? Lock Table (Jena_Mutex):一個(gè)沒有內(nèi)容的表。如果該表存在,在一定時(shí)間段里數(shù)據(jù)庫被鎖定。
可以參照\\Jena-2.6.4\doc\DB\layout.html獲取各個(gè)表的詳細(xì)信息。
3 創(chuàng)建本體的持久模型
Jena同時(shí)支持內(nèi)存模型和數(shù)據(jù)庫模型。一般來講,創(chuàng)建內(nèi)存模型只需要調(diào)用Jena的一些接口,但創(chuàng)建數(shù)據(jù)庫模型,或者打開先前創(chuàng)建的模型,要求一些具體的步驟。
任何數(shù)據(jù)庫的持久模型通過以下步驟創(chuàng)建:
1) ? ? ? ? 加載數(shù)據(jù)庫JDBC驅(qū)動(dòng)
2) ? ? ? ? 創(chuàng)建數(shù)據(jù)庫連接
3) ? ? ? ? 為數(shù)據(jù)庫創(chuàng)建一個(gè)ModelMaker
4) ? ? ? ? 為本體創(chuàng)建一個(gè)模型
4 將本體持久化存入MySQL中
1) 其中數(shù)據(jù)庫的配置文件為:
jdbc.drivers=com.mysql.jdbc.Driver
jdbc.url=jdbc\:mysql\://localhost\:3306/ontologies?useUnicode\=true&characterEncoding\=UTF-8
jdbc.username=root
jdbc.password=root
2) 實(shí)例類
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.IOException;
importjava.io.InputStreamReader;
importjava.io.UnsupportedEncodingException;
importjava.sql.SQLException;
importcom.hp.hpl.jena.db.DBConnection;
importcom.hp.hpl.jena.db.IDBConnection;
importcom.hp.hpl.jena.db.RDFRDBException;
importcom.hp.hpl.jena.ontology.OntModel;
importcom.hp.hpl.jena.ontology.OntModelSpec;
importcom.hp.hpl.jena.rdf.model.Model;
importcom.hp.hpl.jena.rdf.model.ModelFactory;
importcom.hp.hpl.jena.rdf.model.ModelMaker;
importedu.hrbeu.ontology.util.getDBPropeties;
/**
*?@purpose?本體數(shù)據(jù)庫功能
*?@author?zhaohongjie
*
*/
publicclassOntologyDBImplimplementsIOntologyDB?{
/**
*?數(shù)據(jù)庫連接對象
*/
privateIDBConnection?conn?=null;
/**
*?文件輸入流對象
*/
privateInputStreamReader?in?=null;
/**
*?獲取數(shù)據(jù)連接
*?@return
*/
privateIDBConnection?getDBConn()?{
getDBPropeties?getdb?=?newgetDBPropeties();
try{
this.conn?=newDBConnection(getdb.getUrl(),?getdb.getUser(),?getdb.getPassword(),"MySQL");
Class.forName(getdb.getClassDrive());
}?catch(RDFRDBException?e)?{
System.out.println("Exceptions?occur...");
}?catch(ClassNotFoundException?e)?{
System.out.println("ClassNotFoundException,?Driver?is?not?available...");
}
returnthis.conn;
}
/**
*?從數(shù)據(jù)流獲取本體
*?@param?filePath
*/
publicInputStreamReader?getFileStream(String?filePath)?{
FileInputStream?inputSreamfile?=?null;
try{
File?file?=?newFile(filePath);//"./Expert.owl"
inputSreamfile?=?newFileInputStream(file);
}?catch(FileNotFoundException?e)?{
e.printStackTrace();
System.out.println("Ontology?File?is?not?available...");
}
try{
this.in?=newInputStreamReader(inputSreamfile,"UTF-8");
}?catch(UnsupportedEncodingException?e)?{
e.printStackTrace();
}
returnthis.in;
}
/**
*?將本體存入數(shù)據(jù)庫
*?@param?ontoName
*/
publicvoidtoMySQL(String?ontoName)?{
ModelMaker?maker?=?ModelFactory.createModelRDBMaker(getDBConn());
Model?defModel?=?maker.createModel(ontoName);?//"expert"
defModel.read(in,null);
defModel.commit();
closeDBResource();
}
/**
*?OntModelSpec
*?@param?maker
*?@return
*/
privateOntModelSpec?getModelSpec(ModelMaker?maker)?{
OntModelSpec?spec?=?newOntModelSpec(OntModelSpec.OWL_MEM);
spec.setImportModelMaker(maker);
returnspec;
}
/**
*?返回本體
*?@param?ontoName
*?@return
*/
privateOntModel?getModelFromDB(String?ontoName)?{
ModelMaker?maker?=?ModelFactory.createModelRDBMaker(getDBConn());
Model?base?=?maker.getModel(ontoName);
OntModel?newmodel?=?ModelFactory.createOntologyModel(getModelSpec(maker),?base);
returnnewmodel;
}
/**
*?獲取本體對象
*?@param?ontoName
*/
publicOntModel?fromMySQL(String?ontoName)?{
OntModel?model?=?getModelFromDB(ontoName);
returnmodel;
}
/**
*?關(guān)閉占用資源
*/
privatevoidcloseDBResource()?{
closeFileStream();
closeDBConn();
}
/**
*?關(guān)閉數(shù)據(jù)流
*/
privatevoidcloseFileStream()?{
try{
this.in.close();
}?catch(IOException?e)?{
e.printStackTrace();
}
}
/**
*?關(guān)閉數(shù)據(jù)庫連接
*/
publicvoidcloseDBConn()?{
try{
if(this.conn?!=null)?{
this.conn.close();
}
}?catch(SQLException?sqlEx)?{
sqlEx.printStackTrace();
}
}
}
3) mian函數(shù)
publicstaticvoidmain(String[]?args)?{
String?ruleFile?=?"file:./expert/Expert.rules";
IOntologyDB?ontoDB?=?OntologyDBFactory.createDBont();
ontoDB.getFileStream(ruleFile);
ontoDB.toMySQL("expert");
ontoDB.closeDBConn();
}
總結(jié)
以上是生活随笔為你收集整理的rdf mysql持久化l_Jena 利用数据库保存,持久化本体的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux 多线程开发-等待线程结束pt
- 下一篇: emqx使用webhook数据持久化到m