How to use the java api to make a simple query

I’m using java to query Stardog and I can’t find a way to use a Getter to have the same results than the query below :

select ?s ?p ?o where{
    ?s ?p ?o.
    ?s a ex:MyClass
}

I tried the code below but, as expected, it only retrieves the triples having the predicate RDF.TYPE

connection.get()
          .object(iri(ex, "MyClass"))
          .statements();

So how can I do that in java without using a query string ?

Getter API can only execute queries equivalent to a single triple pattern. If you are not using a SPARQL query, you would need to loop over the results of the call you show and make another call for each subject to retrieve their triples. Needless to say this is very inefficient and a bad idea in general.

A better alternative is write a query template and set the parameter(s) programmatically:

connection
          .select(query)
          .parameter("type", iri(ex, "MyClass"))
          .execute();

where query looks like

select ?s ?p ?o where{
    ?s ?p ?o.
    ?s a ?type
}

Best,
Evren

Agree.

What I do is to use a

final static String QUERY_GETTER = "select ?s ?p ?o....";

and then use .parameter() to configure the values. Note that they have to be inside the WHERE clause, otherwise you need to use BIND.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.