Azure Data Services
This article will cover the basics of the Azure data service offerings and also show you how to use them.
Catch our previous article on Azure cloud services here.
This article will cover the basics of design and implementation of storage (tables and blobs) and Azure SQL databases. We will also touch upon services using the REST API.
For data storage, you need a SQL management system and data providing service
To start with, Microsoft Azure provides us two storage options: Azure Storage and Azure SQL database
Azure Storage
Azure Table storage is a NOSQL key value store which provides the ability to store non-relational data and structured data without any schemas – thus the application data models aren’t necessarily locked down to a set of schemas. It is not a relational data store and does not provide the same relational data management functions as Azure SQL Database does (joins, stored procedures etc.). It is more useful for high volume databases, geographically distributed databases etc.
Azure SQL Database
Azure SQL Database is a relational database management system based on the SQL Server engine and built on standard relational principles and practices. As such, it provides relational data management capabilities through Transact-SQL queries, ACID transactions, and stored procedures that are executed on the server side. SQL Database is more suitable when the application requires data sets with relationships, integrity, referential constraints, and data volume not exceeding 150 GB.
To use Azure Storage, the user will need to create an Azure Storage Account. The account provides us with three services – Table, Blob and Queue. Before moving on to creating an Azure Storage Account, let’s talk about Table Storage and Blob Storage in brief.
Azure Table Storage
Azure Table Storage stores large amounts of non-relational structured data. Due to its unstructured nature (schema-less) it enables us to store and retrieve simple relational data efficiently. Additionally, different rows within the same table can have different structures in Azure Table Storage.
Azure Blob Storage
Azure Blob (Binary Large Objects) storage stores large amounts of unstructured binary and text data. It provides persistent data storage which is perfect for users of the PaaS system – Cloud Services – because the hard drives used in cloud server instances themselves are not persistent.
Creating an Azure Storage Account
(These instructions are only valid if the user already has a Microsoft Azure account. Click here to find out how to get an account)
Step 1:
Login to the Windows Azure management portal, and then click Storage.
Step 2:
Then click on Quick Create and fill in the required credentials. The URL through which the user wants to access the Azure Storage account is entered in the first box. In the second box, the name should be unique (globally for all users) and all must be entered in lower cases. The closest area in terms of proximity needs to be filled in, and then the kind of subscription. The replication option is primarily to replicate the database for availability and durability.
Step 3:
The storage account should now be created successfully with the status saying that it is Online.
Step 4:
If you scroll down the same page, you can notice three different endpoints are listed – Blobs, Tables, and Queues. They are all created by default while creating the storage. As already mentioned, this article will focus more on Tables and Blobs.
And that's all it takes to set up a Azure Storage Account!
Azure Table Storage Implementation
Table storage doesn’t enforce schema and that means entities with different properties can be contained in the same table – it could be viewed as a container for a collection of entities with properties. An entity is similar to a database row and it has size limitation of 1 MB, and each property is a name-value pair and has its own size restrictions. Each entity must have three properties: a partition key, a row key and a timestamp
This should be made further clear with this example. In the figure above, Library Catalogue is the Table, Books and DVD are the entities, and Title, Author, etc. are the key-value pair properties. The tables can be physically partitioned for load balancing and stability – for instance, Books and DVD can be partitioned using LINQ queries, and we would be able to search either only in Books or DVD instead of scanning the entire table. The query for looking for a book with a particular title would look something like:
var query = _serviceContext.LibraryCatalogue.AsTableServiceQuery()
Where(c => c.PartitionKey=="LibraryCatalogue_Books"&& c.Title == "mySearchTitle");
REST API to use Table Service
The REST API for Table service provides operations to manage the table and the date it contains. In this section, we will look at some basic operations.
As a pre-requisite, the user needs to create connection strings to the services using the Azure cloud service.
1) Namespaces to Use:
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Table;
2) Creating a Table
// Retrieve the storage account from the connection string
CloudStorageAccount storageAccount = CloudStorageAccount.Parse();
CloudConfigurationManager.GetSetting("myFirstAppStorageConnectionString"));
// Create the table client
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
// Create the table if it doesn't exist
CloudTable table = tableClient.GetTableReference("LibraryCatalogue");
table.CreateIfNotExists();
3) Creating an Entity
public class BooksEntity : TableEntity
{
public BooksEntity(string Title, string Author, string Language)
{
this.PartitionKey = Language;
this.RowKey = Title;
// hidden lines of code
}
public BooksEntity() { }
public string PublishedYear { get; set; }
}
4) Insert Operation on Table
BookEntity myBook = new BookEntity(“myTitle”, “myAuthor”,”myLanguage”);
myBook.PublishedYear = “2014”;
TableOperation insertOperation = TableOperation.Insert(myBook);
Table.Execute(insertOperation);
Azure Blob Storage Implementation
Blob storage is similar to Table storage in the sense that it has containers and blobs. A container is a set of blobs, and a Blob can store any type of file and have various sizes (Block and Page blobs). All file blobs (with 200 GB size limitation) are block blobs.
Over here Books, Audio books, Movies are containers and the myBook.pdf, myAudiofile.wav and myMovie.avi files are block blobs.
REST API to use Blob Service
The REST API for the Blob service defines HTTP operations against container and blob resources. With these operations we can manage them programmatically. In this section, we will look at basic operations.
As a pre-requisite, the user will need to create connection strings to the services using the Azure cloud service.
1) Namespaces to Use
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Blob;
2) Create a Container
// Retrieve the storage account from the connection string
CloudStorageAccount storageAccount = CloudStorageAccount.Parse();
CloudConfigurationManager.GetSetting(“StorageConnectionString”));
// Create the blob client
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve a reference to a container.
CloudBlobContainer container = blobClient.GetContainerReference(“DigitalLibary”);
// Create the container if it doesn’t already exist.
container.CreateIfNotExists();
3) Upload the Blob
// Retrieve reference to a previously created container
CloudBlobContainer container = blobClient.GetContainerReference("DigitalLibrary");
// Retrieve reference to a blob named "Books"
CloudBlockBlob blockBlob = container.GetBlockBlobReference("Books");
// Create or overwrite the "Books" blob with contents from a local file.
using (var fileStream = System.IO.File.OpenRead(@"path\myBook.pdf"))
{
blockBlob.UploadFromStream(fileStream);
}
To conclude with, this article described Azure Storage Accounts, Tables and Blob services in brief. It also covered code samples to use the table and blob REST services and its operations.
The next article in this series would cover Microsoft Azure’s media service offerings. Give it a read here.