Amrit Bhattacharjee
Amrit Bhattacharjee

Reputation: 160

How can I load a Jena Resultset to Jena Model

I have written a query on DBpedia.

SELECT * WHERE {
    <http://dbpedia.org/resource/London> ?p ?o.
    FILTER (langMatches(lang(?x),"en"))
}

And I want to load the results in a Jena model so that I can make queries on them later. How can I do that?

I have checked this answer. But this is not working or maybe I am not getting it properly. Would someone explain it with proper examples?

Upvotes: 2

Views: 115

Answers (1)

Ortomala Lokni
Ortomala Lokni

Reputation: 62653

As said in the comments, you can run a first CONSTRUCT SPARQL query on DBpedia to get a local model. You can then run a second SELECT SPARQL local query on that model.

final String dbPediaEndpoint = "https://dbpedia.org/sparql";

final Query remoteQuery = QueryFactory.create("""
        CONSTRUCT {
          ?subject ?predicate ?object .
        }
        WHERE {
          ?subject ?predicate ?object .
          FILTER (?subject = <http://dbpedia.org/resource/London>)
        }""");

final Query localQuery = QueryFactory.create("""
        SELECT ?subject ?object {
           ?subject <http://dbpedia.org/property/populationTotal> ?object
        }""");

try (QueryExecution remoteExecution = QueryExecutionHTTP.service(dbPediaEndpoint, remoteQuery)) {
  Model model = remoteExecution.execConstruct();
  try (QueryExecution localExecution = QueryExecutionFactory.create(localQuery, model)) {
    ResultSet resultSet = localExecution.execSelect();
    ResultSetFormatter.out(System.out, resultSet);
  }
}

Note: tested with Apache Jena 4.10.0

Upvotes: 0

Related Questions