4

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 ?

Cœur
  • 37,241
  • 25
  • 195
  • 267
hudi
  • 15,555
  • 47
  • 142
  • 246

3 Answers3

9

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.

danja
  • 1,030
  • 2
  • 10
  • 15
  • can you write here sparql which return from dbpedia all rdf file where is mention something about berlin ? – hudi Mar 08 '12 at 23:31
  • 2
    @hudi It sounds like you are just starting out with SPARQL, have you tried reading one of the popular tutorials like http://www.cambridgesemantics.com/2008/09/sparql-by-example/ which explains it step by step? – RobV Mar 09 '12 at 01:15
  • yes I read this but still no understand sparql because in this example is just query and result but no describton of sparql query – hudi Mar 09 '12 at 07:58
4

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.

wikier
  • 2,517
  • 2
  • 26
  • 39
  • Which in any SPARQL 1.1 compliant implementation should be equivalent to a [DESCRIBE](https://www.w3.org/TR/sparql11-query/#describe) query: `DESCRIBE `. – wikier Aug 08 '16 at 13:54
  • can i ask you one question about the rdf saving locally??? what is the different i just use results.serialize(destination=path , format="xml") ,,,, and use file=open(......."w").......file.close()... your answer help me solve the problem. but i would like to know the difference. – bob90937 Nov 11 '16 at 07:23
2

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

zazi
  • 686
  • 1
  • 7
  • 19