Leveraging Azure Tables with Azure Functions

Introduction

Azure Tables provide a NoSQL key-value store for rapid development using massive semi-structured datasets. They are part of Azure Storage and are designed for large-scale data storage and high-performance queries. Azure Functions, on the other hand, offers a serverless compute service that enables you to run event-driven code without having to explicitly provision or manage infrastructure. Combining Azure Tables with Azure Functions allows for scalable, efficient, and cost-effective data management solutions.

Prerequisites

Before we dive into the implementation, ensure you have the following.

  1. Azurite (Microsoft Azure Storage Simulator)
  2. Basic knowledge of C#
  3. VisualStudio 2022

Create an Azure Function project using Visual Studio

Azure Function

Enter the project and solution name and click on the next button.

Next button

Select Function Worker as .NET 8 Isolated as In process worker have been deprecated by Microsoft. Select the Function type as HTPP Trigger and click on the Create button.

.NET 8

Install the required Nuget packages from the package manager.

# Install the Azure Tables extension
dotnet add package Microsoft.Azure.Functions.Worker.Extensions.Tables --version 1.0.0

# Update the combined Azure Storage extension (to a version which no longer includes Azure Tables)
dotnet add package Microsoft.Azure.Functions.Worker.Extensions.Storage --version 5.0.0

Set up Azure Storage Table Entity and Inherit it using Microsoft.Azure.TableITableEntity

public class StudentEntity : ITableEntity
{
    public string Name { get; set; } = string.Empty;
    public string Email { get; set; } = string.Empty;
    public string PartitionKey { get; set; } = string.Empty;
    public string RowKey { get; set; } = string.Empty;
    public DateTimeOffset? Timestamp { get; set; }
    public ETag ETag { get; set; }
}

InsertStudent function uses TableOutput binding to write data into Azure Table storage.

[Function("InsertStudent")]
[TableOutput("studentsTable", "AzureWebJobsStorage")]
public StudentEntity InsertStudent([HttpTrigger(AuthorizationLevel.Function,"post")] HttpRequest req, [Microsoft.Azure.Functions.Worker.Http.FromBody]StudentEntity student)
{
    _logger.LogInformation("C# HTTP trigger function processed a request.");
    student.RowKey = Guid.NewGuid().ToString();
    return student;
}

GetAllStudent function uses TableInput binding to read data from Azure Table storage.

[Function("GetAllStudents")]
public IActionResult GetAllStudents([HttpTrigger(AuthorizationLevel.Function,"get")] HttpRequest req,
    [TableInput("studentsTable",Connection = "AzureWebJobsStorage")] IEnumerable<StudentEntity> students)
{
    return new OkObjectResult(students);
}

GetAllMaleStudents uses the TableInput binding parameter with a partition named "male" for getting all student's data from the "male" partition.

[Function("GetAllMaleStudents")]
public IActionResult GetAllMaleStudents([HttpTrigger(AuthorizationLevel.Function,"get")] HttpRequest req,
    [TableInput("studentsTable","male",Connection = "AzureWebJobsStorage")] IEnumerable<StudentEntity> students)
{
    return new OkObjectResult(students);
}

Conclusion

Integrating Azure Tables with Azure Functions provides a powerful and scalable solution for managing large datasets with serverless computing. This approach ensures high availability, scalability, and cost-efficiency, making it ideal for modern cloud-based applications. By following the steps outlined in this article, you can set up and deploy your own solutions leveraging these robust Azure services.

Source Code

You can access this example source code from my GitHub repo. Please give it a start if you like it.

Video Tutorial

You can watch the video tutorial on CSharp TV.


Similar Articles
Finchship
We Provide Web, Desktop and Mobile Apps Solution