SPARQL语言的使用总结
来源:互联网 发布:sql注入漏洞的原理 编辑:程序博客网 时间:2024/06/05 15:51
由于之前系统学过SPARQL的理论和实践,但都不够系统,为此重新进行总结一下:
1 工具环境采用:Sesame-workbench来直接测试SPARQL语句的正确性。
2 编码环境采用:jena来测试相关代码应用。
3 测试数据:如下内容,并保存为UTF-8的文件test.rdf文件
<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF
xmlns:c="http://s.opencalais.com/1/pred/"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<rdf:Description rdf:about="http://d.opencalais.com/dochash-1/788f2f97-5b54-3202-9db8-61d1834a3f77">
<c:资料类型>a327a4b0-8c6a-b911-3ef0-54c84c22723b</c:资料类型>
<c:externalID>http://www.deri.ie中国</c:externalID>
<c:id>http://id.opencalais.com/k5Mp3gJaH21KD339XKs8PQ</c:id>
<rdf:type rdf:resource="http://s.opencalais.com/1/type/sys/DocInfo"/>
<c:document></c:document>
<c:docTitle></c:docTitle>
<c:docDate>2013-05-29 07:48:08.424</c:docDate>
</rdf:Description>
<rdf:Description rdf:about="http://d.opencalais.com/dochash-1/788f2f97-5b54-3202-9db8-61d1834a3222">
<c:资料类型>a327a4b0-8c6a-b911-3ef0-54c84c227222</c:资料类型>
<c:externalID>http://www.deri.com</c:externalID>
<c:id>http://id.opencalais.com/k5Mp3gJaH21KD339XsssssKs8PQ</c:id>
<rdf:type rdf:resource="http://s.opencalais.com/1/type/sys/DocInfo"/>
<c:document></c:document>
<c:docTitle></c:docTitle>
<c:docDate>2013-05-29 07:48:18.424</c:docDate>
</rdf:Description>
<rdf:Description rdf:about="http://d.opencalais.com/dochash-1/788f2f97-5b54-3202-9db8-61d1834a3f77/meta">
<c:contentType>text/html</c:contentType>
<c:emVer>7.1.1103.5</c:emVer>
<c:langIdVer>DefaultLangId</c:langIdVer>
<c:language>InputTextTooShort</c:language>
<c:processingVer>CalaisJob01</c:processingVer>
<c:submissionDate>2013-05-29 07:48:08.268</c:submissionDate>
<rdf:type rdf:resource="http://s.opencalais.com/1/type/sys/DocInfoMeta"/>
<c:docId rdf:resource="http://d.opencalais.com/dochash-1/788f2f97-5b54-3202-9db8-61d1834a3f77"/>
<c:submitterCode>d98c1dd4-008f-04b2-e980-0998ecf8427e</c:submitterCode>
<c:signature>digestalg-1|AILQiyYhIcdMscM1psx1oyF9Kpg=|dkCpZQrN
+zzh7aOln8p9IRHA7p5hSnFbV2cGGM56f3fkDRod4cl9Ew==</c:signature>
</rdf:Description>
<rdf:Description rdf:about="http://d.opencalais.com/dochash-1/788f2f97-5b54-3202-9db8-61d1834a3f77/lid/DefaultLangId">
<rdf:type rdf:resource="http://s.opencalais.com/1/type/lid/DefaultLangId"/>
<c:docId rdf:resource="http://d.opencalais.com/dochash-1/788f2f97-5b54-3202-9db8-61d1834a3f77"/>
<c:lang rdf:resource="http://d.opencalais.com/lid/DefaultLangId/InputTextTooShort"/>
</rdf:Description>
</rdf:RDF>
4 测试语句:
(1) 查询相关连的内容组信息(基于同一id可,及x):
PREFIX c:<http://s.opencalais.com/1/pred/>
PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?externalID ?docDate ?id
WHERE
{
?x c:externalID ?externalID .
?x c:docDate ?docDate .
?x c:id ?id .}
查询出两条记录:
ExternalID DocDate Id
PREFIX c:<http://s.opencalais.com/1/pred/>
PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?externalID ?docDate ?id from
WHERE
{
?x c:externalID ?externalID .
?y c:docDate ?docDate .
?z c:id ?id .}
查询出组合记录:
ExternalID DocDate Id
PREFIX c:<http://s.opencalais.com/1/pred/>
PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT *
WHERE
{
?subject ?predicate ?object .}
结果为全部内容。
(4)查询object所在subject,即内容所在主题:
SELECT ?x
WHERE
{
?x c:processingVer ?processingVer}
结果为:
X
PREFIX c:<http://s.opencalais.com/1/pred/>
PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
construct {?x rdf:type ?externalID} where {?x c:externalID ?externalID}
执行结果:
Subject Predicate Object
PREFIX c:<http://s.opencalais.com/1/pred/>
PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?externalID ?docDate ?id from
WHERE
{
?x c:externalID ?externalID .
?x c:docDate ?docDate .
?x c:id ?id .
FILTER regex(?externalID, "com", "i") }
执行结果:
ExternalID DocDate Id
【未差异处理前】
PREFIX c:<http://s.opencalais.com/1/pred/>
PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?docId ?contentType
WHERE
{
?x c:docId ?docId .
?x c:contentType ?contentType .
}
执行结果:
DocId ContentType
PREFIX c:<http://s.opencalais.com/1/pred/>
PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?docId ?contentType
WHERE
{
?x c:docId ?docId .
OPTIONAL { ?x c:contentType ?contentType}
}
执行结果
DocId ContentType
DocId ContentType
PREFIX c:<http://s.opencalais.com/1/pred/>
PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?externalID ?docDate ?资料类型
WHERE
{
?x c:externalID ?externalID .
?x c:docDate ?docDate .
?x c:资料类型 ?资料类型 .}
执行过程中发现sesame工具不支持中文本本体,因此采用编程手段了实现该查询
5 测试代码:
采用读文件的办法,采用Jena的编程接口来实现sqarql查询,并解决了中文问题。例子如下:
package RWRdfOwl;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.ResultSetFormatter;
import com.hp.hpl.jena.rdf.model.InfModel;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.reasoner.Reasoner;
import com.hp.hpl.jena.reasoner.ReasonerRegistry;
public class TestSPARQL {
/**
* @param args
*/
public static void main(String[] args) {
try{
OntModel model = ModelFactory.createOntologyModel();
FileInputStream file = new FileInputStream("G:\\wokspace\\java\\jena\\test.rdf");
InputStreamReader in = new InputStreamReader(file, "UTF-8");
model.read(file,null);
System.out.print("that is ok");
System.out.println("成功载入本体");
String prefix = "PREFIX c:<http://s.opencalais.com/1/pred/>"
+ "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>";
String slect = "SELECT ?externalID ?资料类型 ";
String where = "WHERE { " + "?k c:externalID" + " ?externalID ."
+" ?k c:资料类型 ?资料类型 ."
+" }";
Query query = QueryFactory.create(prefix + slect + where);
Reasoner reasoner = ReasonerRegistry.getOWLReasoner();
InfModel inf = ModelFactory.createInfModel(reasoner, model);
QueryExecution qe = QueryExecutionFactory.create(query, inf);
ResultSet results = qe.execSelect();
System.out.print(ResultSetFormatter.asText(results));//图形化显示结果。
qe.close();
}catch(Exception e)
{
e.printStackTrace();
}
}
}
运行结果如下:
-------------------------------------------------------------------
| externalID | 资料类型 |
===================================================================
| "http://www.deri.com" | "a327a4b0-8c6a-b911-3ef0-54c84c227222" |
| "http://www.deri.ie中国" | "a327a4b0-8c6a-b911-3ef0-54c84c22723b" |
-------------------------------------------------------------------
- SPARQL语言的使用总结
- 使用 SPARQL 查询 RDF
- 使用 SPARQL 查询 RDF
- RDF数据查询语言SPARQL
- sparql查询语言学习摘要
- Sparql语言模型(一)
- 一个SPARQL语句的说明
- Sparql与sql的比较
- [转】sparql查询语言学习摘要
- RDF数据查询语言SPARQL:初步
- Java使用SPARQL访问DBPedia Endpoint错误
- RDF与SPARQL的学习资料
- jena SPARQL查询的简单应用
- Semantic Web: SPARQL——RDF查询语言
- sparql 学习,理解sparql
- C语言的联合体的使用总结
- SQL语言的CASE使用总结
- 总结MySQL 的C语言接口使用
- URAL1780----格雷码的应用,数论
- POJ1458(最长公共子序列)
- BufferedInputStream与FileInputStream的IO效率对比
- linux内核的oops信息
- 漂亮,美观的图表之Matlab强势回归~~~~走你8
- SPARQL语言的使用总结
- CSV文件的读写
- Eclipse Indigo (3.7.2)安装ssh插件(struts2+hibernate3.x+spring)
- MongoDB的查询
- 批处理删除SVN标记
- Android雁翎刀之ImageView之定制头像
- HashMap的实现与优缺点
- API 和 SDK的区别与联系
- 从工程师到管理者转变——来自项目管理群的讨论