利用RDFLib的SPARQL进行查询的一个例子
2015-10-09 10:26
603 查看
利用RDFLib的SPARQL进行查询的一个例子
该例子实现如下功能:
1. Import RDFLib.
2. 创建一个简单的graph(包含2到4个关系)
3. 将graph以rdf格式写入文件(Graph.serialize)
4. 从文件中读取graph
5.利用SPARQL从graph中提取信息(Graph.parse)
Reference
1. http://stackoverflow.com/questions/16829351/is-there-a-hello-world-example-for-sparql-with-rdflib
该例子实现如下功能:
1. Import RDFLib.
2. 创建一个简单的graph(包含2到4个关系)
3. 将graph以rdf格式写入文件(Graph.serialize)
4. 从文件中读取graph
5.利用SPARQL从graph中提取信息(Graph.parse)
import rdflib g = rdflib.Graph() has_border_with = rdflib.URIRef('http://www.example.org/has_border_with') located_in = rdflib.URIRef('http://www.example.org/located_in') germany = rdflib.URIRef('http://www.example.org/country1') france = rdflib.URIRef('http://www.example.org/country2') china = rdflib.URIRef('http://www.example.org/country3') mongolia = rdflib.URIRef('http://www.example.org/country4') europa = rdflib.URIRef('http://www.example.org/part1') asia = rdflib.URIRef('http://www.example.org/part2') g.add((germany,has_border_with,france)) g.add((china,has_border_with,mongolia)) g.add((germany,located_in,europa)) g.add((france,located_in,europa)) g.add((china,located_in,asia)) g.add((mongolia,located_in,asia)) q = "select ?country where { ?country <http://www.example.org/located_in> <http://www.example.org/part1> }" x = g.query(q) print list(x) # write graph to file, re-read it and query the newly created graph g.serialize("graph.rdf") g1 = rdflib.Graph() g1.parse("graph.rdf", format="xml") x1 = g1.query(q) print list(x1)
Reference
1. http://stackoverflow.com/questions/16829351/is-there-a-hello-world-example-for-sparql-with-rdflib
相关文章推荐
- 找出有向图中的弱联通分量
- MVC设计模式
- vs重装找不到 $(WindowsSdkDir) 配置问题
- mysql+quartz
- Angularjs基础知识
- C++中的指针与引用、如何参数传递
- PSR-3 日志接口规范 (中文版)
- C++中的指针与引用、如何参数传递
- TCP的流模式与UDP的报文模式对比
- JQuery AJAX 分页,跳页下一页,上一页【总结了一天啊干货】
- 一般来说我们总会在设置Block之后,在合适的时间回调Block,而不希望回调Block的时候Block已经被释放了,所以我们需要对Block进行copy,copy到堆中,以便后用。 当一个Bloc
- 人工智能●专家系统
- [LintCode] Palindrome Partitioning II
- 词法分析
- 文件File封装(读、写)
- 欢迎使用CSDN-markdown编辑器
- cocoapods一些坑
- 1人制作手游2月收入1259万:开发者意外成功的背后
- QT5 在VS里用QT Designer 打开UI文件资源丢失情况解决
- android中的ListView与ScrollView冲突保留ScrollView滚动