Reputation: 3096
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?
Upvotes: 2
Views: 847
Reputation: 22052
Yet another way to achieve this is as follows:
var out = new FileOutputStream("rdf.ttl")
Rio.write(cxn.getStatements(null,null,null), out, RDFFormat.TURTLE)
This works because the output of getStatements
is a RepositoryResult
object, which inherits from Iteration<Statement>
, and as such can be fed directly into the RDFHandler
.
You can also do this:
var writer = Rio.createWriter(RDFFormat.TURTLE, out)
cxn.export(writer)
The advantage of using export
over getStatements
is that it will also write any namespace declarations existing in your repository to the file.
The advantage of either of these approaches over the other answers is that you bypass the SPARQL query parser altogether - so it's more efficient for large repos.
Upvotes: 2
Reputation: 3096
This Scala code worked for me. It's entirely based on ChristophE's answer. I already had a connection, but I did need to create a file output stream. I removed the try wrapper since there wasn't any catch block. Not recommended for production!
var out = new FileOutputStream("rdf.ttl")
var writer = Rio.createWriter(RDFFormat.TURTLE, out)
cxn.prepareGraphQuery(QueryLanguage.SPARQL,
"CONSTRUCT {?s ?p ?o } WHERE {?s ?p ?o } ").evaluate(writer)
Upvotes: 2
Reputation: 873
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.
Upvotes: 2