Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure table storage - Simplest possible example

Whenever learning new technologies I like to write the simplest possible example. Usually this means a console app with the least number of references. I've been trying, with little success, to write an app that reads and writes to Azure table storage. I've used this how-to guide as a basis, but try to do everything in the Main method. Similar approach worked well with the blob storage, but the table storage is giving trouble.

I was able to create a table with this code.

static void Main(string[] args)
{
    Microsoft.WindowsAzure.Storage.Table.CloudTableClient tableClient =
    new Microsoft.WindowsAzure.Storage.Table.CloudTableClient(
        new Uri("http://mystorage.table.core.windows.net/"),
    new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials("[somename]", "[somekey]"));

    CloudTable table = tableClient.GetTableReference("people");
    table.CreateIfNotExists();
}

After running this code I could see a table in my storage using Azure Storage Explorer. (Still haven't figured out how to see the table in manage.windowsazure.com.)

However, if I try to insert records (as described in the how-to guide mentioned before), I get a conflict 409 EntityAlreadyExists. Azure Storage Explorer doesn't show any records in my table.

CustomerEntity customer1 = new CustomerEntity("Harp", "Walter");
customer1.Email = "[email protected]";
customer1.PhoneNumber = "425-555-0101";

TableOperation insertOperation = TableOperation.Insert(customer1);
table.Execute(insertOperation);

Also, I'm baffled by the two overlapping namespaces. Both Microsoft.WindowsAzure.Storage.Table and Microsoft.WindowsAzure.StorageClient contain e.g. a CloudTableClient class. Why are there two client namespaces and which one am I supposed to use?

EDIT Turns out the record does exist. Simply double-clicking the table in Azure Table Explorer doesn't show the table contents. You have to click Query. The last question still stands. Why the two namespaces?

like image 343
Rubio Avatar asked Jun 05 '13 09:06

Rubio


People also ask

What is Azure table storage used for?

Azure Table storage is a cloud-based NoSQL datastore you can use to store large amounts of structured, non-relational data. Azure Table offers a schemaless design, which enables you to store a collection of entities in one table. An entity contains a set of properties, and each property defines a name-value pair.

How much data can be stored in a single table storage account in Azure?

A single storage account can store up to 500TB of data and like any other Azure service, users can take advantage of the pay-per-use pricing model.


1 Answers

The simplest sample I could think of is this. You need to NuGet WindowsAzure.Storage 2.0.

static void Main(string[] args)
{
  try
  {
     CloudStorageAccount storageAccount =
        CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=<your_storage_name>;AccountKey=<your_account_key>");
     CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

     CloudTable table = tableClient.GetTableReference("people");
     table.CreateIfNotExists();

     CustomerEntity customer1 = new CustomerEntity("Harp", "Walter");
     customer1.Email = "[email protected]";
     customer1.PhoneNumber = "425-555-0101";

     // Create the TableOperation that inserts the customer entity.
     var insertOperation = TableOperation.Insert(customer1);

     // Execute the insert operation.
     table.Execute(insertOperation);

     // Read storage
     TableQuery<CustomerEntity> query =
        new TableQuery<CustomerEntity>()
           .Where(TableQuery.GenerateFilterCondition("PartitionKey",
               QueryComparisons.Equal, "Harp"));
     var list = table.ExecuteQuery(query).ToList();
   }
   catch (StorageException ex)
   {
       // Exception handling here.
   }
}

public class CustomerEntity : TableEntity
{
    public string Email { get; set; }
    public string PhoneNumber { get; set; }

    public CustomerEntity(string lastName, string firstName)
    {
        PartitionKey = lastName;
        RowKey = firstName;
    }

    public CustomerEntity() { }
}

The answer to the seconds question, why are there two namespaces that provided more or less the same APIs, Azure Storage Client Library 2.0 contains a new simplified API. See link below.

What's New in Storage Client Library for .NET (version 2.0)

like image 193
Rubio Avatar answered Jan 03 '23 04:01

Rubio