{"id":4235,"date":"2023-11-04T23:14:10","date_gmt":"2023-11-04T23:14:10","guid":{"rendered":"http:\/\/localhost:10003\/implementing-azure-table-storage-as-nosql-database\/"},"modified":"2023-11-05T05:47:54","modified_gmt":"2023-11-05T05:47:54","slug":"implementing-azure-table-storage-as-nosql-database","status":"publish","type":"post","link":"http:\/\/localhost:10003\/implementing-azure-table-storage-as-nosql-database\/","title":{"rendered":"Implementing Azure Table Storage as NoSQL database"},"content":{"rendered":"
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.<\/p>\n
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:<\/p>\n
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.<\/p>\n
Create a resource<\/code> button<\/li>\n- Search for ‘Azure Table Storage’ and select it<\/li>\n
- Click on the
Create<\/code> button<\/li>\n- Fill in the necessary information such as the subscription, resource group, and account name.<\/li>\n
- Select the desired pricing tier and performance tier.<\/li>\n
- Click on the
Review + create<\/code> button.<\/li>\n- Review the details and click on the
Create<\/code> button.<\/li>\n<\/ol>\nCreating an Azure Table Storage account using Azure Storage Explorer<\/h2>\n\n- Download and install Azure Storage Explorer<\/li>\n
- Open the Azure Storage Explorer and select the ‘Connect to Azure Storage’ option from the ‘Explorer’ menu.<\/li>\n
- Enter the Azure Subscription details and login.<\/li>\n
- Click on the
Add Account<\/code> button and select Azure Table Storage<\/code>.<\/li>\n- Fill in the necessary information such as the account name, access key, and resource group.<\/li>\n
- Click on the
Create<\/code> button.<\/li>\n<\/ol>\nStep 2: Create an ASP.NET Core project<\/h1>\n
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.<\/p>\n
\n- Open the .NET Core CLI window or terminal and navigate to the desired directory.<\/li>\n
- Enter the command
dotnet new webapi -n <ProjectName><\/code>.<\/li>\n- Once the project has been created, we can navigate to the project directory and open it in our preferred code editor.<\/li>\n<\/ol>\n
Step 3: Add the Microsoft.Azure.Cosmos.Table nuget package<\/h1>\n
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.<\/p>\n
We can add this package by running the following command in the .NET Core CLI window or terminal:<\/p>\n
dotnet add package Microsoft.Azure.Cosmos.Table\n<\/code><\/pre>\nAlternatively, we can add this package through the Package Manager Console by running the following command:<\/p>\n
Install-Package Microsoft.Azure.Cosmos.Table\n<\/code><\/pre>\nStep 4: Create a Table Storage entity<\/h1>\n
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.<\/p>\n
“`C#
\nusing Microsoft.Azure.Cosmos.Table;<\/p>\n
public class CustomerEntity : TableEntity
\n{
\n public CustomerEntity(string lastName, string firstName)
\n {
\n this.PartitionKey = lastName;
\n this.RowKey = firstName;
\n }<\/p>\n
public CustomerEntity() { }\n\npublic string Email { get; set; }\n\npublic string PhoneNumber { get; set; }\n<\/code><\/pre>\n}<\/p>\n
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.<\/p>\n# Step 5: Save data to Table Storage<\/p>\n
Now that we've defined our entity class, we can save data to Table Storage.<\/p>\n
```C#
\nusing Microsoft.Azure.Cosmos.Table;<\/p>\n
CloudStorageAccount storageAccount = CloudStorageAccount.Parse("<connection_string>");
\nCloudTableClient tableClient = storageAccount.CreateCloudTableClient(new TableClientConfiguration());
\nCloudTable table = tableClient.GetTableReference("<table_name>");<\/p>\n
CustomerEntity customer = new CustomerEntity("Smith", "John")
\n{
\n Email = "john.smith@contoso.com",
\n PhoneNumber = "425-555-0101"
\n};<\/p>\n
TableOperation insertOperation = TableOperation.Insert(customer);
\ntable.Execute(insertOperation);
\n<\/code><\/pre><\/p>\n
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.<\/p>\n
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.<\/p>\n
<h1>Step 6: Retrieve data from Table Storage<\/h1><\/p>\n
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.<\/p>\n
```C#
\nusing Microsoft.Azure.Cosmos.Table;<\/p>\n
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(\"\");
\nCloudTableClient tableClient = storageAccount.CreateCloudTableClient(new TableClientConfiguration());
\nCloudTable table = tableClient.GetTableReference(\"\");<\/p>\n\/\/ Retrieve a single entity
\nTableOperation retrieveOperation = TableOperation.Retrieve(\"Smith\", \"John\");
\nTableResult result = table.Execute(retrieveOperation);<\/p>\nCustomerEntity customer = result.Result as CustomerEntity;
\nif (customer != null)
\n{
\n Console.WriteLine($\"Email: {customer.Email}, Phone Number: {customer.PhoneNumber}\");
\n}<\/p>\n
\/\/ Retrieve a list of entities
\nTableQuery query = new TableQuery().Where(TableQuery.GenerateFilterCondition(\"PartitionKey\", QueryComparisons.Equal, \"Smith\"));
\nIEnumerable customers = table.ExecuteQuery(query);
\nforeach (CustomerEntity entity in customers)
\n{
\n Console.WriteLine($\"Email: {entity.Email}, Phone Number: {entity.PhoneNumber}\");
\n}<\/p>\n
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.<\/p>\nWe can also retrieve a list of `CustomerEntity` instances by creating a `TableQuery` instance and executing the query using the `table.ExecuteQuery` method.<\/p>\n
# Step 7: Update data in Table Storage<\/p>\n
Updating data in Table Storage is done by calling the `TableOperation.Replace` or the `TableOperation.Merge` method.<\/p>\n
```C#
\nusing Microsoft.Azure.Cosmos.Table;<\/p>\n
CloudStorageAccount storageAccount = CloudStorageAccount.Parse("<connection_string>");
\nCloudTableClient tableClient = storageAccount.CreateCloudTableClient(new TableClientConfiguration());
\nCloudTable table = tableClient.GetTableReference("<table_name>");<\/p>\n
TableOperation retrieveOperation = TableOperation.Retrieve<CustomerEntity>("Smith", "John");
\nTableResult result = table.Execute(retrieveOperation);
\nCustomerEntity customer = result.Result as CustomerEntity;
\nif (customer != null)
\n{
\n customer.PhoneNumber = "425-555-0123";
\n TableOperation updateOperation = TableOperation.Replace(customer);
\n table.Execute(updateOperation);
\n}
\n<\/code><\/pre><\/p>\n
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.<\/p>\n
<h1>Step 8: Delete data from Table Storage<\/h1><\/p>\n
Deleting data from Table Storage is done by calling the <code>TableOperation.Delete<\/code> method.<\/p>\n
```C#
\nusing Microsoft.Azure.Cosmos.Table;<\/p>\n
CloudStorageAcco<\/p>\n
unt storageAccount = CloudStorageAccount.Parse(\"\");
\nCloudTableClient tableClient = storageAccount.CreateCloudTableClient(new TableClientConfiguration());
\nCloudTable table = tableClient.GetTableReference(\"\");<\/p>\nTableOperation retrieveOperation = TableOperation.Retrieve(\"Smith\", \"John\");
\nTableResult result = table.Execute(retrieveOperation);
\nCustomerEntity customer = result.Result as CustomerEntity;
\nif (customer != null)
\n{
\n TableOperation deleteOperation = TableOperation.Delete(customer);
\n table.Execute(deleteOperation);
\n}
\n```<\/p>\nIn the code above, we retrieve a CustomerEntity<\/code> instance using the TableOperation.Retrieve<\/code> method. We then create a new TableOperation<\/code> instance with the delete operation and CustomerEntity<\/code> instance, executing the operation using the table.Execute<\/code> method.<\/p>\nConclusion<\/h1>\n
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.<\/p>\n","protected":false},"excerpt":{"rendered":"
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 Continue Reading<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[87,30,456,212,1814,1816,1815,1813],"yoast_head":"\nImplementing Azure Table Storage as NoSQL database - Pantherax Blogs<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n\t\n