1

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)
....
Taryn
  • 242,637
  • 56
  • 362
  • 405
Sareh
  • 11
  • 1
  • 2
  • Are you sure that is all your code? The code doesn't look like you've done anything that could cause that error trace so is there other code you have omitted? The error trace seems to be referring to problems reading a file but your code is concerned with writing to a file which seems odd to me – RobV Jun 26 '11 at 17:12
  • Also though you closed the `BufferedWriter` you haven't closed the `FileStream` so I'd ensure that was closed as well and see if that makes any difference – RobV Jun 26 '11 at 17:12
  • No, it's not all my code, it's a part of it! These errors are related when I want to take query from my outputfile (here its name is dbpediafilmdataset)! Is there any way to save sparql query in a rdf file? – Sareh Jun 26 '11 at 19:16

1 Answers1

3

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 
}
Community
  • 1
  • 1
Manuel Salvadores
  • 16,287
  • 5
  • 37
  • 56