Implementing Azure Table Storage as NoSQL database

Overview

Azure Table Storage is a NoSQL database that provides a scalable and flexible solution for managing structured data in the cloud. It is designed to handle large amounts of structured data in a distributed environment, making it an excellent choice for handling massive amounts of data that require high performance and scalability. In this tutorial, we will discuss how to implement Azure Table Storage as a NoSQL database.

Prerequisites

Before we dive into implementing Azure Table Storage, we should make sure that we are prepared with the necessary tools and resources. Some of the prerequisites that we need are:

  • An Azure subscription
  • .NET Core SDK
  • Azure Storage Explorer

Step 1: Create an Azure Table Storage account

The first thing that we need to do is create an Azure Table Storage account. We can create this account through the Azure portal or using the Azure Storage Explorer.

Create an Azure Table Storage account using the Azure portal

  1. Log in to the Azure portal
  2. Click on the Create a resource button
  3. Search for ‘Azure Table Storage’ and select it
  4. Click on the Create button
  5. Fill in the necessary information such as the subscription, resource group, and account name.
  6. Select the desired pricing tier and performance tier.
  7. Click on the Review + create button.
  8. Review the details and click on the Create button.

Creating an Azure Table Storage account using Azure Storage Explorer

  1. Download and install Azure Storage Explorer
  2. Open the Azure Storage Explorer and select the ‘Connect to Azure Storage’ option from the ‘Explorer’ menu.
  3. Enter the Azure Subscription details and login.
  4. Click on the Add Account button and select Azure Table Storage.
  5. Fill in the necessary information such as the account name, access key, and resource group.
  6. Click on the Create button.

Step 2: Create an ASP.NET Core project

After setting up the Azure Table Storage account, we should create an ASP.NET Core project that will be used to interact with the Table Storage account.

  1. Open the .NET Core CLI window or terminal and navigate to the desired directory.
  2. Enter the command dotnet new webapi -n <ProjectName>.
  3. Once the project has been created, we can navigate to the project directory and open it in our preferred code editor.

Step 3: Add the Microsoft.Azure.Cosmos.Table nuget package

To interact with Azure Table Storage, we need to have the Microsoft.Azure.Cosmos.Table nuget package installed in our project. This package provides the necessary classes and methods to interact with the Table Storage account.

We can add this package by running the following command in the .NET Core CLI window or terminal:

dotnet add package Microsoft.Azure.Cosmos.Table

Alternatively, we can add this package through the Package Manager Console by running the following command:

Install-Package Microsoft.Azure.Cosmos.Table

Step 4: Create a Table Storage entity

In Table Storage, data is stored in tables as entities. An entity is a collection of properties that define the characteristics of an item. Before we save data in Table Storage, we need to define an entity class that represents the object that we want to store.

“`C#
using Microsoft.Azure.Cosmos.Table;

public class CustomerEntity : TableEntity
{
public CustomerEntity(string lastName, string firstName)
{
this.PartitionKey = lastName;
this.RowKey = firstName;
}

public CustomerEntity() { }

public string Email { get; set; }

public string PhoneNumber { get; set; }

}


In this example, we have defined a `CustomerEntity` class that inherits from the `TableEntity` class provided by the Cosmos.Table nuget package. This class has four properties: `PartitionKey`, `RowKey`, `Email`, and `PhoneNumber`. The `PartitionKey` and `RowKey` properties are inherited from the `TableEntity` class and are used to uniquely identify the entity. The `Email` and `PhoneNumber` properties are custom properties that we have defined to store additional customer information.

# Step 5: Save data to Table Storage

Now that we've defined our entity class, we can save data to Table Storage.

```C#
using Microsoft.Azure.Cosmos.Table;

CloudStorageAccount storageAccount = CloudStorageAccount.Parse("<connection_string>");
CloudTableClient tableClient = storageAccount.CreateCloudTableClient(new TableClientConfiguration());
CloudTable table = tableClient.GetTableReference("<table_name>");

CustomerEntity customer = new CustomerEntity("Smith", "John")
{
Email = "[email protected]",
PhoneNumber = "425-555-0101"
};

TableOperation insertOperation = TableOperation.Insert(customer);
table.Execute(insertOperation);
</code></pre>

In the code above, we create an instance of the <code>CloudStorageAccount</code> class by parsing the connection string for our Table Storage account. We then create a <code>CloudTableClient</code> instance from the storage account and obtain a reference to the table that we want to add data to.

We create a <code>CustomerEntity</code> instance with the properties set to the desired values and then create a new <code>TableOperation</code> instance with the insert operation and the <code>CustomerEntity</code> instance. Finally, we execute the operation by calling the <code>Execute</code> method on the table object.

<h1>Step 6: Retrieve data from Table Storage</h1>

Retrieving data from Table Storage is also straightforward. We can use the <code>TableOperation.Retrieve</code> method to retrieve a specific entity or the <code>TableQuery</code> class to retrieve a list of entities.

```C#
using Microsoft.Azure.Cosmos.Table;

CloudStorageAccount storageAccount = CloudStorageAccount.Parse("");
CloudTableClient tableClient = storageAccount.CreateCloudTableClient(new TableClientConfiguration());
CloudTable table = tableClient.GetTableReference("");

// Retrieve a single entity
TableOperation retrieveOperation = TableOperation.Retrieve("Smith", "John");
TableResult result = table.Execute(retrieveOperation);

CustomerEntity customer = result.Result as CustomerEntity;
if (customer != null)
{
Console.WriteLine($"Email: {customer.Email}, Phone Number: {customer.PhoneNumber}");
}

// Retrieve a list of entities
TableQuery query = new TableQuery().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "Smith"));
IEnumerable customers = table.ExecuteQuery(query);
foreach (CustomerEntity entity in customers)
{
Console.WriteLine($"Email: {entity.Email}, Phone Number: {entity.PhoneNumber}");
}


In the code above, we retrieve a single `CustomerEntity` instance by calling the `TableOperation.Retrieve` method and passing in the `PartitionKey` and `RowKey` values to identify the entity. We then execute the operation and retrieve the result using the `TableResult.Result` property.

We can also retrieve a list of `CustomerEntity` instances by creating a `TableQuery` instance and executing the query using the `table.ExecuteQuery` method.

# Step 7: Update data in Table Storage

Updating data in Table Storage is done by calling the `TableOperation.Replace` or the `TableOperation.Merge` method.

```C#
using Microsoft.Azure.Cosmos.Table;

CloudStorageAccount storageAccount = CloudStorageAccount.Parse("<connection_string>");
CloudTableClient tableClient = storageAccount.CreateCloudTableClient(new TableClientConfiguration());
CloudTable table = tableClient.GetTableReference("<table_name>");

TableOperation retrieveOperation = TableOperation.Retrieve<CustomerEntity>("Smith", "John");
TableResult result = table.Execute(retrieveOperation);
CustomerEntity customer = result.Result as CustomerEntity;
if (customer != null)
{
customer.PhoneNumber = "425-555-0123";
TableOperation updateOperation = TableOperation.Replace(customer);
table.Execute(updateOperation);
}
</code></pre>

In the code above, we retrieve a <code>CustomerEntity</code> instance using the <code>TableOperation.Retrieve</code> method. We then update the <code>PhoneNumber</code> property and create a new <code>TableOperation</code> instance with the update operation and <code>CustomerEntity</code> instance, executing the operation using the <code>table.Execute</code> method.

<h1>Step 8: Delete data from Table Storage</h1>

Deleting data from Table Storage is done by calling the <code>TableOperation.Delete</code> method.

```C#
using Microsoft.Azure.Cosmos.Table;

CloudStorageAcco

unt storageAccount = CloudStorageAccount.Parse("");
CloudTableClient tableClient = storageAccount.CreateCloudTableClient(new TableClientConfiguration());
CloudTable table = tableClient.GetTableReference("");

TableOperation retrieveOperation = TableOperation.Retrieve("Smith", "John");
TableResult result = table.Execute(retrieveOperation);
CustomerEntity customer = result.Result as CustomerEntity;
if (customer != null)
{
TableOperation deleteOperation = TableOperation.Delete(customer);
table.Execute(deleteOperation);
}
```

In the code above, we retrieve a CustomerEntity instance using the TableOperation.Retrieve method. We then create a new TableOperation instance with the delete operation and CustomerEntity instance, executing the operation using the table.Execute method.

Conclusion

In this tutorial, we covered the basics of implementing Azure Table Storage as a NoSQL database. We learned how to create a Table Storage account, create an ASP.NET Core project, add the necessary nuget package, create a Table Storage entity, save data to Table Storage, retrieve data from Table Storage, update data in Table Storage, and delete data from Table Storage. With these foundational concepts, we can build applications that utilize Azure Table Storage to manage and store structured data.

Related Post