Programming Windows Azure : Table Operations - Understanding Pagination

11/21/2010 11:35:49 AM

Now that you understand partition and row keys, there is one final query variant to consider. In several scenarios, Azure tables will return a subset of the entities that matched the query. This could happen if the number of entities matching the query exceeds 1,000, if it exceeds a query timeout set (by default) to 60 seconds, if it exceeds 4 MB combined, or for a few other reasons. You can also force Azure tables to return only a subset by asking for only a specific number of entities at a time.

In all of these cases, Azure tables will return two continuation tokens: one for the next partition and one for the next row. When you want to get the next set of results, you send these continuation tokens back to the Azure Table service. These tokens essentially tell the Table service where your query left off, and which partition/row to continue with.

For example, if you queried a hypothetical Employee table for 5 employees at a time (assuming the employees are partitioned by department), you would see something such as the following:

GET /EmployeeTable()?$top=5&NextPartitionKey=HR&NextRowKey=EmpID205

And as shown in the following code, you would get results that have the x-ms-continuation-NextPartitionKey and x-ms-continuation-NextRowKey headers, the values of which are the continuation tokens:

HTTP/1.1 200 OK
Cache-Control: no-cache
Transfer-Encoding: chunked
Content-Type: application/atom+xml;charset=utf-8
Server: Table Service Version 1.0 Microsoft-HTTPAPI/2.0
x-ms-request-id: 91e00f79-7b87-4733-ab71-aa7b71a06c58
x-ms-continuation-NextPartitionKey: Engineering
x-ms-continuation-NextRowKey: EmpID1000

When writing .NET code against tables, you don’t need to deal with continuation tokens if you don’t want to. If you enumerate through all the results of a query, the storage client library does the right magic under the covers to get and insert continuation tokens. Look for the AsTableServiceQuery extension method which deals with continuation tokens automatically for you.

However, sometimes you must do this manually. The canonical scenario is when you want page output—such as several pages in a blog, and you want “back” and “next” links. Example 1 shows code to retrieve continuation tokens. A new class, DataServiceQuery, is used here, since it gives you access to the headers of the queries you are performing.

Example 1. Retrieving continuation tokens
           // Code to get continuation tokens from executing a query
//'query' below refers to a typical query created using CreateQuery
var dsQuery = (DataServiceQuery<Contact>)query;
var res = dsQuery.Execute();

var qor = (QueryOperationResponse)res;
string partitionToken = null;
string rowToken = null;
out partitionToken);
qor.Headers.TryGetValue("x-ms-continuation-NextRowKey", out rowToken);

Once you have the continuation tokens, you must insert them into the subsequent query. Again, you use the DataServiceQuery class to do this, as shown in Example 2.

Example 2. Inserting continuation tokens
          //Code below shows how to insert continuation tokens into a query
var dsQuery = (DataServiceQuery<Contact>)query;
query = query
.AddQueryOption("NextPartitionKey", partitionToken)
.AddQueryOption("NextRowKey", rowToken);


This code assumes there will always be a continuation token for partitions and rows. In some cases, you might get just one (if you’re on the last row or the last partition). In production code, you must add checks for that.

Other -----------------
- Programming Windows Azure : Table Operations - Using Partitioning
- Programming Windows Azure : Table Operations - Querying Data
- Programming Windows Azure : Table Operations - Creating Entities
- Programming Windows Azure : Table Operations - Creating Tables
- iPad Development : Document Management (part 2)
- iPad Development : Document Management (part 1)
- iPad Development : The Split View Concept
- jQuery 1.3 : Developing plugins - Adding new shortcut methods
- jQuery 1.3 : Developing plugins - DOM traversal methods
- Using Cloud Services : Exploring Online Planning and Task Management
- Using Cloud Services : Exploring Online Scheduling Applications
- Using Cloud Services : Exploring Online Calendar Applications
- SOA with .NET and Windows Azure : Service Contracts with WCF (part 3)
- SOA with .NET and Windows Azure : Service Contracts with WCF (part 2)
- SOA with .NET and Windows Azure : Service Contracts with WCF (part 1)
- Cloud Security and Privacy : Data Security and Storage
- iPad SDK : Working with Documents - Desktop Synchronization
- Required Project Images for iPad Apps
- iPhone SDK : GameKit Voice Chat
- iPhone SDK : Creating Basic GameKit Services (part 2) : Sending and Receiving Data
- First look: Apple Watch

- 10 Amazing Tools You Should Be Using with Dropbox

- Sigma 24mm f/1.4 DG HSM Art

- Canon EF11-24mm f/4L USM

- Creative Sound Blaster Roar 2

- Alienware 17 - Dell's Alienware laptops

- Smartwatch : Wellograph

- Xiaomi Redmi 2
Popular tags
Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Indesign Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe After Effects Adobe Photoshop Adobe Fireworks Adobe Flash Catalyst Corel Painter X CorelDRAW X5 CorelDraw 10 QuarkXPress 8 windows Phone 7 windows Phone 8