Accessing Windows Azure Table Data as OData via PHP

Did you know that data stored in Windows Azure Table storage can be accessed through an OData feed? Does that question even make sense to you? If you answered no to either of those questions and you are interested in learning more, then read on. In this post I’ll show you how to use the OData SDK for PHP to retrieve, insert, update, and delete data stored in Windows Azure Table storage. If you are new to either Windows Azure Table storage or the OData protocol, I suggest reading either (or both) of these posts (among other things, they will describe what Azure Table storage and OData are, and walk you through set up of Azure Table storage and the OData SDK for PHP):

Once you have set up Azure Table storage and installed the OData SDK for PHP, writing code to leverage Azure Table storage is easy…

 

Set Up

To use the OData SDK for PHP with Azure tables, we must define a class that describes the table structure. And, for the SDK to extract table metadata from the class, the class must…

  1. Inherit from the TableEntry class
  2. All the properties which is to be stored in the azure table must have the attribute @Type:EntityProperty
  3. The table entry must have the following attributes:
    1. @Class:name_of_table_Entry_class
    2. @Key:PartitionKey
    3. @Key:RowKey

So, the class I’m using (in a file called Contacts.php) describes a contact (note that the name of the class must be the name of the table in Windows Azure):

**
*@Class:Contacts
*@Key:PartitionKey
*@Key:RowKey
*/
class Contacts extends TableEntry
{
    /**
     *@Type:EntityProperty
     */
    public $Name;
    /**
     *@Type:EntityProperty
     */
    public $Address;
    /**
     *@Type:EntityProperty
     */
    public $Phone;
}

I’ll use "business” or “personal” as the value for the partition key, and an e-mail address for the value of the row key (the partition key and row key together define a unique key for each table entry). With that class in place we are ready to start using the SDK. In the code below, I create a new ObjectContext (which manages in-memory table data and provides functionality for persisting the data) and set the Credential property:

require_once 'Context/ObjectContext.php';
require_once 'Contacts.php';
define("AZURE_SERVICE_URL",'
http://your_azure_account_name.table.core.windows.net');
define("AZURE_ACCOUNT_NAME", "your_azure_account_name");
define("AZURE_ACCOUNT_KEY", 'your_azure_account_key');

$svc = new ObjectContext(AZURE_SERVICE_URL);
$svc->Credential = new AzureTableCredential(AZURE_ACCOUNT_NAME, AZURE_ACCOUNT_KEY);

 

Creating a Table

To create a table, we use the Tables class that is included in the SDK. Note that the table name must be the same as my class above:

$table = new Tables();
$table->TableName = "Contacts";   
$svc->AddObject("Tables", $table);
$svc->SaveChanges();

 

Inserting an Entity

Inserting an entity simply means creating a new Contacts object, setting it’s properties, and using the AddObject and SaveChanges method on the ObjectContext class:

$tableEntity = new Contacts();
$tableEntity->PartitionKey = "business";
$tableEntity->RowKey = "Brian@address.com";
$tableEntity->Name = "Brian Swan";
$tableEntity->Address = "One Microsoft Way, Redmond WA";
$tableEntity->Phone = "555-555-5555";
$svc->AddObject("Contacts", $tableEntity);
$svc->SaveChanges();

 

Retrieving Entities

The following code executes a query that returns all entities in the Contacts table. Note that all contacts come back as instances of the Contacts class.

$query = new DataServiceQuery("Contacts", $svc);
$response = $query->Execute();

foreach($response->Result as $result)
{
    //each result is a Contacts object!
    echo $result->Name."<br/>";
    echo $result->Address."<br/>";
    echo $result->Phone."<br/>";
    echo $result->PartitionKey."<br/>";
    echo $result->RowKey."<br/>";
    echo "----------------<br/>";
}

You can filter entities by using the Filter method on the DataServiceQuery object. For example, by replacing  $response = $query->Execute(); with this code…

$response = $query->Filter("PartitionKey eq 'business'")->Execute();

…you retrieve all “business” contacts. Or, this code will return all entities whose Name property matches the given string:

$response = $query->Filter("Name eq 'Brian Swan'")->Execute();

I’ve only demonstrated the equals (eq) condition in filters here. The SDK supports these conditions too: ne (not equal), lt (less than), le (less than or equal), gt (greater than), and ge (greater than or equal).

 

Updating an Entity

To update an entity, we have to first retrieve the entity to be updated. Once the entity is in memory, we can update its non-key properties. The Partition Key and Row Key cannot be updated. To achieve an update of a key property, an entity must be deleted and re-created with the correct keys. The following code updates the Address property of an entity:

$query = new DataServiceQuery("Contacts", $svc);
$filter = "(PartitionKey eq 'business') and (RowKey eq 'Brian@address.com')";
$response = $query->Filter($filter)->Execute();

$tableEntity = $response->Result[0];
$tableEntity->Phone = "999-999-9999";
$svc->UpdateObject($tableEntity);
$svc->SaveChanges();

 

Deleting an Entity

To delete an entity, we have to retrieve the entity to be deleted, mark it for deletion, then persist the deletion:

$query = new DataServiceQuery("Contacts", $svc);
$filter = "(PartitionKey eq 'business') and (RowKey eq 'Brian@address.com')";
$response = $query->Filter($filter)->Execute();

$tableEntity = $response->Result[0];
$svc->DeleteObject($tableEntity);
$svc->SaveChanges();

 

Deleting a Table

And finally, to delete a table…

$query = new DataServiceQuery('Tables', $svc);
$response = $query->Filter("TableName eq 'Contacts'")->Execute();
$table= $response->Result[0];   
$svc->DeleteObject($table);
$svc->SaveChanges();

 

That’s it. You want to play with the code above, you can download the files attached to this post. Let me know if this information is interesting and/or useful.

Thanks.

-Brian

Share this on Twitter

No Comments