Recursively Loop the Embedded Entity Object to Insert a Relational Data Set to Cloud Storage Tables virtual public bool InsertICloudEntity entity { bool success = false; ICloudEntity
Trang 1CHAPTER 2 ■ ACCESS CLOUD TABLE STORAGE
63
userTableService.Insert(user as User);
}
Listing 2-26 Recursively Loop the Embedded Entity Object to Insert a Relational Data Set to Cloud
Storage Tables
virtual public bool Insert(ICloudEntity entity)
{
bool success = false;
ICloudEntity dependency = null;
try
{
dataTableContext.AddObject( dataTableContext.TableName, entity);
dataTableContext.SaveChanges();
dependency = entity.GetDependencyEntity();
while (null != dependency)
{
CloudTableServiceFactory cloudTableFactory =
new CloudTableServiceFactory();
cloudTableFactory.FactoryCloudTableService(dependency)
.Insert(dependency);
dependency = dependency.GetDependencyEntity();
}
success = true;
}
catch { }
return success;
}
• To delete a set of relational data entities from cloud table storage, a parent entity
object is responsible for passing the partition key that is the RowKey of the child
record The list of dependency entities is the one we built up when constructing
each entity instance The highlighted lines from Listing 2-27 show how to
accomplish this task
Listing 2-27 Recursively Loop the Embedded Entity Object to Delete a Relational Data Set
virtual public bool Delete(ICloudEntity entity)
{
bool success = false;
foreach (ICloudEntity entityType in entity.DependencyType())
{
ICloudEntity dependency =
QueryDependencyEntity(entityType, (entity as TableStorageEntity).RowKey);
Trang 2CHAPTER 2 ■ ACCESS CLOUD TABLE STORAGE
64
if (null != dependency)
{
cloudTableFactory.FactoryCloudTableService(dependency).Delete(dependency); }
}
try
{
dataTableContext.AttachTo( dataTableContext.TableName, entity, "*");
dataTableContext.DeleteObject(entity);
dataTableContext.SaveChanges();
success = true;
}
catch {}
return success;
}
• The implementation for the Update() method is relatively simple as shown in
Listing 2-28 It is just a combination call to the Delete() and Insert() methods based upon the strategy updating cloud storage data in buckets
Listing 2-28 Updating Relational Entities in Buckets
virtual public bool Update(ICloudEntity entity)
{
bool success = false;
try
{
if (Delete(entity))
{
success = Insert(entity);
}
}
catch { }
return success;
}
• Elaborately designed, tested, and tuned PartitionKey and RowKey are very
important tasks for cloud table data modeling To learn more about this topic from the Microsoft documentation search the web for “Azure Choosing a partition key is important for an application to be able to scale well.”
If you are interested in the data center traffic characteristics you can find the Microsoft official documentation at http://research.microsoft.com/en-us/people/mzh/wren09.pdf
Trang 3CHAPTER 2 ■ ACCESS CLOUD TABLE STORAGE
65
Summary
In this chapter we covered a lot of ground; we examined quite a few aspects of cloud table storage We
saw how to access data in the cloud and worked through quite a few hints and tips to use when working with cloud table storage data access There are many useful tools available to you, such as Fiddler 2,
LINQ, and REST
Storing your data in the cloud would be useless if you couldn't alter it or remove it, so we covered
this aspect of data storage next We saw how to update data in buckets (a useful technique with cloud
storage) and covered some best practices
Finally, we looked at relational storage in cloud table storage, which is definitely an option for some projects
Trang 5C H A P T E R 3
■ ■ ■
67
Working with Cloud Queue and
Blob Storage
Azure Queue and Blob storage services are two basic forms of storage offered by the Azure framework
Another basic cloud storage offered by the Azure framework is table storage, which we covered in the
last two chapters In this chapter we are going to focus on Queue and Blob Azure Queue messages can
be listened for via event subscription This feature makes Azure Queue a good candidate for building an event-driven distributed system In this chapter I’ll provide the basic know-how for using these two
types of storage, a description of how to use both storage types to build an event-driven distributed
system, and a tool for you to load a large stream to cloud Blob storage
The first exercise is about the basics of how to create and use the Azure Queue The second exercise
is an example of cloud Blob storage In this exercise we will use Azure Queue as a trigger to create a blob record when the queue receives a message The third exercise uses both Azure Queue and Blob storage services to build an event-driven distributed system
Before we see the exercises, however, let’s see what Azure Queue and Blob Storage services are
Azure Queue
Azure Queue provides a simple and asynchronous work dispatch mechanism This makes Azure Queue a great message delivery tool that can be used to connect different components of a cloud application into
an integrated service system The outstanding merits of Azure Queue are high availability, durability,
and performance efficiency Azure Queue guarantees message delivery and ensures that a message can
be processed at least once Azure Queue provides REST interfaces, which allow applications written in languages other than C# to access the queue at any time from anywhere across the Internet This makes
a cloud application or on-premises application very easily integrated, extendable, and scalable
Azure Queue can be used for both cloud applications and on-premises applications for the
following two purposes:
• Message communication bus
• Component or functional module decoupling