1. Retrieving all entities in a table using the REST API
In this section we’ll look at
how to build a small console application that will display all the
entities in the Product table. This application is similar to the one
that you built earlier to list tables in storage accounts.
The base URI used to query the
Products table is the same base URI you used to insert, update, and
delete table entities. To return all shirts stored in the Products table
in the development storage account, you would make an HTTP GET request using the following URI:
http://127.0.0.1:10002/devstoreaccount1/Products
The code in listing 1 will return all entities in the Products table and display the result in the console window.
Listing 1. Listing the entities in the Products table using the REST API
The code in listing 1 is pretty much the same code as in this listing
(which listed all the tables in a storage account). The only
modification you need to make to that code is to change the URI for the
HTTP request at to the URI of the Products table.
The
REST API call that you used to list all product entities adheres to the
AtomPub protocol, so the result that’s returned to the console window
will display the entities from the Products table in Atom XML format, as
shown in the following listing.
Listing 2. Atom XML output from the console application in listing 1
<feed xml:base="http://127.0.0.1:10002/devstoreaccount1/" xmlns:d="http:// schemas .microsoft.com/ado/2007/08/dataservices" xmlns:m="http:// schemas.microsoft.com/ ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom"> <title type="text">Products</title> <id>http://127.0.0.1:10002/devstoreaccount1/Products</id> <updated>2009-08-01T11:23:48Z</updated> <link rel="self" title="Products" href="Products" /> <entry m:etag="W/"datetime'2009-07-23T19%3A55%3A38.7'""> <id>http://127.0.0.1:10002/devstoreaccount/Products(PartitionKey='Shirts',RowKey='BlueShirt')</id> <title type="text"></title> <updated>2009-08-01T11:23:48Z</updated> <author> <name /> </author> <link rel="edit" title="Products" href="Product(PartitionKey='Shirts',RowKey='BlueShirt')" /> <category term="AiAChapter7Web_WebRole.Products" scheme="http://schemas. microsoft.com/ado/2007/08/dataservices/scheme" /> <content type="application/xml"> <m:properties> <d:Name>Blue Shirt</d:Name> <d:Description>A Blue Shirt</d:Description> <d:Timestamp m:type="Edm.DateTime">2009-07-23T19:55:38.7</d:Timestamp> <d:PartitionKey>Shirts</d:PartitionKey> <d:RowKey>BlueShirt</d:RowKey> </m:properties> </content> </entry> <entry m:etag="W/"datetime'2009-07-23T19%3A13%3A28.09'""> <id>http://127.0.0.1:10002/devstoreaccount1/Products(PartitionKey='Shirts',RowKey='RedShirt') </id> <title type="text"></title> <updated>2009-08-01T11:23:48Z</updated> <author> <name /> </author> <link rel="edit" title="Products" href="Product(PartitionKey='Shirts',RowKey='RedShirt')" /> <category term="AiAChapter7Web_WebRole.Products" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /> <content type="application/xml"> <m:properties> <d:Name>red shirt</d:Name> <d:Description>a red shirt</d:Description> <d:Timestamp m:type="Edm.DateTime">2009-07-23T19:13:28.09</d:Timestamp> <d:PartitionKey>Shirts</d:PartitionKey> <d:RowKey>RedShirt</d:RowKey> </m:properties> </content> </entry> </feed>
|
The Atom XML output in listing 2 returns all entities stored in the Products table (RedShirt and BlueShirt).
As you can see, the returned XML is very descriptive (and also
verbose), including the name of the property, the value, and the data
type.
The verbosity of the
returned XML means that the returned datasets are usually pretty large.
You should be careful to cache data whenever possible and return the
minimum amount of data required.
Hopefully, in the future,
the Windows Azure Table service team will support a less verbose
serialization format, such as JSON, and will also support local data
shaping (explained later on in this section). JSON would be an ideal
format to support because WCF Data Services (but not the Table service
implementation) already supports this method of serialization.
Using JSON would require few
changes to your application code, but you’d gain large benefits in terms
of reduced bandwidth. The following code shows how the previously
returned Atom XML could be represented in JSON:
Products:
[
{
"Name" : "blue shirt"
"Description" : "a Blue Shirt"
"Timestamp" : "2009-07-23T19:13:28.09"
"PartitionKey" : "Shirts"
"RowKey" : "BlueShirt"}
},
{
"Name" : "red shirt"
"Description" : "A Blue Shirt"
"Timestamp" : "2009-07-23T19:13:28.09"
"PartitionKey" : "Shirts"
"RowKey" : "RedShirt"}
},
]
As you can see, the JSON
representation is much more readable and terse, meaning that the size of
the returned documents would be greatly reduced and will therefore
improve the speed of your application. Hopefully this will be supported
in future versions of the Table service.