Top Banner
Windows Azure Storage Table Storage
24

Table Storage. Access Table storage Add the following namespace declarations to the top of any C# file in which you wish to programmatically access Windows.

Jan 01, 2016

Download

Documents

Audra Adams
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Table Storage. Access Table storage Add the following namespace declarations to the top of any C# file in which you wish to programmatically access Windows.

Windows Azure Storage Table Storage

Page 2: Table Storage. Access Table storage Add the following namespace declarations to the top of any C# file in which you wish to programmatically access Windows.

Access Table storageAdd the following namespace declarations to the

top of any C# file in which you wish to programmatically access Windows Azure Storage:

using Microsoft.WindowsAzure;using Microsoft.WindowsAzure.Storage;using Microsoft.WindowsAzure.Storage.Auth;using Microsoft.WindowsAzure.Storage.Table;

Microsoft.WindowsAzure.Storage.dll assembly is to be added.

Page 3: Table Storage. Access Table storage Add the following namespace declarations to the top of any C# file in which you wish to programmatically access Windows.

Access Table storageRetrieving your connection stringuse the CloudConfigurationManager type to

retrieve your storage connection string and storage account information from the Windows Azure service configuration:

CloudStorageAccount storageAccount = CloudStorageAccount.Parse( CloudConfigurationManager.GetSetting("StorageConnectionString"));

Page 4: Table Storage. Access Table storage Add the following namespace declarations to the top of any C# file in which you wish to programmatically access Windows.

Access Table storageA CloudTableClient type allows you to

retrieve objects that represent tables stored within the Table Storage Service.

The following code creates a CloudTableClient object using the storage account object we retrieved above:

CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

Page 5: Table Storage. Access Table storage Add the following namespace declarations to the top of any C# file in which you wish to programmatically access Windows.

Access Table storageCreate a Tableuse a CloudTableClient object to get a

reference to the Table you want to use. You can create the Table if it doesn't exist:

Page 6: Table Storage. Access Table storage Add the following namespace declarations to the top of any C# file in which you wish to programmatically access Windows.

Access Table storageCreate a Table...// Retrieve a reference to a Table.CloudTable table=tableClient.GetTableReference(“contacts");

// Create the table if it doesn‘t alreadyexist.

table.CreateIfNotExists();

Page 7: Table Storage. Access Table storage Add the following namespace declarations to the top of any C# file in which you wish to programmatically access Windows.

Access Table storageEntities map to C# objects using a custom

class derived from TableEntity. To add an entity to a table, create a class that

defines the properties of the entity. For any property that should be stored in the

table service, the property must be a public property of a supported type that exposes both get and set.

Also, the entity type must expose a parameter-less constructor.

Page 8: Table Storage. Access Table storage Add the following namespace declarations to the top of any C# file in which you wish to programmatically access Windows.

Access Table storageEntities with the same partition key can be

queried faster than those with different partition keys, but using diverse partition keys allows for greater parallel operation scalability.

The following code defines an entity class that uses the contact type as the row key and phone number as the partition key.

Together, an entity's partition and row key uniquely identify the entity in the table.

Page 9: Table Storage. Access Table storage Add the following namespace declarations to the top of any C# file in which you wish to programmatically access Windows.

Access Table storagepublic class ContactEntity : TableEntity { public ContactEntity(string contactType string lastName, string firstName,int contactNo) {

this.PartitionKey = contactTYpe; this.RowKey = contactNo; this.firstName=firstName;this.lastName=lastName;

} public ContactEntity() { } public string lastName{get;set;}public string firstName{get;set;}

}

Page 10: Table Storage. Access Table storage Add the following namespace declarations to the top of any C# file in which you wish to programmatically access Windows.

Access Table storageTo create an entity// Create a new contact entity. ContactEntity contact1 = new ContactEntity(“friends”,"Harp“,"Walter“, 4255550101);

Page 11: Table Storage. Access Table storage Add the following namespace declarations to the top of any C# file in which you wish to programmatically access Windows.

Access Table storageTo create an entity// Create the TableOperation that inserts the contact entity.

TableOperation insertOperation = TableOperation.Insert(contact1);

// Execute the insert operation. table.Execute(insertOperation);

Page 12: Table Storage. Access Table storage Add the following namespace declarations to the top of any C# file in which you wish to programmatically access Windows.

Access Table storageTo retrieve an entity from a table use a a TableQuery object.

TableQuery<ContactEntity> query = new TableQuery<ContactEntity>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, “friends"));

Page 13: Table Storage. Access Table storage Add the following namespace declarations to the top of any C# file in which you wish to programmatically access Windows.

Access Table storageTo retrieve an entity from a table use a a

TableQuery object.

foreach (ContactEntity entity in table.ExecuteQuery(query)) {

Console.WriteLine("{0}, {1}\t{2}\t{3}", entity.PartitionKey, entity.RowKey, entity.firstName, entity.lastName);

}

Page 14: Table Storage. Access Table storage Add the following namespace declarations to the top of any C# file in which you wish to programmatically access Windows.

Access Table storageReplace an entity

To update an entity, retrieve it from the table service, modify the entity object, and then save the changes back to the table service.

The following code changes an existing contact's phone number. Instead of calling Insert, this code uses Replace.

This causes the entity to be fully replaced on the server, unless the entity on the server has changed since it was retrieved, in which case the operation will fail.

Page 15: Table Storage. Access Table storage Add the following namespace declarations to the top of any C# file in which you wish to programmatically access Windows.

Access Table storageReplace an entity

This failure is to prevent your application from inadvertently overwriting a change made between the retrieval and update by another component of your application.

The proper handling of this failure is to retrieve the entity again, make your changes (if still valid), and then perform another Replace operation.

Page 16: Table Storage. Access Table storage Add the following namespace declarations to the top of any C# file in which you wish to programmatically access Windows.

Access Table storageReplace an entity// Create a retrieve operation that takes a customer entity.

TableOperation retrieveOperation = TableOperation.Retrieve<ContactEntity>(“<partionkey>", “<rowkey>");

// Execute the operation. TableResult retrievedResult = table.Execute(retrieveOperation);

Page 17: Table Storage. Access Table storage Add the following namespace declarations to the top of any C# file in which you wish to programmatically access Windows.

Access Table storageReplace an entity// Assign the result to a CustomerEntity object.

ContactEntity updateEntity = (ContactEntity)retrievedResult.Result;

Page 18: Table Storage. Access Table storage Add the following namespace declarations to the top of any C# file in which you wish to programmatically access Windows.

Access Table storageReplace an entityif (updateEntity != null) { // Change the phone number. updateEntity.RowKey = 4255550105;

// Create the Replace TableOperation TableOperation updateOperation = TableOperation.Replace(updateEntity);

// Execute the operation. table.Execute(updateOperation); Console.WriteLine("Entity updated.");

} else Console.WriteLine("Entity could not be retrieved.");

Page 19: Table Storage. Access Table storage Add the following namespace declarations to the top of any C# file in which you wish to programmatically access Windows.

Access Table storageInsert-or-replace an entity

Replace operations will fail if the entity has been changed since it was retrieved from the server.

Furthermore, you must retrieve the entity from the server first in order for the Replace to be successful.

Sometimes, however, you don't know if the entity exists on the server and the current values stored in it are irrelevant - your update should overwrite them all.

Page 20: Table Storage. Access Table storage Add the following namespace declarations to the top of any C# file in which you wish to programmatically access Windows.

Access Table storageInsert-or-replace an entity

To accomplish this, you would use an InsertOrReplace operation.

This operation inserts the entity if it doesn't exist, or replaces it if it does, regardless of when the last update was made.

In the following code example, the contact entity for Ben Smith is still retrieved, but it is then saved back to the server using InsertOrReplace.

Any updates made to the entity between the retrieval and update operation will be overwritten.

Page 21: Table Storage. Access Table storage Add the following namespace declarations to the top of any C# file in which you wish to programmatically access Windows.

Access Table storageReplace an entity

TableOperation insertOrReplaceOperation = TableOperation.InsertOrReplace(updateEntity);

// Execute the operation. table.Execute(updateOperation);

Page 22: Table Storage. Access Table storage Add the following namespace declarations to the top of any C# file in which you wish to programmatically access Windows.

Access Table storageDelete an entity

You can easily delete an entity after you have retrieved it, using the same pattern shown for updating an entity.

The following code retrieves and deletes a customer entity.

Page 23: Table Storage. Access Table storage Add the following namespace declarations to the top of any C# file in which you wish to programmatically access Windows.

Access Table storageDelete an entity

// Create the Delete TableOperation. TableOperation deleteOperation = TableOperation.Delete(deleteEntity);

// Execute the operation. table.Execute(deleteOperation);

Page 24: Table Storage. Access Table storage Add the following namespace declarations to the top of any C# file in which you wish to programmatically access Windows.

Access Table storageDelete a table

Finally, the following code example deletes a table from a storage account.

A table which has been deleted will be unavailable to be recreated for a period of time following the deletion.

// Delete the table it if exists. table.DeleteIfExists();