Azure Basic - Windows Storage Enhancement (Table)

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;
        }
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值