| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751 |
- <?xml version="1.0" encoding="UTF-8"?>
- <!-- Reviewed: no -->
- <sect1 id="zend.service.windowsazure.storage.table">
- <title>Zend_Service_WindowsAzure_Storage_Table</title>
- <para>
- The Table service offers structured storage in the form of tables.
- </para>
- <para>
- Table Storage is offered by Windows Azure as a REST <acronym>API</acronym> which is wrapped
- by the <classname>Zend_Service_WindowsAzure_Storage_Table</classname> class in order to
- provide a native <acronym>PHP</acronym> interface to the storage account.
- </para>
- <para>
- This topic lists some examples of using the
- <classname>Zend_Service_WindowsAzure_Storage_Table</classname> class. Other features are
- available in the download package, as well as a detailed <acronym>API</acronym>
- documentation of those features.
- </para>
- <para>
- Note that development table storage (in the Windows Azure <acronym>SDK</acronym>) does not
- support all features provided by the <acronym>API</acronym>. Therefore, the examples listed
- on this page are to be used on Windows Azure production table storage.
- </para>
- <sect2 id="zend.service.windowsazure.storage.table.api">
- <title>Operations on tables</title>
- <para>
- This topic lists some samples of operations that can be executed on tables.
- </para>
- <sect3 id="zend.service.windowsazure.storage.table.api.create">
- <title>Creating a table</title>
- <para>
- Using the following code, a table can be created on Windows Azure production table
- storage.
- </para>
- <example id="zend.service.windowsazure.storage.table.api.create.example">
- <title>Creating a table</title>
- <programlisting language="php"><![CDATA[
- $storageClient = new Zend_Service_WindowsAzure_Storage_Table(
- 'table.core.windows.net', 'myaccount', 'myauthkey'
- );
- $result = $storageClient->createTable('testtable');
- echo 'New table name is: ' . $result->Name;
- ]]></programlisting>
- </example>
- </sect3>
- <sect3 id="zend.service.windowsazure.storage.table.api.list">
- <title>Listing all tables</title>
- <para>
- Using the following code, a list of all tables in Windows Azure production table
- storage can be queried.
- </para>
- <example id="zend.service.windowsazure.storage.table.api.list.example">
- <title>Listing all tables</title>
- <programlisting language="php"><![CDATA[
- $storageClient = new Zend_Service_WindowsAzure_Storage_Table(
- 'table.core.windows.net', 'myaccount', 'myauthkey'
- );
- $result = $storageClient->listTables();
- foreach ($result as $table) {
- echo 'Table name is: ' . $table->Name . "\r\n";
- }
- ]]></programlisting>
- </example>
- </sect3>
- </sect2>
- <sect2 id="zend.service.windowsazure.storage.table.entities">
- <title>Operations on entities</title>
- <para>
- Tables store data as collections of entities. Entities are similar to rows. An entity
- has a primary key and a set of properties. A property is a named, typed-value pair,
- similar to a column.
- </para>
- <para>
- The Table service does not enforce any schema for tables, so two entities in the same
- table may have different sets of properties. Developers may choose to enforce a schema
- on the client side. A table may contain any number of entities.
- </para>
- <para>
- <classname>Zend_Service_WindowsAzure_Storage_Table</classname> provides 2 ways of
- working with entities:
- </para>
- <itemizedlist>
- <listitem>
- <para>
- Enforced schema
- </para>
- </listitem>
- <listitem>
- <para>
- No enforced schema
- </para>
- </listitem>
- </itemizedlist>
- <para>
- All examples will make use of the following enforced schema class.
- </para>
- <example id="zend.service.windowsazure.storage.table.entities.schema">
- <title>Enforced schema used in samples</title>
- <programlisting language="php"><![CDATA[
- class SampleEntity extends Zend_Service_WindowsAzure_Storage_TableEntity
- {
- /**
- * @azure Name
- */
- public $Name;
- /**
- * @azure Age Edm.Int64
- */
- public $Age;
- /**
- * @azure Visible Edm.Boolean
- */
- public $Visible = false;
- }
- ]]></programlisting>
- </example>
- <para>
- Note that if no schema class is passed into table storage methods,
- <classname>Zend_Service_WindowsAzure_Storage_Table</classname> automatically works with
- <classname>Zend_Service_WindowsAzure_Storage_DynamicTableEntity</classname>.
- </para>
- <sect3 id="zend.service.windowsazure.storage.table.entities.enforced">
- <title>Enforced schema entities</title>
- <para>
- To enforce a schema on the client side using the
- <classname>Zend_Service_WindowsAzure_Storage_Table</classname> class, you can create
- a class which inherits
- <classname>Zend_Service_WindowsAzure_Storage_TableEntity</classname>. This class
- provides some basic functionality for the
- <classname>Zend_Service_WindowsAzure_Storage_Table</classname> class to work with a
- client-side schema.
- </para>
- <para>
- Base properties provided by
- <classname>Zend_Service_WindowsAzure_Storage_TableEntity</classname> are:
- </para>
- <itemizedlist>
- <listitem>
- <para>
- PartitionKey (exposed through <methodname>getPartitionKey()</methodname> and
- <methodname>setPartitionKey()</methodname>)
- </para>
- </listitem>
- <listitem>
- <para>
- RowKey (exposed through <methodname>getRowKey()</methodname> and
- <methodname>setRowKey()</methodname>)
- </para>
- </listitem>
- <listitem>
- <para>
- Timestamp (exposed through <methodname>getTimestamp()</methodname> and
- <methodname>setTimestamp()</methodname>)
- </para>
- </listitem>
- <listitem>
- <para>
- Etag value (exposed through <methodname>getEtag()</methodname> and
- <methodname>setEtag()</methodname>)
- </para>
- </listitem>
- </itemizedlist>
- <para>
- Here's a sample class inheriting
- <classname>Zend_Service_WindowsAzure_Storage_TableEntity</classname>:
- </para>
- <example id="zend.service.windowsazure.storage.table.entities.enforced.schema">
- <title>Sample enforced schema class</title>
- <programlisting language="php"><![CDATA[
- class SampleEntity extends Zend_Service_WindowsAzure_Storage_TableEntity
- {
- /**
- * @azure Name
- */
- public $Name;
- /**
- * @azure Age Edm.Int64
- */
- public $Age;
- /**
- * @azure Visible Edm.Boolean
- */
- public $Visible = false;
- }
- ]]></programlisting>
- </example>
- <para>
- The <classname>Zend_Service_WindowsAzure_Storage_Table</classname> class will map
- any class inherited from
- <classname>Zend_Service_WindowsAzure_Storage_TableEntity</classname> 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:
- </para>
- <example id="zend.service.windowsazure.storage.table.entities.enforced.schema-property">
- <title>Enforced property</title>
- <programlisting language="php"><![CDATA[
- /**
- * @azure <property name in Windows Azure> <optional property type>
- */
- public $<property name in PHP>;
- ]]></programlisting>
- </example>
- <para>
- Let's see how to define a propety "Age" as an integer on Windows Azure table
- storage:
- </para>
- <example
- id="zend.service.windowsazure.storage.table.entities.enforced.schema-property-sample">
- <title>Sample enforced property</title>
- <programlisting language="php"><![CDATA[
- /**
- * @azure Age Edm.Int64
- */
- public $Age;
- ]]></programlisting>
- </example>
- <para>
- 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.
- </para>
- <para>
- The following data types are supported:
- </para>
- <itemizedlist>
- <listitem>
- <para>
- <constant>Edm.Binary</constant> - An array of bytes up to 64 KB in size.
- </para>
- </listitem>
- <listitem>
- <para>
- <constant>Edm.Boolean</constant> - A boolean value.
- </para>
- </listitem>
- <listitem>
- <para>
- <constant>Edm.DateTime</constant> - 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.
- </para>
- </listitem>
- <listitem>
- <para>
- <constant>Edm.Double</constant> - A 64-bit floating point value.
- </para>
- </listitem>
- <listitem>
- <para>
- <constant>Edm.Guid</constant> - A 128-bit globally unique identifier.
- </para>
- </listitem>
- <listitem>
- <para>
- <constant>Edm.Int32</constant> - A 32-bit integer.
- </para>
- </listitem>
- <listitem>
- <para>
- <constant>Edm.Int64</constant> - A 64-bit integer.
- </para>
- </listitem>
- <listitem>
- <para>
- <constant>Edm.String</constant> - A UTF-16-encoded value. String values may
- be up to 64 KB in size.
- </para>
- </listitem>
- </itemizedlist>
- </sect3>
- <sect3 id="zend.service.windowsazure.storage.table.entities.dynamic">
- <title>No enforced schema entities (a.k.a. DynamicEntity)</title>
- <para>
- To use the <classname>Zend_Service_WindowsAzure_Storage_Table</classname> class
- without defining a schema, you can make use of the
- <classname>Zend_Service_WindowsAzure_Storage_DynamicTableEntity</classname> class.
- This class inherits
- <classname>Zend_Service_WindowsAzure_Storage_TableEntity</classname> like an
- enforced schema class does, but contains additional logic to make it dynamic and not
- bound to a schema.
- </para>
- <para>
- Base properties provided by
- <classname>Zend_Service_WindowsAzure_Storage_DynamicTableEntity</classname> are:
- </para>
- <itemizedlist>
- <listitem>
- <para>
- PartitionKey (exposed through <methodname>getPartitionKey()</methodname> and
- <methodname>setPartitionKey()</methodname>)
- </para>
- </listitem>
- <listitem>
- <para>
- RowKey (exposed through <methodname>getRowKey()</methodname> and
- <methodname>setRowKey()</methodname>)
- </para>
- </listitem>
- <listitem>
- <para>
- Timestamp (exposed through <methodname>getTimestamp()</methodname> and
- <methodname>setTimestamp()</methodname>)
- </para>
- </listitem>
- <listitem>
- <para>
- Etag value (exposed through <methodname>getEtag()</methodname> and
- <methodname>setEtag()</methodname>)
- </para>
- </listitem>
- </itemizedlist>
- <para>
- Other properties can be added on the fly. Their Windows Azure table storage type
- will be determined on-the-fly:
- </para>
- <example id="zend.service.windowsazure.storage.table.entities.dynamic.schema">
- <title>
- Dynamicaly adding properties to
- Zend_Service_WindowsAzure_Storage_DynamicTableEntity
- </title>
- <programlisting language="php"><![CDATA[
- $target = new Zend_Service_WindowsAzure_Storage_DynamicTableEntity(
- 'partition1', '000001'
- );
- $target->Name = 'Name'; // Will add property "Name" of type "Edm.String"
- $target->Age = 25; // Will add property "Age" of type "Edm.Int32"
- ]]></programlisting>
- </example>
- <para>
- Optionally, a property type can be enforced:
- </para>
- <example
- id="zend.service.windowsazure.storage.table.entities.dynamic.schema-forcedproperties">
- <title>
- Forcing property types on Zend_Service_WindowsAzure_Storage_DynamicTableEntity
- </title>
- <programlisting language="php"><![CDATA[
- $target = new Zend_Service_WindowsAzure_Storage_DynamicTableEntity(
- 'partition1', '000001'
- );
- $target->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');
- ]]></programlisting>
- </example>
- <para>
- The <classname>Zend_Service_WindowsAzure_Storage_Table</classname> class
- automatically works with
- <classname>Zend_Service_WindowsAzure_Storage_TableEntity</classname> if no specific
- class is passed into Table Storage methods.
- </para>
- </sect3>
- <sect3 id="zend.service.windowsazure.storage.table.entities.api">
- <title>Entities API examples</title>
- <sect4 id="zend.service.windowsazure.storage.table.entities.api.insert">
- <title>Inserting an entity</title>
- <para>
- Using the following code, an entity can be inserted into a table named
- "testtable". Note that the table has already been created before.
- </para>
- <example id="zend.service.windowsazure.storage.table.api.entities.insert.example">
- <title>Inserting an entity</title>
- <programlisting language="php"><![CDATA[
- $entity = new SampleEntity ('partition1', 'row1');
- $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";
- ]]></programlisting>
- </example>
- </sect4>
- <sect4 id="zend.service.windowsazure.storage.table.entities.api.retrieve-by-id">
- <title>Retrieving an entity by partition key and row key</title>
- <para>
- 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.
- </para>
- <example
- id="zend.service.windowsazure.storage.table.entities.api.retrieve-by-id.example">
- <title>Retrieving an entity by partition key and row key</title>
- <programlisting language="php"><![CDATA[
- $storageClient = new Zend_Service_WindowsAzure_Storage_Table(
- 'table.core.windows.net', 'myaccount', 'myauthkey'
- );
- $entity= $storageClient->retrieveEntityById(
- 'testtable', 'partition1', 'row1', 'SampleEntity'
- );
- ]]></programlisting>
- </example>
- </sect4>
- <sect4 id="zend.service.windowsazure.storage.table.entities.api.updating">
- <title>Updating an entity</title>
- <para>
- Using the following code, an entity can be updated. Note that the table and
- entity have already been created before.
- </para>
- <example id="zend.service.windowsazure.storage.table.api.entities.updating.example">
- <title>Updating an entity</title>
- <programlisting language="php"><![CDATA[
- $storageClient = new Zend_Service_WindowsAzure_Storage_Table(
- 'table.core.windows.net', 'myaccount', 'myauthkey'
- );
- $entity = $storageClient->retrieveEntityById(
- 'testtable', 'partition1', 'row1', 'SampleEntity'
- );
- $entity->Name = 'New name';
- $result = $storageClient->updateEntity('testtable', $entity);
- ]]></programlisting>
- </example>
- <para>
- If you want to make sure the entity has not been updated before, you can make
- sure the <acronym>Etag</acronym> 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.
- </para>
- <example
- id="zend.service.windowsazure.storage.table.entities.api.updating.example-etag">
- <title>Updating an entity (with Etag check)</title>
- <programlisting language="php"><![CDATA[
- $storageClient = new Zend_Service_WindowsAzure_Storage_Table(
- 'table.core.windows.net', 'myaccount', 'myauthkey'
- );
- $entity = $storageClient->retrieveEntityById(
- 'testtable', 'partition1', 'row1', 'SampleEntity'
- );
- $entity->Name = 'New name';
- // last parameter instructs the Etag check:
- $result = $storageClient->updateEntity('testtable', $entity, true);
- ]]></programlisting>
- </example>
- </sect4>
- <sect4 id="zend.service.windowsazure.storage.table.entities.api.delete">
- <title>Deleting an entity</title>
- <para>
- Using the following code, an entity can be deleted. Note that the table and
- entity have already been created before.
- </para>
- <example id="zend.service.windowsazure.storage.table.entities.api.delete.example">
- <title>Deleting an entity</title>
- <programlisting language="php"><![CDATA[
- $storageClient = new Zend_Service_WindowsAzure_Storage_Table(
- 'table.core.windows.net', 'myaccount', 'myauthkey'
- );
- $entity = $storageClient->retrieveEntityById(
- 'testtable', 'partition1', 'row1', 'SampleEntity'
- );
- $result = $storageClient->deleteEntity('testtable', $entity);
- ]]></programlisting>
- </example>
- </sect4>
- </sect3>
- <sect3 id="zend.service.windowsazure.storage.table.entities.querying">
- <title>Performing queries</title>
- <para>
- Queries in <classname>Zend_Service_WindowsAzure_Storage_Table</classname> table
- storage can be performed in two ways:
- </para>
- <itemizedlist>
- <listitem>
- <para>
- By manually creating a filter condition (involving learning a new query
- language)
- </para>
- </listitem>
- <listitem>
- <para>
- By using the fluent interface provided by the
- <classname>Zend_Service_WindowsAzure_Storage_Table</classname>
- </para>
- </listitem>
- </itemizedlist>
- <para>
- Using the following code, a table can be queried using a filter condition. Note
- that the table and entities have already been created before.
- </para>
- <example id="zend.service.windowsazure.storage.table.entities.querying.query-filter">
- <title>Performing queries using a filter condition</title>
- <programlisting language="php"><![CDATA[
- $storageClient = new Zend_Service_WindowsAzure_Storage_Table(
- 'table.core.windows.net', 'myaccount', 'myauthkey'
- );
- $entities = $storageClient->storageClient->retrieveEntities(
- 'testtable',
- 'Name eq \'Maarten\' and PartitionKey eq \'partition1\'',
- 'SampleEntity'
- );
- foreach ($entities as $entity) {
- echo 'Name: ' . $entity->Name . "\n";
- }
- ]]></programlisting>
- </example>
- <para>
- Using the following code, a table can be queried using a fluent interface. Note
- that the table and entities have already been created before.
- </para>
- <example id="zend.service.windowsazure.storage.table.api.entities.query-fluent">
- <title>Performing queries using a fluent interface</title>
- <programlisting language="php"><![CDATA[
- $storageClient = new Zend_Service_WindowsAzure_Storage_Table(
- 'table.core.windows.net', 'myaccount', 'myauthkey'
- );
- $entities = $storageClient->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";
- }
- ]]></programlisting>
- </example>
- </sect3>
- <sect3 id="zend.service.windowsazure.storage.table.entities.batch">
- <title>Batch operations</title>
- <para>
- 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.
- </para>
- <para>
- 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.
- </para>
- <example id="zend.service.windowsazure.storage.table.api.batch">
- <title>Executing a batch operation</title>
- <programlisting language="php"><![CDATA[
- $storageClient = new Zend_Service_WindowsAzure_Storage_Table(
- 'table.core.windows.net', 'myaccount', 'myauthkey'
- );
- // Start batch
- $batch = $storageClient->startBatch();
- // Insert entities in batch
- $entities = generateEntities();
- foreach ($entities as $entity) {
- $storageClient->insertEntity($tableName, $entity);
- }
- // Commit
- $batch->commit();
- ]]></programlisting>
- </example>
- </sect3>
- </sect2>
- <sect2 id="zend.service.windowsazure.storage.table.sessionhandler">
- <title>Table storage session handler</title>
- <para>
- When running a <acronym>PHP</acronym> application on the Windows Azure platform in a
- load-balanced mode (running 2 Web Role instances or more), it is important that
- <acronym>PHP</acronym> session data can be shared between multiple Web Role instances.
- The Windows Azure <acronym>SDK</acronym> for <acronym>PHP</acronym> provides the
- <classname>Zend_Service_WindowsAzure_SessionHandler</classname> class, which uses
- Windows Azure Table Storage as a session handler for <acronym>PHP</acronym>
- applications.
- </para>
- <para>
- To use the <classname>Zend_Service_WindowsAzure_SessionHandler</classname> session
- handler, it should be registered as the default session handler for your
- <acronym>PHP</acronym> application:
- </para>
- <example id="zend.service.windowsazure.storage.table.api.sessionhandler-register">
- <title>Registering table storage session handler</title>
- <programlisting language="php"><![CDATA[
- $storageClient = new Zend_Service_WindowsAzure_Storage_Table(
- 'table.core.windows.net', 'myaccount', 'myauthkey'
- );
- $sessionHandler = new Zend_Service_WindowsAzure_SessionHandler(
- $storageClient , 'sessionstable'
- );
- $sessionHandler->register();
- ]]></programlisting>
- </example>
- <para>
- The above classname registers the
- <classname>Zend_Service_WindowsAzure_SessionHandler</classname> session handler and will
- store sessions in a table called "sessionstable".
- </para>
- <para>
- After registration of the
- <classname>Zend_Service_WindowsAzure_SessionHandler</classname> session handler,
- sessions can be started and used in the same way as a normal <acronym>PHP</acronym>
- session:
- </para>
- <example id="zend.service.windowsazure.storage.table.api.sessionhandler-usage">
- <title>Using table storage session handler</title>
- <programlisting language="php"><![CDATA[
- $storageClient = new Zend_Service_WindowsAzure_Storage_Table(
- 'table.core.windows.net', 'myaccount', 'myauthkey'
- );
- $sessionHandler = new Zend_Service_WindowsAzure_SessionHandler(
- $storageClient , 'sessionstable'
- );
- $sessionHandler->register();
- session_start();
- if (!isset($_SESSION['firstVisit'])) {
- $_SESSION['firstVisit'] = time();
- }
- // ...
- ]]></programlisting>
- </example>
- <warning>
- <para>
- The <classname>Zend_Service_WindowsAzure_SessionHandler</classname> session handler
- should be registered before a call to <methodname>session_start()</methodname>
- is made!
- </para>
- </warning>
- </sect2>
- </sect1>
|