Query result difference Stardog-Jena

1 view
Skip to first unread message

timmy....@gmail.com

unread,
Jan 9, 2015, 7:47:03 AM1/9/15
to sta...@clarkparsia.com
Hi,

Yet another noob question from my end. :-) I'm trying the execute the following SPARQL query:

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX
: <http://www.semanticweb.org/test#>
SELECT
(count(?s) as ?amount)
FROM
<http://name>
WHERE
{ ?s rdf:type :Datasource . };

When I use the Stardog API, the result is as expected.
Connection connection = null;
 
try {
     connection
= ConnectionConfiguration.to("name").server("snarl://localhost:5820/").connect();
     
final SelectQuery query = connection.select(queryStatement);
     
final TupleQueryResult result = query.execute();
     
return Long.valueOf(result.next().getValue("amount").stringValue());
 
} catch (final Exception e) {
     
// exception handling
 
} finally {
     connection
.close();
 
}

But when I use the Jena API, an exception is thrown.

Dataset dataset = null;
try {
   
final Connection connection = ConnectionConfiguration.to("name").server("snarl://localhost:5820/").connect();
    dataset
= SDJenaFactory.createDataset(connection);
    dataset.begin(ReadWrite.READ);
   
final Model model = dataset.getNamedModel("name");
   
final Query query = QueryFactory.create("the_query_described_above");
   
final QueryExecution queryExecution = QueryExecutionFactory.create(query, model);
   
final ResultSet result = queryExecution.execSelect();
   
final Long amount = result.next().getLiteral("amount").getLong();
   
return amount;
} catch (Exception e) {
   
// exception handling
} finally {
    dataset
.close();
}

java.util.NoSuchElementException: StardogQueryEngine$QIteratorImpl
 at com
.hp.hpl.jena.sparql.engine.iterator.QueryIteratorBase.nextBinding(QueryIteratorBase.java:152)
 at com
.hp.hpl.jena.sparql.engine.iterator.QueryIteratorBase.next(QueryIteratorBase.java:129)
 at com
.hp.hpl.jena.sparql.engine.iterator.QueryIteratorBase.next(QueryIteratorBase.java:41)
 at com
.hp.hpl.jena.sparql.engine.ResultSetStream.nextBinding(ResultSetStream.java:87)
 at com
.hp.hpl.jena.sparql.engine.ResultSetStream.nextSolution(ResultSetStream.java:115)
 at com
.hp.hpl.jena.sparql.engine.ResultSetStream.next(ResultSetStream.java:124)

I'd like to keep using the Jena API, but I don't see what could be wrong here. Thanks.

Evren Sirin

unread,
Jan 9, 2015, 12:25:35 PM1/9/15
to Stardog
You are getting the exception with Jena because that query is not
returning any results. You should run the query against the dataset
directly instead of getting the named model so just do

final Query query = QueryExecutionFactory.create(query, dataset);

This should return the same results as in the first case.

Best,
Evren
> --
> -- --
> You received this message because you are subscribed to the C&P "Stardog"
> group.
> To post to this group, send email to sta...@clarkparsia.com
> To unsubscribe from this group, send email to
> stardog+u...@clarkparsia.com
> For more options, visit this group at
> http://groups.google.com/a/clarkparsia.com/group/stardog?hl=en
Reply all
Reply to author
Forward
0 new messages