Is there any way to have jena return paginated results? I want to display a table with first 5 results and then user clicks next, displays next 5 results.

Is this possible?

asked 12 May '11, 06:38

dan%20leadgy's gravatar image

dan leadgy
accept rate: 0%

Outside of jena you can do this in sparql directly using OFFSET and LIMIT on the query. This approach is the best when you do not have logged in users on webserver.

e.g. OFFSET 0 LIMIT 5 gives the first page. OFFSET 5 and LIMIT 5 gives the second page.

If you have desktop application or logged in users you might be able to keep a reference to the query result. And then you can use ResultSetFormatter.toList() to get a list and paginate on that.

permanent link

answered 12 May '11, 08:40

Jerven's gravatar image

Jerven ♦
accept rate: 35%

edited 12 May '11, 08:40


It's important to note that the default ordering of results in SPARQL is not well-defined, so OFFSET/LIMIT are recommended for use only in conjunction with ORDER BY to give a deterministic ordering.

Otherwise, it would be perfectly valid (although unlikely) for a SPARQL engine to return the same five results every time.

(12 May '11, 09:25) Signified ♦ Signified's gravatar image

@Signified Oh thats sneaky. I did not expect that behaviour. Is there a way to add an default ORDER BY to give any kind of deterministic ordering? Adding ORDER BY * does not work with sesame based stores as far as I see.

(12 May '11, 09:37) Jerven ♦ Jerven's gravatar image

Good question... It's possible to have ORDER BY's which do not contain a variable, like:


...but AFAICS these have the same effect as no ORDER BY. I think that to define a consistent ordering for the results, you'll need to specify an explicit variable.

Although ORDER BY * looks neat in practice, the problem would then be how you order the variables... e.g., would it be ordered ?s then ?p then ?o or ?o then ?p then ?s etc. Order of appearance is one possibility, but this would not play well with current implementations.

(12 May '11, 11:14) Signified ♦ Signified's gravatar image

I'm really impressed for how many details you've covered! Thank you! It's really useful!

(12 May '11, 11:36) dan leadgy dan%20leadgy's gravatar image

@Signified wrote the SPARQL workgroup an e-mail. Hope smarter people than me can write a good specification.

(12 May '11, 11:45) Jerven ♦ Jerven's gravatar image

If you don't use * in the query you can define a default ordering to be the first variable in the result, e.g. SELECT ?s ?p ?o WHERE { ?s ?p ?o} ORDER BY 1. So ORDER BY 1 could be appended to any query blindly so long as it is not a SELECT * query.

(12 May '11, 13:29) harschware ♦ harschware's gravatar image

@harschware, are you sure that ORDER BY 1 is not specific to Virtuoso? Following the grammar for SPARQL 1.1, I can't seem to find that production. Also, such queries do not validate at (The only mentions I can find through Google are Virtuoso specific

(12 May '11, 14:07) Signified ♦ Signified's gravatar image

What's wrong with just picking a variable you fancy and adding that to your order clause? If you care about the order (which you obviously do when paginating), specify it. If not, don't. Why would it be harder to write "ORDER BY ?x" than "ORDER BY *"?

(12 May '11, 17:21) Jeen Broekstra ♦ Jeen%20Broekstra's gravatar image

For any one commenting on the ORDER BY requirement. I mailed the quite a few of you are active on that list. I think its better to either make this a question or discuss it at W3C where something gets done with it.

(13 May '11, 03:11) Jerven ♦ Jerven's gravatar image

@harschware I think signified is right, according to the grammar ORDER BY 1 is not valid though in theory you can write ORDER BY (1) instead which is valid but I'm not sure it'll have the desired effect

(13 May '11, 07:16) Rob Vesse ♦ Rob%20Vesse's gravatar image
showing 5 of 10 show 5 more comments

While there is nothing in SPARQL 1.1 to support this directly, there are two ways to consider.

  1. Client-side caching. The results from a query can be streamed (check your implementation). The pagination, to a large extent, can be done client side e.g reading in 100 answers and showing 5. if something outside the cache is asked for, a suitable LIMIT/OFFSET/ORDER BY can be done to fill the cache.
  2. The engine may efficiently handle the repeated use of LIMIT/OFFSET/ORDER BY to avoid re-executing the query (esp. as ORDER BY can be expensive).
permanent link

answered 12 May '11, 12:56

AndyS's gravatar image

AndyS ♦
accept rate: 33%


e.g. for #2: In the case of Oracle 11G both client and server side result caches can be setup. The client side cache is a feature of the database driver and handled auto-magically, cache policies of the clients can be configured at the server.

(12 May '11, 13:40) harschware ♦ harschware's gravatar image

If your SPARQL endpoint supports SPARQL 1.1 then you might want to ascertain the total number of results using COUNT (for presentation and application purposes)

permanent link

answered 12 May '11, 09:00

William%20Greenly's gravatar image

William Greenly
accept rate: 13%

Except that calls will not be read consistent between successive calls so that max count may go up or down. Better to avoid this I think.

(12 May '11, 13:31) harschware ♦ harschware's gravatar image

Sure, but this is true of any type of pagination. Equally 'next 5 results' might not exist by the time the user comes to click on them. So one solution is to handle this type of exception. Additionally, we don't know how regularly the data is subjected to change.

(13 May '11, 07:08) William Greenly William%20Greenly's gravatar image
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:


question asked: 12 May '11, 06:38

question was seen: 3,927 times

last updated: 13 May '11, 07:16