python + sparql+ jena 实例
程序员文章站
2022-06-12 08:41:31
...
全数据查询:
from SPARQLWrapper import SPARQLWrapper, JSON
sparql = SPARQLWrapper("http://IP:PORT/test")
sparql.setQuery("""
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?s ?p ?o
WHERE {?s ?p ?o }
""")
sparql.setReturnFormat(JSON)
results = sparql.query().convert()
print(results)
固定三元组的第一个元素:
from SPARQLWrapper import SPARQLWrapper, JSON
sparql = SPARQLWrapper("http://IP:PORT/test")
sparql.setQuery("""
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?p ?o
WHERE {<http://test.com/testKG/resource/7b54a2b0373e9205ad5131f213de267879cbb114> ?p ?o }
""")
sparql.setReturnFormat(JSON)
results = sparql.query().convert()
print(results)
下面这个注意?o要和FILTER里的?o名字一致
from SPARQLWrapper import SPARQLWrapper, JSON
sparql = SPARQLWrapper("http://10.100.208.195:3030/presale_test")
sparql.setQuery("""
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?s ?p ?o
WHERE {?s ?p ?o. FILTER (?o='match_str')}
""")
sparql.setReturnFormat(JSON)
results = sparql.query().convert()
print(results)