用Jena工具包实现数据源的融合
All My Eye 博客上最近有几篇帖子在写如何实现几个RDF数据源的联合查询、以及实现遗留的RDB数据库和RDF数据源的融合。前者基于Jena实现,后者基于Jena + SquirrelRDF实现。
用Jena实现几个RDF数据源联合查询的关键在于基于named graph的RDF复杂查询,以all my eye上面这个例子中的Articles和Authors本体以及Berners-Lee本人主页上的CARD.rdf为例,由于card.rdf中有几个URI是../..等的形式,Jena报了些小错,权且把这3个文件下载下来、作为文件读取了,代码如下:
按照上面的代码,得到一个XML文件:
当然也可以直接从URL从文件中读到RDF图中:
all my eye上另外一个例子利用了SquirrelRDF,它实现了从RDB到RDF格式的自动转换,从而可以将遗留的数据格式统一起来。而D2RQ则是另外一个类似的软件包,而且据说它除了能将RDF、RDB统一起来,还实现了多个不同格式数据源查询的优化。
用Jena实现几个RDF数据源联合查询的关键在于基于named graph的RDF复杂查询,以all my eye上面这个例子中的Articles和Authors本体以及Berners-Lee本人主页上的CARD.rdf为例,由于card.rdf中有几个URI是../..等的形式,Jena报了些小错,权且把这3个文件下载下来、作为文件读取了,代码如下:
import com.hp.hpl.jena.rdf.model.* ;
import com.hp.hpl.jena.query.*;
import java.util.* ;
import java.io.* ;
。。。。import com.hp.hpl.jena.query.*;
import java.util.* ;
import java.io.* ;
1 String queryString =
2 "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>" +
3 "PREFIX dc: <http://purl.org/dc/elements/1.1/>" +
4 "PREFIX foaf: <http://xmlns.com/foaf/0.1/>" +
5 "SELECT ?ms_articleid ?ms_title ?ms_authorid ?join_familyname ?ext_authorid ?ext_workplace ?ext_phone" +
6 "WHERE {" +
7 "?ms_articleid dc:identifier <http://metastore.ingenta.com/content/stc/00493155/v46n1/s9>." +
8 "?ms_articleid dc:title ?ms_title ." +
9 "?ms_articleid foaf:maker ?ms_authorid ." +
10 "GRAPH <http://card.rdf> {"+
11 "?ext_authorid foaf:family_name ?join_familyname ." +
12 "?ext_authorid foaf:workplaceHomepage ?ext_workplace ." +
13 "?ext_authorid foaf:phone ?ext_phone " +
14 "}"+
15 "GRAPH <http://authors.rdf> {" +
16 "?ms_authorid foaf:family_name ?join_familyname" +
17 "}" +
18 "FILTER (?ext_authorid != ?ms_authorid)" +
19 "}";
20
21 Query query = QueryFactory.create(queryString);
22
23 // data source
24 DataSource ds = DatasetFactory.create();
25
30 Model articlemodel = ModelFactory.createDefaultModel();
32 articlemodel.read(new FileInputStream(path + "articles.rdf"),null) ;
33
34
35 Model authormodel = ModelFactory.createDefaultModel();
36 authormodel.read(new FileInputStream(path + "authors.rdf"),null) ;
37
38
39 Model cardmodel = ModelFactory.createDefaultModel();
40 cardmodel.read(new FileInputStream(path + "card.rdf"),null) ;
41
42 ds.setDefaultModel(articlemodel);
43 ds.addNamedModel("http://card.rdf",cardmodel);
44 ds.addNamedModel("http://authors.rdf",authormodel);
45
46 QueryExecution qe = QueryExecutionFactory.create(query,ds);
47
48 ResultSet rs = qe.execSelect();
49 ResultSetFormatter.out(System.out, rs, query);
50
51 String ss = ResultSetFormatter.asXMLString(rs);
52 System.out.println(ss);
53
54 qe.close();
2 "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>" +
3 "PREFIX dc: <http://purl.org/dc/elements/1.1/>" +
4 "PREFIX foaf: <http://xmlns.com/foaf/0.1/>" +
5 "SELECT ?ms_articleid ?ms_title ?ms_authorid ?join_familyname ?ext_authorid ?ext_workplace ?ext_phone" +
6 "WHERE {" +
7 "?ms_articleid dc:identifier <http://metastore.ingenta.com/content/stc/00493155/v46n1/s9>." +
8 "?ms_articleid dc:title ?ms_title ." +
9 "?ms_articleid foaf:maker ?ms_authorid ." +
10 "GRAPH <http://card.rdf> {"+
11 "?ext_authorid foaf:family_name ?join_familyname ." +
12 "?ext_authorid foaf:workplaceHomepage ?ext_workplace ." +
13 "?ext_authorid foaf:phone ?ext_phone " +
14 "}"+
15 "GRAPH <http://authors.rdf> {" +
16 "?ms_authorid foaf:family_name ?join_familyname" +
17 "}" +
18 "FILTER (?ext_authorid != ?ms_authorid)" +
19 "}";
20
21 Query query = QueryFactory.create(queryString);
22
23 // data source
24 DataSource ds = DatasetFactory.create();
25
30 Model articlemodel = ModelFactory.createDefaultModel();
32 articlemodel.read(new FileInputStream(path + "articles.rdf"),null) ;
33
34
35 Model authormodel = ModelFactory.createDefaultModel();
36 authormodel.read(new FileInputStream(path + "authors.rdf"),null) ;
37
38
39 Model cardmodel = ModelFactory.createDefaultModel();
40 cardmodel.read(new FileInputStream(path + "card.rdf"),null) ;
41
42 ds.setDefaultModel(articlemodel);
43 ds.addNamedModel("http://card.rdf",cardmodel);
44 ds.addNamedModel("http://authors.rdf",authormodel);
45
46 QueryExecution qe = QueryExecutionFactory.create(query,ds);
47
48 ResultSet rs = qe.execSelect();
49 ResultSetFormatter.out(System.out, rs, query);
50
51 String ss = ResultSetFormatter.asXMLString(rs);
52 System.out.println(ss);
53
54 qe.close();
按照上面的代码,得到一个XML文件:
返回结果
当然也可以直接从URL从文件中读到RDF图中:
URL url = new URL("http://xx/yy.rdf");
InputStream stream = url.openStream();
xxxxmodel.read(stream,null);
InputStream stream = url.openStream();
xxxxmodel.read(stream,null);
all my eye上另外一个例子利用了SquirrelRDF,它实现了从RDB到RDF格式的自动转换,从而可以将遗留的数据格式统一起来。而D2RQ则是另外一个类似的软件包,而且据说它除了能将RDF、RDB统一起来,还实现了多个不同格式数据源查询的优化。
posted on 2006-09-28 11:11 Jacquette.wang 阅读(1097) 评论(0) 编辑 收藏 举报