Sending non-standard SPARQL queries with Apache Jena

Some non-standard extensions of SPARQL results in queries that are not syntactically compliant with SPARQL specifications. An example of such an extension is the Virtuoso full text search that uses a “magic” prefix bif without defining it in the query.

When using non-compliant extension in Apache Jena, a call to QueryExecutionFactory.sparqlService will fail. A workaround that worked for me is to use QueryEngineHTTP class as in:

  QueryEngineHTTP qExec = new QueryEngineHTTP(sparqlEndpointUrl, sparql);
  ResultSet res = qExec.execSelect();
Advertisements

Apache Jena on Google App Engine

Jena uses Http Client to query remote endpoints. When deploying on Google App Engine, you cannot use HttpClient. Here is a quick workaround that worked fine for me:


private ResultSet execSelect(String sparql, String endpoint) throws Exception{ HttpURLConnection connection = (HttpURLConnection) new URL(endpoint + "?query="+URLEncoder.encode(sparql, "UTF-8")).openConnection(); connection.setRequestProperty("Accept", "application/sparql-results+xml"); InputStream response = connection.getInputStream(); return ResultSetFactory.fromXML(response); }