Using ARQoid for Android-based SPARQL Query Execution
Thursday, December 1st, 2011I was recently asked about the SPARQL support in Sparql Droid and whether it could serve as a way for other Android applications to execute SPARQL queries against remote data sources. It could be used in this way but there is a simpler alternative I’d like to discuss here.
On the Android platform it is actually quite easy to execute SPARQL against remote SPARQL endpoints, RDF data and local models. The heavy lifting is handled by Androjena’s ARQoid, an Android-centric port of HP’s Jena ARQ engine.
Both engines (the original and the port) do a great job of simplifying the execution of SPARQL queries and consumption of the resulting data. In this post I’ll go through a simple example of using ARQoid. Note that all the code being shown here is available for download. This post is based specifically on the queryRemoteSparqlEndpoint() method in the com.monead.androjena.demo.arqoid.SparqlExamples class.
Setup
To begin, some environment setup needs to be done in order to have a properly configured Android project ready to use ARQoid.
First, obtain the ARQoid JAR and its dependencies. This is easily accomplished using the download page on the ARQoid Wiki and obtaining the latest ARQoid ZIP file. Unzip the downloaded archive. Since I’m discussing an Android application I’d expect that you would have created an Android project and that it contains a libs directory where the JAR files should be placed.
Second, add the JAR files to the classpath for your Android project. I use the ADT plugin for Eclipse to do Android development. So to add the JARs to my project I choose the Project menu item, select Properties, choose Build Path, select the Libraries tab, click the Add JARs… button, navigate to the libs directory, select the JAR files and click OK on the open dialogs.
Third, setup a minimal Android project. The default layout, with a small change to its definition will work fine.
Overview
Now we are ready to write the code that uses ARQoid to access some data. For this first blog entry I’ll focus on a trivial query against a SPARQL endpoint. There would be some small differences if we wanted to query a local model or a remote data set. Those will be covered in follow-on entries.
Here is a list of the ARQoid classes we will be using for this initial example:
- com.hp.hpl.jena.query.Query – represents the query being executed
- com.hp.hpl.jena.query.Syntax – represents the query syntaxes supported by ARQoid
- com.hp.hpl.jena.query.QueryFactory – creates a Query instance based on supplied parameters such as the query string and syntax definition
- com.hp.hpl.jena.query.QueryExecution – provides the service to execute the query
- com.hp.hpl.jena.query.QueryExecutionFactory – creates a QueryExecution instance based on supplied parameters such as a Query instance and SPARQL endpoint URI
- com.hp.hpl.jena.query.ResultSet – represents the returned data and metadata associated with the executed query
- com.hp.hpl.jena.query.QuerySolution – represents one row of data within the ResultSet.
We’ll use these classes to execute a simple SPARQL query that retrieves some data associated with space exploration. Talis provides an endpoint that we can use to access some interesting space exploration data. The endpoint is located at http://api.talis.com/stores/space/services/sparql.
The query we will execute is:
SELECT ?dataType ?data WHERE { <http://nasa.dataincubator.org/launch/1961-012> ?dataType ?data. }
This query will give us a little information about Vostok 1 launched by the USSR in 1961.