0

This is a follow-up question from How to prevent triples from getting mixed up while uploading to Dydra programmatically?

I've created a new graph using SPARQL CONSTRUCT query. I now want to iterate over it so that I can add the statements to an RDFlib graph and then insert the data into another triplestore. I have the following questions:

  1. If SPARQL CONSTRUCT returns a graph, do I still need to iterate over the statements and add them to an RDFlib graph? I probably need to do so to be able to insert each triple into a triplestore using a loop.
  2. How does one iterate over a graph resulting from SPARQL CONSTRUCT to retrieve the triples? The 'type' of 'output' shows up as string.

This is my code:

sesameSparqlEndpoint = 'http://my.ip.ad.here:8080/openrdf-sesame/repositories/rep_name'
sparql = SPARQLWrapper(sesameSparqlEndpoint)
queryStringDownload = 'CONSTRUCT {?s ?p ?o} WHERE {?s ?p ?o FILTER REGEX(str(?s), "http")}'
dataGraph = Graph()

sparql.setQuery(queryStringDownload)
sparql.method = 'GET'
sparql.setReturnFormat(JSON)
output = sparql.query().convert()
# print output

#Print all statements in dataGraph      
for stmt in output:
    print stmt

This code just gives me a single column of characters from the triples.

Community
  • 1
  • 1
kurious
  • 1,024
  • 10
  • 29
  • Not knowing RDFLib I don't know whether this is the cause, but: have you tried setting a different return format? I would expect something like Turtle to be better supported. – Jeen Broekstra Dec 23 '15 at 06:40
  • 1
    Works with XML (but not other formats such as N3 or Turtle)! I can now retrieve each of triples individually using the for loop. Also, we need to specifically use this at the beginning: from SPARQLWrapper import SPARQLWrapper, JSON, XML, TURTLE, RDF, N3 (basically all formats that we plan to use) – kurious Dec 23 '15 at 21:35

1 Answers1

0
  1. I still had to add statements from the Conjunctive Graph created as a result of running the CONSTRUCT query to an RDFlib graph. Why? Because there were issues with parsing the former when doing INSERT. The output of CONSTRUCT query: a) Doesn't have the URIs enclosed within <> b) Doesn't handle non-ASCII characters (hence output.encode('utf-8') is needed).
  2. To retrieve the triples from the graph generated from the CONSTRUCT query, we need to use XML format for output instead of JSON in the code above.

Interesting aside: The type(output) when using XML is Conjunctive Graph. For everything else I tried, it's a string.

kurious
  • 1,024
  • 10
  • 29