How to get rdf file from sparql endpoint

2019-04-10 01:40发布

问题:

I am new in opendata and need some help. Wikipedia have their sparql endpoint in this url: http://dbpedia.org/sparql Now I need to write webservice to get some rdf file from dbpedia. What should I send to this endpoint to get rdf file ?

回答1:

Send a CONSTRUCT query. A little example:

CONSTRUCT { ?s ?p ?o }
WHERE { ?s ?p ?o }
LIMIT 10

The WHERE clause works just like that of SELECT only the values fill the CONSTRUCT block as a kind of template. It's very flexible - you can either copy statements as here or transform them into a completely different shape.



回答2:

What Danny answered is the right generic answer. But I would not recommend you to perform such query over external services, due the time expected to get the result; do it over a concrete resource

But of course if you'd like to do it directly without the need of manually save the query results, with Python for instance the code would look like:

from SPARQLWrapper import SPARQLWrapper, XML

uri = "http://dbpedia.org/resource/Asturias"
query = "CONSTRUCT { <%s> ?p ?o } WHERE { <%s> ?p ?o }" % (uri, uri)

sparql = SPARQLWrapper("http://dbpedia.org/sparql")
sparql.setQuery(query)
sparql.setReturnFormat(XML)
results = sparql.query().convert()

file = open("output.rdf", "w")
results.serialize(destination=file, format="xml")
file.flush()
file.close()

Of course, this could be done with almost any programming language, as you prefer.



回答3:

I would like to recommend you reading Bob DuCharme's "Learning SPARQL" book. It covers some examples that make use of the DBPedia endpoint as well.

PS: It's not Wikipedia's SPARQL endpoint - it's DBPedia SPARQL endpoint (Wikipedia itself doesn't provide an own SPARQL endpoint ATM). However, DBPedia data relies on Wikipedia data ;)