While the command-line sparql
tool is useful for running standalone queries, Java applications can call on Jena’s SPARQL capabilities directly. SPARQL queries are created and executed with Jena via classes in the com.hp.hpl.jena.query
package. Using QueryFactory
is the simplest approach. QueryFactory
has various create()
methods to read a textual query from a file or from a String
. These create()
methods return a Query
object, which encapsulates a parsed query.
The next step is to create an instance of QueryExecution
, a class that represents a single execution of a query. To obtain a QueryExecution
, call QueryExecutionFactory.create(query, model)
, passing in the Query
to execute and the Model
to run it against. Because the data for the query is provided programmatically, the query does not need a FROM
clause.
There are several different execute methods on QueryExecution
, each performing a different type of query (see the sidebar entitled “Other types of SPARQL queries” for more information). For a simple SELECT
query, call execSelect()
, which returns a ResultSet
. The ResultSet
allows you to iterate over each QuerySolution
returned by the query, providing access to each bound variable’s value. Alternatively, ResultSetFormatter
can be used to output query results in various formats.
Example :
// Open the bloggers RDF graph from the filesystem
InputStream in = new FileInputStream(new File(“bloggers.rdf”));
// Create an empty in-memory model and populate it from the graph
Model model = ModelFactory.createMemModelMaker().createModel();
model.read(in,null); // null base URI, since model URIs are absolute
in.close();
// Create a new query
String queryString =
“PREFIX foaf: <http://xmlns.com/foaf/0.1/> ” +
“SELECT ?url ” +
“WHERE {” +
” ?contributor foaf:name \”Jon Foobar\” . ” +
” ?contributor foaf:weblog ?url . ” +
” }”;
Query query = QueryFactory.create(queryString);
// Execute the query and obtain results
QueryExecution qe = QueryExecutionFactory.create(query, model);
ResultSet results = qe.execSelect();
// Output query results
ResultSetFormatter.out(System.out, results, query);
// Important – free up resources used running the query
qe.close();
Source : http://www-128.ibm.com/developerworks/library/j-sparql/
February 16, 2010 at 12:57 pm
I have aggregated the sparql Query to run all words in a description and find all genres for a single event simultanesouly, Something along the lines of:
String queryString=
“PREFIX : ” +
“PREFIX a: ” +
“SELECT ?genre WHERE { \n”;
for (int i = 0; i maxSize; i++) {
String word = queries.get(i);
if (i+1 ==queries.size()||i == maxSize ) {
queryString+=” {:”+word+” a:genre ?genre}”;
break;
}
queryString+=” {:”+word+” a:genre ?genre} UNION “;
}
queryString+=”}”;
With this I have run into a problem, I can only run a query with a maximum length of around 2000 characters. If a description of an event has many words, around 50 or so, the query will not execute (404ERROR :|) :
– The current method of SPARQl execution in jena uses a HTTP GET request which has a limited length
– I need to figure out how to to force jena to use a HTTP POST request to allow for possibly infinitly long query strings.
How can this be done?
February 16, 2010 at 12:59 pm
Check out my Blog for more info.