Zend_Service_WindowsAzure_Storage_Table
Der Table Service bietet einen strukturierten Speicher in der Form von Tabellen.
Tabellen Speicher wird von Windows Azure als REST API angeboten die von der Klasse
Zend_Service_WindowsAzure_Storage_Table umhüllt ist um ein natives
PHP Interface zum Speicher Konto zu bieten.
Dieses Thema zeigt einige Beispiele der Verwendung der Klasse
Zend_Service_WindowsAzure_Storage_Table. Andere Features sind im
Download Paket enthalten sowie in den detailierten API Dokumentationen dieser Features.
Es ist zu beachten das bei der Entwicklung der Tabellen Speicher (in der SDK von Windows
Azure) nicht alle Features unterstützt welche von dieser API angeboten werden. Deshalb sind
die Beispiele welche auf dieser Seite aufgeführt sind, dazu gedacht auf Windows Azure
Produktions Tabellen Speichern verwendet zu werden.
Operationen auf Tabellen
Dieses Thema zeigt einige Beispiele für Operationen welche auf Tabellen ausgeführt
werden können.
Erstellung einer Tabelle
Bei Verwendung des folgenden Codes, kann eine Tabelle auf dem Windows Azure
Produktions Tabellen Speicher erstellt werden.
Erstellen einer Tabelle
createTable('testtable');
echo 'Der neue Name der Tabelle ist: ' . $result->Name;
]]>
Ausgeben aller Tabellen
Bei Verwendung des folgendes Codes, kann eine Liste alle Tabellen im Windows Azure
Produktions Tabellen Speicher abgefragt werden.
Ausgeben aller Tabellen
listTables();
foreach ($result as $table) {
echo 'Der Name der Tabelle ist: ' . $table->Name . "\r\n";
}
]]>
Operationen auf Entitäten
Tabellen speichern Daten als Sammlung von Entitäten. Entitäten sind so ähnlich wie
Zeilen. Eine Entität hat einen primären Schlüssel und ein Set von Eigenschaften. Eine
Eigenschaft ist ein benanntes, Typ-Werte Paar, ähnlich einer Spalte.
Der Tabellen Service erzwingt kein Schema für Tabellen, deshalb können zwei Entitäten in
der selben Tabelle unterschiedliche Sets von Eigenschaften haben. Entwickler können
auswählen das ein Schema auf Seite des Clients erzwungen wird. Eine Tabelle kann eine
beliebige Anzahl an Entitäten enthalten.
Zend_Service_WindowsAzure_Storage_Table bietet 2 Wege um mit
Entitäten zu arbeiten:
Erzwungenes Schema
Nicht erzwungenes Schema
Alle Beispiel verwenden die folgende erwzungene Schema Klasse.
Erzwungenes Schema welches in Beispielen verwendet wird
Note that if no schema class is passed into table storage methods,
Zend_Service_WindowsAzure_Storage_Table automatically works with
Zend_Service_WindowsAzure_Storage_DynamicTableEntity.
Enforced schema entities
To enforce a schema on the client side using the
Zend_Service_WindowsAzure_Storage_Table class, you can create
a class which inherits
Zend_Service_WindowsAzure_Storage_TableEntity. This class
provides some basic functionality for the
Zend_Service_WindowsAzure_Storage_Table class to work with a
client-side schema.
Base properties provided by
Zend_Service_WindowsAzure_Storage_TableEntity are:
PartitionKey (exposed through getPartitionKey() and
setPartitionKey())
RowKey (exposed through getRowKey() and
setRowKey())
Timestamp (exposed through getTimestamp() and
setTimestamp())
Etag value (exposed through getEtag() and
setEtag())
Here's a sample class inheriting
Zend_Service_WindowsAzure_Storage_TableEntity:
Sample enforced schema class
The Zend_Service_WindowsAzure_Storage_Table class will map
any class inherited from
Zend_Service_WindowsAzure_Storage_TableEntity to Windows
Azure table storage entities with the correct data type and property name. All there
is to storing a property in Windows Azure is adding a docblock comment to a public
property or public getter/setter, in the following format:
Enforced property
*/
public $;
]]>
Let's see how to define a propety "Age" as an integer on Windows Azure table
storage:
Sample enforced property
Note that a property does not necessarily have to be named the same on Windows Azure
table storage. The Windows Azure table storage property name can be defined as well
as the type.
The following data types are supported:
Edm.Binary - An array of bytes up to 64 KB in size.
Edm.Boolean - A boolean value.
Edm.DateTime - A 64-bit value expressed as Coordinated
Universal Time (UTC). The supported DateTime range begins from 12:00
midnight, January 1, 1601 A.D. (C.E.), Coordinated Universal Time (UTC). The
range ends at December 31st, 9999.
Edm.Double - A 64-bit floating point value.
Edm.Guid - A 128-bit globally unique identifier.
Edm.Int32 - A 32-bit integer.
Edm.Int64 - A 64-bit integer.
Edm.String - A UTF-16-encoded value. String values may
be up to 64 KB in size.
No enforced schema entities (a.k.a. DynamicEntity)
To use the Zend_Service_WindowsAzure_Storage_Table class
without defining a schema, you can make use of the
Zend_Service_WindowsAzure_Storage_DynamicTableEntity class.
This class inherits Zend_Service_WindowsAzure_Storage_TableEntity
like an enforced schema class does, but contains additional logic to make it dynamic
and not bound to a schema.
Base properties provided by
Zend_Service_WindowsAzure_Storage_DynamicTableEntity are:
PartitionKey (exposed through getPartitionKey() and
setPartitionKey())
RowKey (exposed through getRowKey() and
setRowKey())
Timestamp (exposed through getTimestamp() and
setTimestamp())
Etag value (exposed through getEtag() and
setEtag())
Other properties can be added on the fly. Their Windows Azure table storage type
will be determined on-the-fly:
Dynamicaly adding properties to Zend_Service_WindowsAzure_Storage_DynamicTableEntity
Name = 'Name'; // Will add property "Name" of type "Edm.String"
$target->Age = 25; // Will add property "Age" of type "Edm.Int32"
]]>
Optionally, a property type can be enforced:
Forcing property types on Zend_Service_WindowsAzure_Storage_DynamicTableEntity
Name = 'Name'; // Will add property "Name" of type "Edm.String"
$target->Age = 25; // Will add property "Age" of type "Edm.Int32"
// Change type of property "Age" to "Edm.Int32":
$target->setAzurePropertyType('Age', 'Edm.Int64');
]]>
The Zend_Service_WindowsAzure_Storage_Table class
automatically works with
Zend_Service_WindowsAzure_Storage_TableEntity if no specific
class is passed into Table Storage methods.
Entities API examples
Inserting an entity
Using the following code, an entity can be inserted into a table named
"testtable". Note that the table has already been created before.
Inserting an entity
FullName = "Maarten";
$entity->Age = 25;
$entity->Visible = true;
$storageClient = new Zend_Service_WindowsAzure_Storage_Table(
'table.core.windows.net', 'myaccount', 'myauthkey'
);
$result = $storageClient->insertEntity('testtable', $entity);
// Check the timestamp and etag of the newly inserted entity
echo 'Timestamp: ' . $result->getTimestamp() . "\n";
echo 'Etag: ' . $result->getEtag() . "\n";
]]>
Retrieving an entity by partition key and row key
Using the following code, an entity can be retrieved by partition key and row
key. Note that the table and entity have already been created before.
Retrieving an entity by partition key and row key
retrieveEntityById(
'testtable', 'partition1', 'row1', 'SampleEntity'
);
]]>
Updating an entity
Using the following code, an entity can be updated. Note that the table and
entity have already been created before.
Updating an entity
retrieveEntityById(
'testtable', 'partition1', 'row1', 'SampleEntity'
);
$entity->Name = 'New name';
$result = $storageClient->updateEntity('testtable', $entity);
]]>
If you want to make sure the entity has not been updated before, you can make
sure the Etag of the entity is checked. If the entity already
has had an update, the update will fail to make sure you do not overwrite any
newer data.
Updating an entity (with Etag check)
retrieveEntityById(
'testtable', 'partition1', 'row1', 'SampleEntity'
);
$entity->Name = 'New name';
// last parameter instructs the Etag check:
$result = $storageClient->updateEntity('testtable', $entity, true);
]]>
Deleting an entity
Using the following code, an entity can be deleted. Note that the table and
entity have already been created before.
Deleting an entity
retrieveEntityById(
'testtable', 'partition1', 'row1', 'SampleEntity'
);
$result = $storageClient->deleteEntity('testtable', $entity);
]]>
Performing queries
Queries in Zend_Service_WindowsAzure_Storage_Table table
storage can be performed in two ways:
By manually creating a filter condition (involving learning a new query
language)
By using the fluent interface provided by the
Zend_Service_WindowsAzure_Storage_Table
Using the following code, a table can be queried using a filter condition. Note
that the table and entities have already been created before.
Performing queries using a filter condition
storageClient->retrieveEntities(
'testtable',
'Name eq \'Maarten\' and PartitionKey eq \'partition1\'',
'SampleEntity'
);
foreach ($entities as $entity) {
echo 'Name: ' . $entity->Name . "\n";
}
]]>
Using the following code, a table can be queried using a fluent interface. Note
that the table and entities have already been created before.
Performing queries using a fluent interface
storageClient->retrieveEntities(
'testtable',
$storageClient->select()
->from($tableName)
->where('Name eq ?', 'Maarten')
->andWhere('PartitionKey eq ?', 'partition1'),
'SampleEntity'
);
foreach ($entities as $entity) {
echo 'Name: ' . $entity->Name . "\n";
}
]]>
Batch operations
This topic demonstrates how to use the table entity group transaction features
provided by Windows Azure table storage. Windows Azure table storage supports batch
transactions on entities that are in the same table and belong to the same partition
group. A transaction can include at most 100 entities.
The following example uses a batch operation (transaction) to insert a set of
entities into the "testtable" table. Note that the table has already been created
before.
Executing a batch operation
startBatch();
// Insert entities in batch
$entities = generateEntities();
foreach ($entities as $entity) {
$storageClient->insertEntity($tableName, $entity);
}
// Commit
$batch->commit();
]]>
Table storage session handler
When running a PHP application on the Windows Azure platform in a load-balanced mode
(running 2 Web Role instances or more), it is important that PHP session data can be
shared between multiple Web Role instances. The Windows Azure SDK for PHP provides the
Zend_Service_WindowsAzure_SessionHandler class, which uses
Windows Azure Table Storage as a session handler for PHP applications.
To use the Zend_Service_WindowsAzure_SessionHandler session
handler, it should be registered as the default session handler for your PHP
application:
Registering table storage session handler
register();
]]>
The above classname registers the
Zend_Service_WindowsAzure_SessionHandler session handler and will
store sessions in a table called "sessionstable".
After registration of the
Zend_Service_WindowsAzure_SessionHandler session handler,
sessions can be started and used in the same way as a normal PHP session:
Using table storage session handler
register();
session_start();
if (!isset($_SESSION['firstVisit'])) {
$_SESSION['firstVisit'] = time();
}
// ...
]]>
The Zend_Service_WindowsAzure_SessionHandler session handler
should be registered before a call to session_start()
is made!