1

I'm trying to run a simple query:

"""
    PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
    SELECT ?label
    WHERE { ?a rdfs:label ?label }
""")

Over my GraphDB repository, but from Python. After some searching I found that SPARQL wrapper can be used for this, but I'm not sure how to make python connect to my GraphDB (endpoint?). I have no experience with API's. What I have so far:

from SPARQLWrapper import SPARQLWrapper, JSON

sparql = SPARQLWrapper("http://192.168.0.12:7200/repositories/MyRepository")
sparql.setQuery("""
    PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
    SELECT ?label
    WHERE { ?a rdfs:label ?label }
""")
sparql.setReturnFormat(JSON)
results = sparql.query().convert()

for result in results["results"]["bindings"]:
    print(result["label"]["value"])

print('---------------------------')

for result in results["results"]["bindings"]:
    print('%s: %s' % (result["label"]["xml:lang"], result["label"]["value"]))

I copied this example query from another stackoverflow, but only changed the subject of the where query so it works with my data, and it works with the repository they pointed to there (http://dbpedia.org/sparql), but not with the link that I get from the repository in graphDB.

Any ideas?

Robin
  • 135
  • 10
  • the question: do you get an exception or just an empty resultset? – UninformedUser Jun 09 '21 at 08:16
  • Try with a simple select query to see if you get any result. The example above works on my side, with the exception of last print statement. select * where { ?s ?p ?o . } limit 100 – Konstantin Petrov Jun 09 '21 at 08:36
  • Interesting! I actually get the following error: urllib.error.URLError: – Robin Jun 09 '21 at 09:36
  • Which to me indicates that my python is not communicating properly with my repository on graphDB. This is the bit I'm confused about: Am I pointing towards my repository correctly using sparql = SPARQLWrapper("http://192.168.0.12:7200/repositories/MyRepository") – Robin Jun 09 '21 at 09:38
  • Your hunch sounds about right, so you should now remove the chrome and debug the connection directly. Start with `curl -v http://192.168.0.12:7200/repositories/MyRepository` and see what comes back. – alexis Jun 09 '21 at 09:49
  • But it may be that this is not a [python], [sparql] or [sparqlwrapper] problem... – alexis Jun 09 '21 at 09:51
  • @alexis, thank you for your response. Perhaps I'm too inexperienced, but I get a syntax error when I run your line of code. I pipinstalled curl and pycurl. " import curl curl -v http://192.168.0.12:7200/repositories/MyRepository " What does it mean when you say I should "remove the chrome" ? I have also tried running it without the 192.168.0.12. – Robin Jun 09 '21 at 10:21
  • Hi, you have too many quotes. Replace sparql.setQuery(""" PREFIX rdfs: SELECT ?label WHERE { ?a rdfs:label ?label } """) with sparql.setQuery("PREFIX rdfs: SELECT ?label WHERE { ?a rdfs:label ?label }") – Sava Savov Jun 09 '21 at 11:22
  • @SavaSavov why too many quotes? This is Python multiline string notation. – UninformedUser Jun 09 '21 at 12:43
  • 1
    @Robin, curl is a command-line program. Use it to test your gdb endpoint without risk that something in your python toolstack is getting in the way. – alexis Jun 09 '21 at 12:51
  • I have found what was wrong. When I change my repository location link to: sparql = SPARQLWrapper("http://localhost:7200/repositories/MyRepository") it works. @SavaSavov, Thank you for the suggestion, but this part of the query was working. – Robin Jun 09 '21 at 13:03

2 Answers2

1

Considering that the solution mentioned by Robin may not work for everyone, I'll introduce the formal method for this case:

  1. Open your GraphDB -> Right sidebars -> Setup -> Repositories
  2. Find the interested repository -> Click the "Copy repository URL to clipboard" button
  3. Enter the copied URL between the quotations sparql = SPARQLWrapper("")

Then it should work fine.

phwt
  • 1,356
  • 1
  • 22
  • 42
Zheng Feng
  • 11
  • 1
0

The key is to make sure the link you are using to connect to your repository is not the version GraphDB gives you, but the localhost version of that link. GraphDB provides you with an ipadress link. You have to replace the ip adress with "localhost" and it will work. (at least locally).

Robin
  • 135
  • 10
  • The key is to make sure that whatever form you use is accessible from your environment. GraphDB might be running on a separate host or VM, in which case localhost would not have worked. But good that you solved your problem... – alexis Jun 09 '21 at 13:49