Creating tables and managing CRUD operations in Windows Azure…
Windows Azure Table Service provides structured storage, but tables are not relational database tables as they are in SQL Server for instance. Windows Azure Table Service instead follows a simple and flexible model of entities and properties.
The table service was specially designed for massive scalability and availability, supporting billions of entities. In this post I am going to share the steps to create and manage table operations in Windows Azure.
The following diagram shows the Table service architecture:
- Storage Account: Encompasses the blob, queue and table services. Defines the URI scheme for accessing the services. For example the URI for accessing the table service is http|https://<account name>.table.core.windows.net
- Tables: are the containers for storing data. Data is stored as a collection of entities. There can be any number of tables in an account in the table service.
- Entities: stored in tables, they are pretty similar to rows in a relational database.
- Properties: entities consist of a set of name-value pairs called properties (analogous to columns).
- Properties available data types
- Binary -> byte
- Boolean -> bool
- DateTime -> DateTime
- Double ->Double
- Guid -> Guid
- Int32 -> int or Int32
- Int64 -> long or Int64
- Stirng –> string
Where to start?
The following are the three steps required to create a table:
1. Define the schema for the table (Entity)
2. Define the context, the actions that we will do with the table
3. Create the table
1. Defining an Entity
Before we can store an entity we must define it. In order to do this we have to create a class, which inherits from TableServiceEntity. The TableServiceEntity defines a PartitionKey, RowKey and Timestamp properties, which we will inherit. The first two attributes should be initialized in the creation time, while the timestamp is assigned automatically.
2. Creating a Context
Once we have created the entity then we should create the Context to manage the table [CRUD operations], creating a class that inherits from TableServiceContext, which is included in the Microsoft.WindowsAzure.StorageClient assembly. Here we create properties to expose collections of each entity. (Our table(s))
The following could be an example:
3. Creating the table
The last step is creating the table itself; we can create it locally or in the cloud. Additionally, we can do it from any application. In the example, I am doing it directly within a WebRole.cs class that is generated when a WebRole project is created.
Now, we can use the context to manage the table. The following are examples of how to use the context:
· Initialize the context:
CloudStorageAccount _account = CloudStorageAccount.DevelopmentStorageAccount;
PersonTableServiceContext _personServiceContext =
new PersonTableServiceContext(_account.TableEndpoint.ToString(), _account.Credentials);
· Get all:
· Add a record:
Person person = new Person();
· Remove a record:
· Update record data
Hope you find this post useful as well as try Windows Azure Table Service in your cloud apps.