Search code examples
pythonrdfsesamerdflib

How to export rdf from a specific context for sesame database?


I managed to output the statement from sesame database. When I export, it will export the whole data set from that repository. Is there any way to only export from a specific context?

endpoint = "http://localhost:8080/openrdf-sesame/repositories/reference/statements"

from rdflib import Graph
g = Graph()
g.parse(endpoint) 

import pprint
outputfile = open("d:\\testrdfexport" + ".rdf", "w")
for stmt in g:
    pprint.pprint(stmt, outputfile)

Solution

  • There are several ways to do this, but the simplest approach for you is to just add a context parameter to your request. The value of this parameter should be the IRI of the context you want to extract, in N-Triples syntax (that is, with angle brackets around it):

    <http://example.org/context1>
    

    The full request (with proper encoding) then becomes:

    http://localhost:8080/openrdf-sesame/repositories/reference/statements?context=%3Chttp%3A%2F%2Fexample.org%2Fcontext1%3E
    

    Other approaches are using the equivalent Graph Store Protocol operation (basically an alternative REST API call that does the exact same thing), or instead of doing an export operation you can of course also execute a SPARQL (CONSTRUCT) query that retrieves the data from a particular named graph.

    See the Sesame REST API documentation for details.