Parameterized SPARQL query with JENA

I am trying to create a small semantic web application using the Jena framework, JSP and JAVA. I have a remote SPARQL endpoint, and I already wrote a simple query that works fine, but now I need to use some parameters. Here is my code:

final static String serviceEndpoint = "http://fishdelish.cs.man.ac.uk/sparql/"; String comNameQuery = "PREFIX fd: <http://fishdelish.cs.man.ac.uk/rdf/vocab/resource/> " + "SELECT ?name ?language ?type" + "WHERE { ?nameID fd:comnames_ComName ?name ;" + "fd:comnames_Language ?language ;" + "fd:comnames_NameType ?type ." + "}"; Query query = QueryFactory.create(comNameQuery); QueryExecution qe = QueryExecutionFactory.sparqlService(serviceEndpoint,query); try { ResultSet rs = qe.execSelect(); if ( rs.hasNext() ) { System.out.println(ResultSetFormatter.asText(rs)); } } catch(Exception e) { System.out.println(e.getMessage()); } finally { qe.close(); } 

What I want to do is a parameterized name. I'm new to Jena and I'm not sure how to use parameters in a SPARQL query. I would appreciate it if someone would help me with this.

+7
source share
2 answers

If you just want to restrict a variable to a specific value for local queries, you can do this by overloading the QueryFactory.create() method, which takes a QuerySolutionMap value to set value limits. Please note that this does not change your query, it simply limits the final results, so this is not really a parameterization.

If you really want to have true parameterized queries (i.e. replace variables for constants), there are several ways to do this depending on your version of ARQ.

Using any current version (up to 2.9.0), the only way to do this is to concatenate strings, i.e. instead of having? name in your query, you simply enter the desired value, for example. "Bean"

Using the last trunk (hereinafter 2.9.1-SNAPSHOT), a new ParameterizedSparqlString class has appeared, which makes it much more user-friendly, for example.

 ParameterizedSparqlString queryStr = new ParameterizedSparqlString(comNameQuery); queryStr.setLiteral("name", "Bob"); Query query = QueryFactory.create(queryStr.toString()); 

And in fact, you can simplify your code, as ParameterizedSparqlString has a StringBuffer style interface and can be used to build a query in stages and includes useful functions, such as prefixes of prefixes to your query.

The advantage of this new method is that it provides a more general way to execute parameterized queries, which can also be used with updates, and can be used to prepare remote queries that existing methods do not cover.

+6
source

You can try looking at Twinkql . This is the SPARQL-to-Java mapping framework. It uses Jena at the back, but tries to simplify SPARQL queries and Java binding to results.

It allows you to define SPARQL queries in xml:

 <select id="getNovel" resultMap="novelResultMap"> <![CDATA[ SELECT ?novel ?author WHERE { ?novel a <http://dbpedia.org/class/yago/EnglishNovels> ; <http://dbpedia.org/property/name> "#{novelName}"@en ; <http://dbpedia.org/property/author> ?author . } ]]> </select> 

Pay attention to placeholder #{novelName} - here the parameters can be passed during the request.

In addition, the results can be tied to Java Beans:

 <resultMap id="novelResultMap" resultClass="org.twinkql.example.Novel"> <uniqueResult>novel</uniqueResult> <rowMap var="novel" varType="localName" beanProperty="name" /> <rowMap var="author" varType="localName" beanProperty="author"/> </resultMap> 

There is an API for calling these requests, for passing parameters, etc. This is very similar to MyBatis , but instead of SPARQL SQL.

+2
source

All Articles