前面有讲到通过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
今天就写到这里,如果对你有帮助请点个赞,如果有任何技术问题欢迎指正,谢谢~