A memorandum when you want to see the data acquired by Jena & SPARQL for each variable.

When I googled with Jena & SPARQL ...

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?

Resource and Literal classes

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 ...

  1. It is possible to loop with hasNext () like ResultSet in SQL.
  2. Get one result as a QuerySolution object
  3. Get the value of each variable with an RDFNode object
  4. Get data with asResource () for resources and asLiteral () for literals
  5. The value can be obtained with the getter of the Resource or Literal object.

As far as I can tell, it seems like this. If you use it incorrectly, please point it out. m (_ "_ m)

Recommended Posts

A memorandum when you want to see the data acquired by Jena & SPARQL for each variable.
[Ruby] When you want to assign the result obtained by conditional branching to a variable and put it in the argument
When you want to ZIP download the image data saved locally
A memo when you want to clear the time part of the calendar
When you want Rails to disable a session for a specific controller only
When you want to use the method outside
If you want to know the options when configuring Ruby, see `RbConfig :: CONFIG ["configure_args "]`
When you want to change the wording to be displayed when making a select box from enum
Use JLine when you want to handle keystrokes on the console character by character in Java
When you want to change the MySQL password of docker-compose
A memorandum to reach the itchy place for Java Gold
[Swift] When you want to know if the number of characters in a String matches a certain number ...
When you want to check whether the contents of a property can be converted to a specific type
If you want to mock a method in RSpec, you should use the allow method for mock and the singleton method.
I want to create a chat screen for the Swift chat app!
A story that could be implemented neatly by using polymorphism when you want to express two types of data in one table
When you want to add a string type column with a limited length with the `rails generate migration` command
[For beginners] I want to automatically enter pre-registered data in the input form with a selection command.
[RSpec] When you want to use the instance variable of the controller in the test [assigns is not recommended]
I want you to use Enum # name () for the Key of SharedPreference
Are you still exhausted by the sample video search? A button to send FANZA videos to Slack when pressed.
LIMIT 11 when data is acquired by irb
What to do when you want to delete a migration file that is "NO FILE"
How to create a route directly from the URL you want to specify + α
How to display the amount of disk used by Docker container for each container
I want you to use Scala as Better Java for the time being
[java tool] A useful tool when you want to send the ipmsg log of PC-A to the specified PC on a regular basis.