Search code examples
scalasparqlrdfblazegraphrdf4j

How can I dump embedded Blazegraph contents to an RDF file?


I have created a blazegraph RDF4J repository and connection in Scala:

val props = new Properties()
props.put(Options.BUFFER_MODE, BufferMode.DiskRW)
props.put(Options.FILE, "embedded.jnl")
var sail = new BigdataSail(props)
var repo = new BigdataSailRepository(sail)
repo.initialize()
var cxn = repo.getConnection()

I can add statements, retrieve SPARQL results, etc.

Now I'd like to dump the contents of the repository to an RDF file, like this:

Rio.write(model, System.out, RDFFormat.RDFXML);

But if I try to substitute my cxn or repo for the expected model argument, Eclipse complains:

overloaded method value write with alternatives: (x$1: Iterable[org.openrdf.model.Statement],x$2: java.io.Writer,x$3: org.openrdf.rio.RDFFormat)Unit (x$1: Iterable[org.openrdf.model.Statement],x$2: java.io.OutputStream,x$3: org.openrdf.rio.RDFFormat)Unit cannot be applied to (com.bigdata.rdf.sail.BigdataSailRepository, java.io.FileOutputStream, org.openrdf.rio.RDFFormat).

How do I get from the repo and connection that I have to a model expected by Rio.write()? Or can I dump the triples in some other way?


Solution

  • It is quite nicely described here http://docs.rdf4j.org/programming/ point 3.2.8. Using RDFHandlers

    import org.eclipse.rdf4j.rio.Rio;
    import org.eclipse.rdf4j.rio.RDFFormat;
    import org.eclipse.rdf4j.rio.RDFWriter;
    
    try (RepositoryConnection conn = repo.getConnection()) {
    RDFWriter writer = Rio.createWriter(RDFFormat.TURTLE, System.out);
    conn.prepareGraphQuery(QueryLanguage.SPARQL,
       "CONSTRUCT {?s ?p ?o } WHERE {?s ?p ?o } ").evaluate(writer);
    }
    

    And instead of System.out write to a file.