How to get rdf file from sparql endpoint

余生长醉 提交于 2020-01-13 04:16:00

问题


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 ;)



来源:https://stackoverflow.com/questions/9625129/how-to-get-rdf-file-from-sparql-endpoint

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