CHAPTER 1 ■ CREATE CLOUD TABLE STORAGE 3 Table 1-1. Value Types Supported by Cloud Table Entity Properties Property Type Details Binary An array of bytes up to 64 KB Bool A Boolean value DateTime A 64-bit value expressed as UTC time; range is 1/1/1600 to 12/31/9999 Double A 64-bit floating point value GUID A 128-bit globally unique identifier Int A 32-bit integer Int64 A 64-bit integer String A UTF-16-encoded value; may be up to 64 KB • PartitionKey: Every table has a special property called PartitionKey. Since the actual data of table storage is physically distributed to many storage nodes, which may cross many storage servers running in the cloud, the cloud storage system uses this key to manage the storage nodes’ distribution. • RowKey: RowKey is the second key property defined for each table storage. This is the unique ID of the entity, and an entity can be identified using the combination of the PartitionKey and RowKey in a table. • Timestamp: The Timestamp indicates the time a table is created. • Partition: The Partition is a logical set of entities defined in a table with the same PartitionKey value. • Sort Order: A single index is provided for all entities in a table. Data entities are sorted by PartitionKey and then RowKey. This makes queries specifying these keys more efficient. A Closer Look at Entities It’s worth having a closer look at entities before we go any further: • Number of properties: The maximum number of properties an entity can define is 255, including PartitionKey, RowKey, and Timestamp. • Type: PartitionKey and RowKey are of string type. • Timestamp: Timestamp is a read-only property. CHAPTER 1 ■ CREATE CLOUD TABLE STORAGE 4 • Schema: There is no schema stored in Windows Azure tables. The data storage model for properties is a name and typed value pair. A table can not have two entities with the same name, but it may have two properties with the same name because they belong to different parent entities. • Size of an entity: The size limit for an entity is 1 MB. This size is the summation of the size of the property, the property values or their types, and the two mandatory key properties, PartitionKey and RowKey. Now that we know a little about Azure’s table storage, we should look at development storage, which allows us to test our Azure applications on our local machine. Azure Development Storage The Windows Azure SDK development environment includes out-of-the-box development storage, a utility that simulates the storage services available in the cloud. The Azure SDK provides the development storage services to allow developers to create, debug, and unit test the cloud data service on a local machine before they deploy their application to production. By default, development storage relies on a SQL Server Express database, either the 2005 edition or the 2008 edition, to simulate the storage environment in the cloud. It’s possible to use the full SQL Server as we’ll see next, but to use SQL Server Express, you must have it installed. You also need to install SQL Server Management Studio to manage SQL Server Express. Development storage connects to SQL Server Express by using Windows authentication. To switch from SQL Server Express to SQL Server 2005 or 2008, you need to modify the configuration file, DevelopmentStorage.exe.config, and one line in the DevtableGen.exe.config file, as shown in Listing 1-1 and Listing 1-2. These configuration files are located in the bin directory of Windows Azure as shown in Figure 1-1. Figure 1-1. Azure SDK DevelopmentStorage.exe.config and DevtableGen.exe.config configuration files CHAPTER 1 ■ CREATE CLOUD TABLE STORAGE 5 Listing 1-1. The DevelopmentStorage.exe.config SQL Storage Service <connectionStrings> <add name="DevelopmentStorageDbConnectionString" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=DevelopmentStorageDb; Integrated Security=True" providerName="System.Data.SqlClient" /> </connectionStrings> <appSettings> <add key="ClientSettingsProvider.ServiceUri" value="" /> </appSettings> <developmentStorageConfig> <services> <service name="Blob" url="http://127.0.0.1:10000/"/> <service name="Queue" url="http://127.0.0.1:10001/"/> <service name="Table" url="http://127.0.0.1:10002/" dbServer="localhost\SQLExpress"/> </services> Replace the data source configuration with the local machine name in two places in this configuration file. Listing 1-2. The DevtableGen.exe.config for SQL Table Service <?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="DefaultSQLInstance" value=".\SQLExpress"/> </appSettings> </configuration> The first exercise in this chapter uses development storage to create cloud data storage with a simple data structure. Create Cloud Data Storage with a Simple Data Structure In the following exercise, we are going to create a data table in the local cloud development environment. The code is available in the Exercise 1-1 code bundle. 1. Create a new project from Visual Studio, using the Worker Cloud Service template from the Add New Project dialog panel as Figure 1-2 shows. The path to find the template is the Visual C# ➤ Cloud Service. Enter the name CreateDataStorage for the solution. CHAPTER 1 ■ CREATE CLOUD TABLE STORAGE 6 Figure 1-2. Visual Studio Worker Cloud Service template 2. Visual Studio will generate two projects for this solution by default. Add a reference to StorageClient.dll to the project. This assembly can be found from the bin directory where the Azure SDK was installed, for example, C:\Program Files\Windows Azure SDK\v1.0\Samples\StorageClient\Lib\ bin\Debug as Figure 1-3 shows. ■ Note You may need to load the sample project into Visual Studio after you have installed the SDK (as discussed in the Introduction) and recompile it. After installation, a ZIP file named samples.zip will be generated under the install target folder. For example, if the install folder is C:\Program Files then the full path to find this file is C:\Program Files\Windows Azure SDK\v1.0\samples.zip. This ZIP file contains a set of sample projects. Unzip this file and find the solution folder called CloudDrive. Load that solution into Visual Studio and recompile it (this requires you to run Visual Studio under a login account with Administrator privilege), and the assembly file StorageClient.dll will be generated as Figure 1-3 shows. CHAPTER 1 ■ CREATE CLOUD TABLE STORAGE 7 Figure 1-3. Azure SDK assembly StorageClient.dll location 3. Add a new C# library project, CloudData.Models, to the solution. In this project, define a very simple data entity class, Address, which must inherit from a SDK base class, Microsoft.Samples.ServiceHosting.StorageClient.TableStorageEntity. Listing 1-3 shows that this class simply has a group of attributes of address information and no method functions but the class constructors. Listing 1-3. Class Address Definition public class Address : TableStorageEntity { private State? state; public string Address1 { get; set; } public string Address2 { get; set; } public string City { get; set; } public int? State { get { return (int) state; } set { state = (State)value; } } public string Zip { get; set; } public string County { get; set; } public string Country { get; set; } public Address():this(Guid.NewGuid()) { } public Address(Guid internalID) : base(ConfigurationManager.AppSettings["PartitionKey"], internalID.ToString()) { } public Address(string address1, string address2, string city, State state, string zip, string county, string country, Guid internalID) :this(internalID) { Address1 = address1; Address2 = address2; . property. CHAPTER 1 ■ CREATE CLOUD TABLE STORAGE 4 • Schema: There is no schema stored in Windows Azure tables. The data storage model for properties is a name and typed value pair. A table. allows us to test our Azure applications on our local machine. Azure Development Storage The Windows Azure SDK development environment includes out-of-the-box development storage, a utility. Studio to manage SQL Server Express. Development storage connects to SQL Server Express by using Windows authentication. To switch from SQL Server Express to SQL Server 2005 or 2008, you need