Reputation: 11
I want to save all triples of dbpedia
films on a N-Triple file (.nt file), I take a query on dbpedia endpoint with a java application and save the result on a file, but I can not read this file and take query from it! could u help me?
//my code for save the result is here
try{
FileWriter fostream = new FileWriter("C:\\Documents and Settings\\me\\Desktop\\DataSets\\dbpediafilmdataset.nt");
BufferedWriter out = new BufferedWriter(fostream);
String service ="http://dbpedia.org/sparql";
String query =
"SELECT ?s ?p ?o " +
"WHERE {" +
" ?s <http://dbpedia.org/property/wordnet_type> <http://www.w3.org/2006/03/wn/wn20/instances/synset-movie-noun-1> ; ?p ?o "+
" } ";
QueryExecution qexecctest = QueryExecutionFactory.sparqlService(service, query);
try {
ResultSet responseetest = qexecctest.execSelect();
while( responseetest.hasNext()){
QuerySolution solnntest = responseetest.nextSolution();
RDFNode p = solnntest.get("?p");
RDFNode o = solnntest.get("?o");
RDFNode s = solnntest.get("?s");
String object="";
String triple="";
if (o.isLiteral()==true)
{
object="\"" + o.toString() + "\"";
}
else
object="<" + o.toString() + ">";
triple="<" + s + ">"+" " + "<" + p + ">" + " " + object + " " + "." ;
out.write(triple);
out.newLine();
}
} finally {
qexecctest.close();
out.close();}
}catch (Exception e){
System.err.println("Error: " + e.getMessage());}
when I want to read the result file and take query from it, it gives some error like these:
com.hp.hpl.jena.rdf.model.impl.IStream.readChar(NTripleReader.java:485)
com.hp.hpl.jena.rdf.model.impl.NTripleReader.unwrappedReadRDF(NTripleReader.java:140)
com.hp.hpl.jena.rdf.model.impl.NTripleReader.readRDF(NTripleReader.java:120)
com.hp.hpl.jena.rdf.model.impl.NTripleReader.read(NTripleReader.java:84)
com.hp.hpl.jena.rdf.model.impl.NTripleReader.read(NTripleReader.java:72)
com.hp.hpl.jena.rdf.model.impl.ModelCom.read(ModelCom.java:226)
com.hp.hpl.jena.util.FileManager.readModelWorker(FileManager.java:395)
com.hp.hpl.jena.util.FileManager.readModel(FileManager.java:335)
com.hp.hpl.jena.util.FileManager.readModel(FileManager.java:319)
....
Upvotes: 1
Views: 2587
Reputation: 16525
It is better to do this with a CONSTRUCT query. Have a look at it, it is specifically design for this purpose. With Jena just do something like:
Model results = qexec.execConstruct();
results.write(out, "TURTLE");
It is also explained here
The major advantage is that you don't need to worry about writing the triples in Turtle or any other format, Jena will do that for you.
Your CONSTRUCT query might end up looking like:
CONSTRUCT { ?s ?p ?o }
WHERE {
?s <http://dbpedia.org/property/wordnet_type>
<http://www.w3.org/2006/03/wn/wn20/instances/synset-movie-noun-1> ;
?p ?o
}
Upvotes: 3