远程远程端点RDFLib/Redland上的SPARQL查询 [英] SPARQL query on the remote remote endpoint RDFLib / Redland
问题描述
我正在尝试查询远程端点并获取owl:sameAs映射,我已经尝试了RDFLib和Redland,但都没有为我工作,可能我没有正确处理名称空间.
I'm trying to query remote endpoints and get get owl:sameAs mappings, I've tried both RDFLib and Redland but neither worked for me, probably I'm not dealing with namespaces correctly.
这是我尝试RDFLib的尝试:
Here is my attempt in RDFLib:
import rdflib
rdflib.plugin.register('sparql', rdflib.query.Processor, 'rdfextras.sparql.processor', 'Processor')
rdflib.plugin.register('sparql', rdflib.query.Result, 'rdfextras.sparql.query', 'SPARQLQueryResult')
g = rdflib.Graph()
query = """
SELECT *
FROM <http://api.talis.com/stores/bbc-backstage/services/sparql>
WHERE {
?s a http://purl.org/ontology/mo/MusicArtist;
http://www.w3.org/2002/07/owl#sameAs ?o .
}Limit 50
"""
for row in g.query(query):
print row
这是Redland:
import RDF
model = RDF.Model()
query = """
SELECT *
FROM <http://api.talis.com/stores/bbc-backstage/services/sparql>
WHERE {
?s a http://purl.org/ontology/mo/MusicArtist;
http://www.w3.org/2002/07/owl#sameAs ?o .
}Limit 50
"""
for statement in RDF.Query(query ,query_language="sparql").execute(model):
print statement
您能给我一个提示吗? 我还有另一个困难:是否可以获取对象的数据集名称?例如:如果存在:
Can you please give a hint what is wrong in any one of those? Yet another difficulty I have: Is it possible to get dataset name of the object? For example: if there is:
?s = http://www.bbc.co.uk/music/artists/eb5c8564-927d-414d-b152-c7b48a2c9d8b#artist
predicate = http://www.w3.org/2002/07/owl#sameAs
?0 = http://dbpedia.org/resource/The_Boy_Least_Likely_To
在此示例中,我可以获取"Dbpedia"的名称吗?或我拥有sameAs链接的任何其他数据集? (或者也许我可以在对象字符串中查找感兴趣的数据集名称),非常感谢!
Can I get name of the "Dbpedia" in this example? Or any other dataset to which I'm having sameAs link? (Or probably I could just look-up interested dataset names in the object string) thank you very VERY much in advance
推荐答案
各种事物:
是的,您需要将任何URI括在<
>
中.正确的查询是:
You are right, you need to enclose any URI within <
>
. The correct query is:
SELECT ?s ?o WHERE {
?s a <http://purl.org/ontology/mo/MusicArtist>;
<http://www.w3.org/2002/07/owl#sameAs> ?o .
} limit 50
...查看结果 FROM
并未像您认为的那样在rdflib或redland中实现.它不会获取远程SPARQL端点,而是在本地存储区中获取远程图或具有该名称的图.您要使用SERVICE
的方法,请参见它在Jena中如何工作.不幸的是,rdflib和redland都没有为SPARQL实现SERVICE
子句,但是有解决方法.
FROM
is not implemented in rdflib or redland as you think it is. It does not fetch remote SPARQL endpoints it fetches remote graphs or graphs with that name in a local store. In your case you want to use SERVICE
see how it works here with Jena. Unfortunately, neither rdflib nor redland implement the SERVICE
clause for SPARQL but there are workarounds to sort this out.
一种可能的解决方案是使用用于python的SPARQLWrapper .这很简单,在这里您可以使用该库获取代码:
One possible solution is to use SPARQLWrapper for python. It is trivial, here you have your code with that library:
from SPARQLWrapper import SPARQLWrapper, JSON
sparql = SPARQLWrapper("http://api.talis.com/stores/bbc-backstage/services/sparql")
sparql.setQuery("""
SELECT ?s ?o
WHERE {
?s a <http://purl.org/ontology/mo/MusicArtist>;
<http://www.w3.org/2002/07/owl#sameAs> ?o .
} limit 50
""")
sparql.setReturnFormat(JSON)
results = sparql.query().convert()
for result in results["results"]["bindings"]:
print result["s"]['value'], result["o"]['value']
如您所见,远程SPARQL端点成为查询之外的参数.
As you can see the remote SPARQL endpoint becomes a parameter outside the query.
这篇关于远程远程端点RDFLib/Redland上的SPARQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!