Azure Basic - Windows Storage Enhancement (Table)

司徒茂实
2023-12-01

Table 

  1. Entity Group Transactions (SaveChangesOptions.Batch) (In Sample1)
Here’s a more complete description from the “Programming Table Storage” whitepaper:"

For the entities storedwithin the same table and same partition (i.e., they have the same partition key value), the application can atomically perform a transaction involving those entities.This allows the application to atomically perform multiple Create/Update/Delete operations across multiple entities in a single batch request to the storage system, as long as all the entities have the same partition key value and are in the same table. Either all the entity operations succeed in the single transaction or they all fail, andsnapshot isolationis provided for the execution of the transaction. In addition, all other queries executing in parallel at the same time will not see the result of the transaction, since they will be working off a prior snapshot. Queries will only see the result of the transaction, once it has fully successfully committed.

Entity Group Transaction will require the use of the version header with the version set to "2009-04-14" or later

  1. Together the PartitionKey and RowKey uniquely identify every entity within a table

AsTableServiceQuery() vs. ToList()

The AsTableServiceQuery method converts the standard IQueryable result to a CloudTableQuery result. Using a CloudTableQuery object offers the following benefits to the application:

• Data can be retrieved from the table in multiple segments instead of getting it all in one go. This is useful when dealing with a large set of data.
• You can specify a retry policy for cases when the query fails.

Sample 1:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
using System.Threading;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string accountName = "devstoreaccount1";
            string key = "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==";
           
            StorageCredentialsAccountAndKey credentials =
    new StorageCredentialsAccountAndKey(accountName, key);

            string baseUri = string.Format("http://127.0.0.1:10002/{0}", accountName);
            CloudTableClient tableClient = new CloudTableClient(baseUri, credentials);
            
            // Create Movie Table
            string tableName = "Movies";
            tableClient.DeleteTable(tableName);
            tableClient.CreateTableIfNotExist(tableName);

            TableServiceContext context = tableClient.GetDataServiceContext();

            // Add movie
            context.AddObject(tableName, new Movie("Action", "1White Water Rapids Survival"));
            context.AddObject(tableName, new Movie("Action", "2White Water Rapids Survival"));
            context.AddObject(tableName, new Movie("Action", "3White Water Rapids Survival"));
            context.AddObject(tableName, new Movie("Action", "4White Water Rapids Survival"));
            context.AddObject(tableName, new Movie("Action", "5White Water Rapids Survival"));
            context.SaveChangesWithRetries();

            // Query movie, Update movie + Batch process
            var q = (from movie in context.CreateQuery<Movie>(tableName) 
                 where movie.PartitionKey == "Action" && movie.Rating > 4.0
                 select movie).AsTableServiceQuery<Movie>();

            foreach (Movie movieToUpdate in q)
            {
                movieToUpdate.Favorite = true;
                context.UpdateObject(movieToUpdate);
            }
             context.SaveChangesWithRetries(System.Data.Services.Client.SaveChangesOptions.Batch);

             var result = (from movie in context.CreateQuery<Movie>(tableName)
                          select movie).FirstOrDefault<Movie>();
             Console.WriteLine(String.Format(
                 "result.PartitionKey:{0}\n result.RowKey:{1} \n result.Timestamp:{2} \n result.Rating:{3} \n result.Favorite:{4}",
                 result.PartitionKey,
                 result.RowKey,
                 result.Timestamp,
                 result.Rating,
                 result.Favorite));

             Console.ReadKey();
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Services.Common;

namespace ConsoleApplication1
{
    [DataServiceKey("PartitionKey", "RowKey")]
    public class Movie
    {
        public string PartitionKey { get; set; }
        public string RowKey { get; set; }
        public DateTime Timestamp  { get; set; }
        public double Rating { get; set; }
        public bool Favorite { get; set; }

        public Movie() { }
        public Movie(string partitionKey, string rowKey)
        {
            PartitionKey = partitionKey;
            RowKey = rowKey;
            Rating = 5;
            Favorite = false;
        }
    }
}



 类似资料: