SPARQL query on the remote remote endpoint RDFLib / Redland

孤街浪徒 提交于 2019-11-30 19:22:10

Various things:

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

... see the results here.

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.

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']

As you can see the remote SPARQL endpoint becomes a parameter outside the query.

Redland does not currently support using SPARQL endpoints in FROM. What you are using here are are graph names that you load into the RDF Dataset. Also known as redland contexts when you load a triple (s, p, o) + c with something like model.context_add_statement(statement, context)

Rasqal GIT does support parsing SERVICE but not yet executing it in a query.

You could also consider using Virtuoso with RedLand as it implement the SPARQL-FED "Service" param for remote query execution as demonstrated in these online examples

There's another simple solution in the blog entry at http://terse-words.blogspot.com/2012/01/get-real-data-from-semantic-web.html which keeps the code fairly clean. It uses SPARQLWrapper as well.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!