Applying LIMIT values in sparql argument

HI Team,

Can you tell me how can we pass "LIMIT" values as an arguments like "FILTER" in the below example ?

String sparql = "SELECT ?a ?b
WHERE {
FILTER (?a = ?a)
}
LIMIT ?limit";

String testValue = "someValue"; // some dynamic value
int limit= 100; // some dynamic value

Map<String, Object> args = new HashMap<>();
args.put( "a",testValue );
args.put( "limit",limit);

List<Map<String,String>> results = snarlTemplate.query(sparql,args , new

RowMapper<Map<String,String>>() {
@Override
public Map<String,String> mapRow(BindingSet bindingSet) {
Map<String,String> map = new HashMap<String,String>();
map.put("a", bindingSet.getValue("a").stringValue());
map.put("b", bindingSet.getValue("b").stringValue());
return map;
}
});

I appreciate for the help and support.

Hi Shijin,

Welcome to Stardog Community!

You can add a limit to your query like this:

SelectQuery aQuery = aConn.select("select * where { ?s ?p ?o }");

aQuery.limit(10); // or use whatever value you like

You can see more at our documentation or the full Java API spec.

Does this do what you need it to?

Best,
Steve

You can pass LIMIT values as arguments from code. Don't use "limit" as the value. And you cannot use a variable like "?limit" as the value either. You can, however, add something like LIMIT <> at the end where that value is placed as an integer via code.

Hi @StevePlace ,

Thanks for the suggestions.

I was looking for some solution based on SnarlTemplate.

It will be really helpful if you could help me here .

Thanks in advance.

Hi @Matthew_Petrillo ,

Hi @StevePlace ,

Thanks for the suggestions.

It will be really helpful if you could give a example with a query with 'LIMIT <>' using SnarlTemplate.

Thanks in advance.

Hello,

Steve has provided the correct answer to your question. Using the .limit(..) method of the Query API is the only recommended way to specify the limit outside of the query string. The example he linked to is complete and shows how the method should be used.

I think you're using stardog-spring and for the future it'd help if you mention it explicitly. It's open source and if you look into the query method's code, you will see that it does not support setting the limit externally. You can fork the code and add this feature.

Otherwise you'd have to inject the limit value into your query string manually before using SnarlTemplate. Stuff like this is (in general) prone to injection attacks and we do not recommend it.

Best,
Pavel

Hi @pavel ,

Many thanks for the clarification.