Friday, June 3, 2011

Two ways to Query Linked Data with ARQ

ARQ provides two ways to query Linked Data, i.e. remote SPARQL Endpoints.  The first one us to use the "sparqlService" method on the QueryExecutionFactory. 

E.g.: 

QueryExecution qe = QueryExecutionFactory.sparqlService("http://dbpedia.org/sparql", query);

The other way can be done with a QueryExecutionFactory that is also attached to a Jena Model, however you specifcy the "service" in the SPARQL directly.

E.g.:

QueryExecution qe =  QueryExecutionFactory.create(query,model);

Sparql:
SELECT ?s ?p WHERE {
    SERVICE <http://dbpedia.org/sparql> {
        ?s ?p <http://en.wikipedia.org/wiki/Sparql>
    }
 }

Enjoy traversing the linked data!

2 comments:

  1. There is a slight difference between the two: the first lets you do arbitrary SPARQL and the second only basic graph patterns are sent to the end point ( no solution modifiers DISTINCT, ORDER BY, LIMIT etc. ; although these can be done on the outer query locally ).

    ReplyDelete
  2. Hi Tim - thanks, I omitted those details. The local/remote processing dichotomy is definitely something that makes the transition of SQL developers to SPARQL developers more challenging. For Groovy SPARQL, I wound up adding the support to initialize off of a Jena Model for this very reason.

    The Groovy SPARQL also uses Syntax.syntaxARQ to get things like COUNT and other Jena specifics that might not be widely adopted - but are definitely useful for those working with a fully compatible Jena triple store.

    ReplyDelete