I usually write like this.
JenaSample.java
String queryString = "(SPARQL)";
Query query = QueryFactory.create(queryString);
QueryExecution qe = QueryExecutionFactory.sparqlService(service, query);
ResultSet results = qe.execSelect();
ResultSetFormatter.out(System.out,results, query);
The results are displayed in a list, and I'm happy. I'd like to say ... What should I do if I want to get the value for each variable?
For example, when requesting a food ingredient LOD SPARQL endpoint. Food Ingredients LOD
SPARQL looks like this.
SPARQL
PREFIX schema: <http://schema.org/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX cc: <http://creativecommons.org/ns#>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT DISTINCT ?name ?calories ?sodiumContent ?nutritionType ?title
WHERE {
?s schema:category "seafood";
schema:name ?name;
schema:nutrition ?nutrition;
cc:attributionName ?attributionName.
?nutrition schema:calories ?calories;
schema:sodiumContent ?sodiumContent;
rdf:type ?nutritionType.
?attributionName dc:title ?title.
}
LIMIT 10
In Java, try taking it out like this.
JenaSample2.java
Query query = QueryFactory.create(queryString);
QueryExecution qe = QueryExecutionFactory.sparqlService(service, query);
ResultSet results = qe.execSelect();
while (results.hasNext()) {
QuerySolution qs = results.next();
RDFNode node = qs.get("s");
Resource resource = node.asResource();
System.out.println(resource.getURI());
RDFNode node2 = qs.get("calories");
Literal literal = node2.asLiteral();
System.out.println(literal.getInt());
}
Explaining the source ...
As far as I can tell, it seems like this. If you use it incorrectly, please point it out. m (_ "_ m)
Recommended Posts