Fuseki returns no results when called from Python vs curl or online

101 Views Asked by At

I'm setting up Fuseki/Jena to host a persistent collection of rdf and I'd like to control uploads through a python interface using rdflib graph objects as a means of pre-filtering/mastering the content before it reaches the persistence layer.

I've started the server, and can access data via the online SPARQL endpoint, or by curl-ing the store with simple command like:

curl -X POST -d "query=select ?s ?p ?o where { ?s ?p ?o.}" localhost:3030/models/query

Which returns multiple results, as per expected.

It also generates the following lines in the server log:

14:21:01 INFO  Fuseki          :: [49] POST http://localhost:3030/models/query
14:21:01 INFO  Fuseki          :: [49] Query = select ?s ?p ?o where { ?s ?p ?o.}
14:21:01 INFO  Fuseki          :: [49] 200 OK (34 ms)

I'd like to access this sparql enpoint in python via the rdflib graph.query api - and have tried the following:

from rdflib import Graph
from rdflib.plugins.stores import sparqlstore

jena = sparqlstore.SPARQLStore("http://localhost:3030/models/query")
g = Graph(store=jena)
qr = g.query("""select ?s ?p ?o where { ?s ?p ?o. }""")
[r for r in qr]

But this returns no results.

The log entry for this action is a little longer, and looks like this:

14:22:09 INFO  Fuseki          :: [50] GET http://localhost:3030/models/query?query=PREFIX+xml%3A+%3Chttp%3A%2F%2Fwww.w3.org%2FXML%2F1998%2Fnamespace%3E%0APREFIX+rdf%3A+%3Chttp%3A%2F%2Fwww.w3.org%2F1999%2F02%2F22-rdf-syntax-ns%23%3E%0APREFIX+rdfs%3A+%3Chttp%3A%2F%2Fwww.w3.org%2F2000%2F01%2Frdf-schema%23%3E%0APREFIX+xsd%3A+%3Chttp%3A%2F%2Fwww.w3.org%2F2001%2FXMLSchema%23%3E%0APREFIX+xml%3A+%3Chttp%3A%2F%2Fwww.w3.org%2FXML%2F1998%2Fnamespace%3E%0APREFIX+rdf%3A+%3Chttp%3A%2F%2Fwww.w3.org%2F1999%2F02%2F22-rdf-syntax-ns%23%3E%0APREFIX+rdfs%3A+%3Chttp%3A%2F%2Fwww.w3.org%2F2000%2F01%2Frdf-schema%23%3E%0APREFIX+xsd%3A+%3Chttp%3A%2F%2Fwww.w3.org%2F2001%2FXMLSchema%23%3E%0A%0Aselect+%3Fs+%3Fp+%3Fo+where+%7B+%3Fs+%3Fp+%3Fo.+%7D&default-graph-uri=Nf346bf5146514bcd8940bf9a31ae9c9c
14:22:09 INFO  Fuseki          :: [50] Query = PREFIX xml: <http://www.w3.org/XML/1998/namespace> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> PREFIX xml: <http://www.w3.org/XML/1998/namespace> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>  select ?s ?p ?o where { ?s ?p ?o. }
14:22:09 INFO  Fuseki          :: [50] 200 OK (12 ms)

I can see that the majority of the difference here is some additional PREFIX notation, and that the call appears to be a get, rather than a POST. Additionally, there's an additional parameter at the end of the call, referencing default-graph-uri - I don't think I'm using this, or haven't specifically set anything up as a named default graph.

If I try an alternate curl however, I can copy the GET call and get it to work by dropping the final default-graph-uri parameter:

curl http://localhost:3030/models/query?query=PREFIX+xml%3A+%3Chttp%3A%2F%2Fwww.w3.org%2FXML%2F1998%2Fnamespace%3E%0APREFIX+rdf%3A+%3Chttp%3A%2F%2Fwww.w3.org%2F1999%2F02%2F22-rdf-syntax-ns%23%3E%0APREFIX+rdfs%3A+%3Chttp%3A%2F%2Fwww.w3.org%2F2000%2F01%2Frdf-schema%23%3E%0APREFIX+xsd%3A+%3Chttp%3A%2F%2Fwww.w3.org%2F2001%2FXMLSchema%23%3E%0APREFIX+xml%3A+%3Chttp%3A%2F%2Fwww.w3.org%2FXML%2F1998%2Fnamespace%3E%0APREFIX+rdf%3A+%3Chttp%3A%2F%2Fwww.w3.org%2F1999%2F02%2F22-rdf-syntax-ns%23%3E%0APREFIX+rdfs%3A+%3Chttp%3A%2F%2Fwww.w3.org%2F2000%2F01%2Frdf-schema%23%3E%0APREFIX+xsd%3A+%3Chttp%3A%2F%2Fwww.w3.org%2F2001%2FXMLSchema%23%3E%0A%0Aselect+%3Fs+%3Fp+%3Fo+where+%7B+%3Fs+%3Fp+%3Fo.+%7D

Which returns the results I'd expect to see using a modified call from rdflib's api.

So my question is, how do I get rdflib to issue its queries without the breaking default-graph-uri parameter - OR - how should I configure Fuseki to handle this more gracefully in line with my expectations?

1

There are 1 best solutions below

2
chbroecker On

I tried your code with my local Fuseki instance and I also did not get any results with your Python code.

I am not sure if it is a simple copy and paste error on your part but the print in the last line is missing. If I add the print I get back the same triples from the curl command and the python query.

from rdflib.plugins.stores import sparqlstore

jena = sparqlstore.SPARQLStore("http://localhost:3030/models/query")
g = Graph(store=jena)
qr = g.query("""select ?s ?p ?o where { ?s ?p ?o. }""")
print([r for r in qr])

Regarding the default-graph-uri the following stackoverflow comment helped me a lot by showing how to set the default graph with rdflib. https://stackoverflow.com/a/71679855/7940658

My Fuseki configuration

I am using a pretty default Fuseki instance which is running in Docker. Below is my docker config if that might help you troubleshoot your issue but it doesn't seem to include any information about the default graph. Note that this is running Fuseki version 4.7.

  fuseki:
    image: "secoresearch/fuseki"
    restart: "always"
    ports:
      - 15158:3030
    volumes:
      - ./fuseki-data:/fuseki-base/databases
    environment:
      ADMIN_PASSWORD: <password>
      ENABLE_DATA_WRITE: "true"
      ENABLE_UPDATE: "true"
      ENABLE_UPLOAD: "true"
      QUERY_TIMEOUT: "5000"