前面有講到通過JenaAPI讀取RDF文件(或者OWL文件)存儲到mysql數據庫中,這是會默認生成7張表來存儲。具體流程如下:①加載數據庫JDBC驅動。②創建數據庫連接。③從Mysql數據庫讀取OntModel。④操作返回的OntModel,打印三元組查看具體內容。代碼如下:
package d2rq;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.hp.hpl.jena.db.DBConnection;
import com.hp.hpl.jena.db.IDBConnection;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.ModelMaker;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;
public class Mysql2Console {
private static Logger log = LogManager.getLogger(Mysql2Console.class.getName());
public static final String strDriver = "com.mysql.jdbc.Driver"; // path of
public static final String strURL = "jdbc:mysql://localhost:3306/academic?useUnicode=true&characterEncoding=utf8"; // URL
public static final String strUser = "root"; // database user id
public static final String strPassWord = "123456"; // database password
public static final String strDB = "MySQL"; // database type
public static final String PATH = "doc/rdf/academic.rdf"; // file path
public static final String OWL = "doc/owl/unit.owl"; // file path
public static final String MODLE_NAME="MyOntology";
/**
* @param args
* @throws ClassNotFoundException
*/
public static void main(String[] args) throws ClassNotFoundException {
// 加載數據庫驅動類,需要處理異常
Class.forName(strDriver);
// 創建一個數據庫連接
IDBConnection conn = getConnection(strURL, strUser, strPassWord,strDB);
if(conn!=null){
log.debug("-------mysql數據庫連接成功~");
}
//持久化到數據庫
//createModel(conn,MODLE_NAME,PATH);
OntModel model = getModelFromRDB(conn, MODLE_NAME);
log.debug(model+" :"+model.isEmpty());
getTriples(model);
}
//獲取數據連接
public static DBConnection getConnection(String dbUrl, String dbUser,
String dbPwd, String dbName) {
return new DBConnection(dbUrl, dbUser, dbPwd, dbName);
}
//從數據讀取OntModel
public static OntModel getModelFromRDB(IDBConnection con,String name){
ModelMaker maker = ModelFactory.createModelRDBMaker(con);
Model model = maker.getModel(name);//讀取我們之前創建的MyOntology模型
OntModelSpec spec = new OntModelSpec(OntModelSpec.RDFS_MEM);//這裏是指定的RDF格式
OntModel ontModel = ModelFactory.createOntologyModel(spec,model);
return ontModel;
}
//
public static void getTriples(OntModel model){
StmtIterator stmIter=model.listStatements();
while(stmIter.hasNext()){
Statement stmt = stmIter.nextStatement();
RDFNode object= stmt.getObject();
if(object instanceof Resource){//主要爲了判定RDFNode是Resource還是Literal
log.debug(stmt.getSubject().getLocalName()+"--"
+stmt.getPredicate().getLocalName()+"--"
+object.asResource().getLocalName());
}else{
log.debug(stmt.getSubject().getLocalName()+"**"
+stmt.getPredicate().getLocalName()+"**"
+object.toString());
}
}
}
}
查詢結果如下:返回的模型不爲空,內容是三元組label-type-property.跟程序打印結果相同。個人感覺細節還待研究,有問題大家可以提出來,一起學習,謝謝。
2016-03-23 15:00:56.069 DEBUG [main][Mysql2Console.java:40] - -------mysql數據庫連接成功~
2016-03-23 15:00:57.250 DEBUG [main][Mysql2Console.java:45] - <ModelCom {rdfs:label @rdf:type rdf:Property} | > :false
2016-03-23 15:00:57.253 DEBUG [main][Mysql2Console.java:72] - label--type--Property
於此同時我也讀取用Protege製作的people.owl文本到Mysql數據,然後讀取出來,這裏可以看到具體內容。
現在我們統計下表數據:
SELECT count(1) FROM jena_g1t1_stmt ; --1條記錄
SELECT count(1) FROM jena_sys_stmt ; --51條記錄
具體需要更改的代碼如下:
public static final String PATH = "doc/owl/people.owl"; // file path
OntModelSpec spec = new OntModelSpec(OntModelSpec.OWL_MEM);//這裏是指定的OWL格式
執行完之後在做查詢:說明本體數據增加705條,元數據增加6條。
SELECT count(1) FROM jena_g1t1_stmt ; --706條記錄
SELECT count(1) FROM jena_sys_stmt ; --57條記錄
現在我們用代碼繼續查詢Mysql數據讀取出來的OntModel模型和具體三元組信息代碼同上:
OntModel model = getModelFromRDB(conn, MODLE_NAME);
log.debug(model+" :"+model.isEmpty());
getTriples(model);
打印結果如下:因爲數據比較多這裏值複製部分日誌過來供參考。
2016-03-23 15:21:23.946 DEBUG [main][RDF2Mysql.java:67] - -------mysql數據庫連接成功~
2016-03-23 15:21:25.344 DEBUG [main][RDF2Mysql.java:72] - <ModelCom {rdfs:label @rdf:type rdf:Property; http://owl.man.ac.uk/2006/07/sssw/people @rdf:type owl:Ontology; :drives @rdf:type owl:ObjectProperty; :drives @rdfs:label "drives"^^http://www.w3.org/2001/XMLSchema#string; :drives @rdfs:comment ""^^http://www.w3.org/2001/XMLSchema#string; :eaten_by @rdf:type owl:ObjectProperty; :eaten_by @rdfs:label "eaten_by"^^http://www.w3.org/2001/XMLSchema#string; :eaten_by @rdfs:comment ""^^http://www.w3.org/2001/XMLSchema#string; :eats @rdf:type owl:ObjectProperty; :eats @rdfs:label "eats"^^http://www.w3.org/2001/XMLSchema#string; :eats @rdfs:comment ""^^http://www.w3.org/2001/XMLSchema#string; :eats @rdfs:domain :animal; :eats @owl:inverseOf :eaten_by; :has_child @rdf:type owl:ObjectProperty; :has_child @rdfs:label "has_child"^^http://www.w3.org/2001/XMLSchema#string; :has_child @rdfs:comment ""^^http://www.w3.org/2001/XMLSchema#string; :has_father @rdf:type owl:ObjectProperty...} | > :false
2016-03-23 15:21:25.353 DEBUG [main][RDF2Mysql.java:168] - label--type--Property
2016-03-23 15:21:25.354 DEBUG [main][RDF2Mysql.java:168] - people--type--Ontology
2016-03-23 15:21:25.355 DEBUG [main][RDF2Mysql.java:168] - drives--type--ObjectProperty
2016-03-23 15:21:25.355 DEBUG [main][RDF2Mysql.java:173] - drives**label**drives^^http://www.w3.org/2001/XMLSchema#string
2016-03-23 15:21:25.356 DEBUG [main][RDF2Mysql.java:173] - drives**comment**^^http://www.w3.org/2001/XMLSchema#string
2016-03-23 15:21:25.356 DEBUG [main][RDF2Mysql.java:168] - eaten_by--type--ObjectProperty
2016-03-23 15:21:25.357 DEBUG [main][RDF2Mysql.java:173] - eaten_by**label**eaten_by^^http://www.w3.org/2001/XMLSchema#string
2016-03-23 15:21:25.357 DEBUG [main][RDF2Mysql.java:173] - eaten_by**comment**^^http://www.w3.org/2001/XMLSchema#string
2016-03-23 15:21:25.358 DEBUG [main][RDF2Mysql.java:168] - eats--type--ObjectProperty
2016-03-23 15:21:25.358 DEBUG [main][RDF2Mysql.java:173] - eats**label**eats^^http://www.w3.org/2001/XMLSchema#string
2016-03-23 15:21:25.359 DEBUG [main][RDF2Mysql.java:173] - eats**comment**^^http://www.w3.org/2001/XMLSchema#string
2016-03-23 15:21:25.359 DEBUG [main][RDF2Mysql.java:168] - eats--domain--animal
2016-03-23 15:21:25.360 DEBUG [main][RDF2Mysql.java:168] - eats--inverseOf--eaten_by
2016-03-23 15:21:25.360 DEBUG [main][RDF2Mysql.java:168] - has_child--type--ObjectProperty
2016-03-23 15:21:25.360 DEBUG [main][RDF2Mysql.java:173] - has_child**label**has_child^^http://www.w3.org/2001/XMLSchema#string
2016-03-23 15:21:25.361 DEBUG [main][RDF2Mysql.java:173] - has_child**comment**^^http://www.w3.org/2001/XMLSchema#string
2016-03-23 15:21:25.361 DEBUG [main][RDF2Mysql.java:168] - has_father--type--ObjectProperty
2016-03-23 15:21:25.362 DEBUG [main][RDF2Mysql.java:173] - has_father**label**has_father^^http://www.w3.org/2001/XMLSchema#string
如果需要讀取Ontology所有的類和屬性執行下面方法。
//讀取Ontology的本體所有的類和屬性
public static void getClasses(OntModel model) {
for (ExtendedIterator<OntClass> ei = model.listClasses(); ei.hasNext();) {
OntClass oc = ei.next();
System.out.println(oc.getLocalName());
for (ExtendedIterator<OntProperty> eip = oc
.listDeclaredProperties(); eip.hasNext();) {
OntProperty op = eip.next();
log.debug(op.getLocalName());
}
}
}
日誌如下:
2016-03-23 15:24:24.495 DEBUG [main][RDF2Mysql.java:157] - has_parent
2016-03-23 15:24:24.497 DEBUG [main][RDF2Mysql.java:157] - has_mother
2016-03-23 15:24:24.499 DEBUG [main][RDF2Mysql.java:157] - is_pet_of
2016-03-23 15:24:24.502 DEBUG [main][RDF2Mysql.java:157] - part_of
null
2016-03-23 15:24:24.519 DEBUG [main][RDF2Mysql.java:157] - eaten_by
2016-03-23 15:24:24.520 DEBUG [main][RDF2Mysql.java:157] - works_for
如果只想要輸出類信息執行下面代碼:這樣就不是把謂語和賓語打印出來。遍歷所有的類即可。
public static void printModel(OntModel model){
for (Iterator<OntClass> i = model.listClasses(); i.hasNext();) {
OntClass oc = i.next();
log.debug(oc.getLocalName());
}
}
輸出信息如下:
2016-03-23 15:25:20.907 DEBUG [main][RDF2Mysql.java:67] - -------mysql數據庫連接成功~
2016-03-23 15:25:22.099 DEBUG [main][RDF2Mysql.java:146] - adult
2016-03-23 15:25:22.102 DEBUG [main][RDF2Mysql.java:146] - animal
2016-03-23 15:25:22.105 DEBUG [main][RDF2Mysql.java:146] - animal_lover
2016-03-23 15:25:22.108 DEBUG [main][RDF2Mysql.java:146] - null
2016-03-23 15:25:22.111 DEBUG [main][RDF2Mysql.java:146] - bicycle
今天就寫到這裏,如果對你有幫助請點個贊,如果有任何技術問題歡迎指正,謝謝~