-->

Loading a .trig file into TDB?

2019-09-08 18:46发布

问题:

I am currently writing some code in Java, using Jena and TDB -- on a Windows 7.

I want to be able to load a (large) .trig file into TDB Dataset so that querying is a bit faster. The code that I currently have is:

Dataset dataset = TDBFactory.createDataset(directoryPath);
Model tdb = dataset.getDefaultModel();
RDFDataMgr.read(tdb, inputFilePath);
try {
        String theQuery = readFile(testQueryPath, Charset.defaultCharset());        
        Query query = QueryFactory.create(theQuery);
        QueryExecution qe = QueryExecutionFactory.create(query, dataset);
        com.hp.hpl.jena.query.ResultSet results =  qe.execSelect();

        // Output query results    
        ResultSetFormatter.out(System.out, results, query);
        qe.close();

} catch (IOException e) {
        e.printStackTrace();
}

I also tried:

FileManager.get().readModel( tdb, inputFilePath);

instead of:

RDFDataMgr.read(tdb, inputFilePath);

I get the following warning:

2014-06-13 13:02:26 WARN riot:77 - Only triples or default graph data expected : named graph data ignored

The SPARQL queries I ran are:

PREFIX xsd: http://www.w3.org/2001/XMLSchema#

PREFIX dc: http://purl.org/dc/elements/1.1/

PREFIX : <.>

SELECT *

{

{ ?s ?p ?o } UNION { GRAPH ?g { ?s ?p ?o } }

}

and also the same one but without the UNION and GRAPH things.

The queries return nothing.

Does anyone see an apparent problem or know how to load .trig files into TDB?

回答1:

You just read into the dataset:

RDFDataMgr.read(dataset, inputFilePath);

Inside write transaction would be better else call TDB.sync(dataset) before you exit.

You code loads it every run. If the file is really big, use tdbloader, the bulk loader, before running the query program.