Thursday, June 01, 2023

Python : Reading CSV Files from Azure File Share Using Python

Introduction:

In today's data-driven world, handling and analyzing data is a crucial task for businesses and developers alike. Azure File Share provides a scalable and reliable storage solution in the cloud. Python, being a popular programming language for data processing and analysis, can be leveraged to read CSV files from Azure File Share effortlessly. In this article, we will explore step-by-step instructions to read CSV files stored in Azure File Share using Python. We had requirement in our science related project where we need to process some part of our data in Python web app. As all scientist were comfortable with python we need to process the file in python.

Prerequisites:

  1. An Azure account with an active subscription.
  2. An Azure File Share with CSV files uploaded.
  3. Python installed on your local machine or Docker.

Step 1: Install Azure Storage File Share SDK

To interact with Azure File Share in Python, we need to install the Azure Storage File Share SDK. Open a terminal or command prompt and run the following command:

pip install azure-storage-file-share

Step 2: Import Required Libraries

Create a Python script and start by importing the necessary libraries:

from azure.storage.fileshare import ShareClient, ShareFileClient
import pandas as pd
import numpy as np

Step 3: Set Up Azure File Share Connection

Retrieve the connection string for your Azure Storage account from the Azure Portal. Replace 'your_connection_string' with your actual connection string:

connection_string = 'your_connection_string'
file_share_name = 'your_file_share_name'
file_path = '00-TestFiles/mytestfile.csv'

Step 4: Read CSV File from Azure File Share

Next, we will define a function to read the content of CSV file from the Azure File Share:
The below example read the file content into variable file_content. You need to write code to deal with it.

def read_csv_from_azure_file_share(file_path):
    share_client = ShareClient.from_connection_string(connection_string, file_share_name)
    with share_client.get_directory_client('your_directory_name') as directory_client:
        with directory_client.get_file_client(file_path) as file_client:
            file_content = file_client.download_file().readall().decode('utf-8')
    return file_content

Step 5: Process CSV Data

Now that we have the CSV file content in a string, we can use the Pandas library to process and analyze the data:

def process_csv_data(csv_content):
    df = pd.read_csv(pd.compat.StringIO(csv_content))
    # Your data processing and analysis code here
    return df

Sometimes you need particular column to be imported in numpy array. You can use below code to do so.

def read_csv_from_azure_file_share(file_path):
    share_client = ShareFileClient.from_connection_string(connection_string, file_share_name, file_path)
    DEST_FILE = "downloadedfiles/downloadedfile.csv"
    with open(DEST_FILE , "wb") as file_handle:
        stream = file_client.download_file()
        file_handle.write(stream.readall())
        array = np.loadtxt(DEST_FILE, delimiter=",", usecols=(0), skiprows=0)    
    return array

Step 6: Putting It All Together

Call the functions to read and process the CSV data:

if __name__ == '__main__':
    file_path = 'your_file_name.csv'
    csv_content = read_csv_from_azure_file_share(file_path)
    data_frame = process_csv_data(csv_content)
    print(data_frame.head())  # Display the first few rows of the DataFrame

Conclusion:

In this article, we learned how to read CSV files stored in Azure File Share using Python. By leveraging the Azure Storage File Share SDK and the Pandas library, developers can easily access and process data from Azure File Share, making it a powerful combination for data analysis tasks. Whether you are working with small or large datasets, Python and Azure File Share provide a scalable and efficient solution for handling your data in the cloud.

Wednesday, April 26, 2023

Azure Series : File Share - Connecting and Reading CSV Files from Azure File Share in ASP.NET Core

 In today’s data-driven world, handling CSV files is a common task in many applications. Azure File Share provides a scalable and efficient way to store and manage these files in the cloud. In this article, we will explore how to connect to an Azure File Share and read CSV files from it using ASP.NET  Core and C#.

Prerequisites

Before we begin, ensure that you have the following prerequisites in place:

  1. An Azure account with an active subscription.
  2. A storage account and an Azure File Share created.

Setting Up Azure File Share

  1. Log in to the Azure portal (https://portal.azure.com ) and create a storage account if you haven’t already.
  2. Inside your storage account, create a new file share.
  3. Note down the storage account name, the file share name, and the storage account key. You will need these details to connect to the Azure File Share from your ASP.NET  Core application.

Creating an ASP.NET  Core Application

Now, let’s create an ASP.NET  Core application to read CSV files from Azure File Share.

  1. Open Visual Studio or your preferred code editor.
  2. Create a new ASP.NET  Core project.
  3. Install the required NuGet packages:
  4. Microsoft.Azure.Storage.File for Azure Storage File Share access.
  5. CsvHelper for CSV file parsing.
  6. Configure your Azure File Share connection in the appsettings.json file:
{
  "AzureStorageSettings": {
    "StorageAccountName": "your-storage-account-name",
    "StorageAccountKey": "your-storage-account-key",
    "FileShareName": "your-file-share-name"
  }
}
  1. Create a service to interact with Azure File Share. This service should use the Azure Storage SDK to connect to the file share.
public class AzureFileShareService
{
    private readonly IConfiguration _configuration;
    public AzureFileShareService(IConfiguration configuration)
    {
        _configuration = configuration;
    }
    public CloudFileDirectory GetFileShareDirectory()
    {
        string storageAccountName = _configuration["AzureStorageSettings:StorageAccountName"];
        string storageAccountKey = _configuration["AzureStorageSettings:StorageAccountKey"];
        string fileShareName = _configuration["AzureStorageSettings:FileShareName"];
        var storageAccount = CloudStorageAccount.Parse($"DefaultEndpointsProtocol=https;AccountName={storageAccountName};AccountKey={storageAccountKey};EndpointSuffix=core.windows.net");
        var fileClient = storageAccount.CreateCloudFileClient();
        var share = fileClient.GetShareReference(fileShareName);
        return share.GetRootDirectoryReference();
    }
    // Add methods to read CSV files here
}

Reading CSV Files from Azure File Share

Now that we have our Azure File Share service set up, let’s create a method to read CSV files:

public async Task<IEnumerable<MyDataModel>> ReadCsvFileAsync(string fileName)
{
    var directory = GetFileShareDirectory();
    var fileReference = directory.GetFileReference(fileName);
    if (await fileReference.ExistsAsync())
    {
        // Open the file stream
        var stream = await fileReference.OpenReadAsync();
        // Use CsvHelper to read the CSV data
        using (var reader = new StreamReader(stream))
        using (var csv = new CsvReader(reader, new CsvConfiguration(CultureInfo.InvariantCulture)))
        {
            var records = csv.GetRecords<MyDataModel>().ToList();
            return records;
        }
    }
    else
    {
        throw new FileNotFoundException("File not found in Azure File Share.");
    }
}

In this example, MyDataModel should be replaced with your specific CSV data model.

Thursday, January 05, 2023

Azure Series : RabbitMQ - Setup and Use - Building a Message Queue in RabbitMQ and Publish Message

Introduction

In today's interconnected and distributed systems, message queues play a crucial role in ensuring reliable communication between various components. RabbitMQ, a popular open-source message broker, provides a robust and scalable solution for implementing message queues. In this article, we will explore how to create a message queue in RabbitMQ using ASP.NET  Core and C#.

Prerequisites

Before we dive into the implementation, make sure you have the following tools installed:

  1. .NET Core SDK (version compatible with ASP.NET  Core)
  2. RabbitMQ server (either locally or on a cloud-based service)

Step 1: Set up the RabbitMQ Server

First, ensure you have RabbitMQ installed and running. You can download and install it from the official website or use a cloud-based service like RabbitMQ Cloud or CloudAMQP. You can also install it locally on your machine or use Docker to run RabbitMQ

Step 2: Create an ASP.NET  Core Project

Open your terminal or command prompt and create a new ASP.NET  Core project using the following command:

bash
dotnet new webapi -n MessageQueueExample
cd MessageQueueExample

Step 3: Install RabbitMQ Client Library

Next, install the RabbitMQ.Client NuGet package, which allows our ASP.NET  Core application to communicate with the RabbitMQ server:

dotnet add package RabbitMQ.Client

Step 4: Implement the Message Queue

Now, let's create a MessageQueueService class to handle interactions with the RabbitMQ server. Inside the Services folder, create a new file named MessageQueueService.cs.

using RabbitMQ.Client;
using System;
using System.Text;

namespace MessageQueueExample.Services
{
    public class MessageQueueService : IDisposable
    {
        private readonly IConnection _connection;
        private readonly IModel _channel;
        private const string QueueName = "my_queue";

        public MessageQueueService()
        {
            var factory = new ConnectionFactory() { HostName = "localhost" }; // Replace "localhost" with your RabbitMQ server address if needed
            _connection = factory.CreateConnection();
            _channel = _connection.CreateModel();
            _channel.QueueDeclare(queue: QueueName, durable: false, exclusive: false, autoDelete: false, arguments: null);
        }

        public void SendMessage(string message)
        {
            var body = Encoding.UTF8.GetBytes(message);
            _channel.BasicPublish(exchange: "", routingKey: QueueName, basicProperties: null, body: body);
        }

        public void Dispose()
        {
            _channel.Close();
            _connection.Close();
        }
    }
}

Step 5: Configure Dependency Injection

In the Startup.cs file, configure the MessageQueueService to be injected into the application:

using MessageQueueExample.Services;
// ...

public void ConfigureServices(IServiceCollection services)
{
    // ...
    services.AddSingleton<MessageQueueService>();
    // ...
}

Step 6: Sending Messages

Now that our message queue service is ready, let's use it to send messages. In your desired controller, inject the MessageQueueService, and use it to send a message:

using Microsoft.AspNetCore.Mvc;
using MessageQueueExample.Services;
// ...

[ApiController]
[Route("api/[controller]")]
public class MessageController : ControllerBase
{
    private readonly MessageQueueService _messageQueueService;

    public MessageController(MessageQueueService messageQueueService)
    {
        _messageQueueService = messageQueueService;
    }

    [HttpPost]
    public IActionResult SendMessage([FromBody] string message)
    {
        _messageQueueService.SendMessage(message);
        return Ok("Message sent successfully!");
    }
}

Conclusion

Congratulations! You have successfully implemented a message queue in RabbitMQ using ASP.NET  Core and C#. This setup will allow your applications to communicate efficiently and reliably, enabling a more scalable and resilient architecture.

Remember to handle error scenarios and add appropriate exception handling in a real-world scenario. Additionally, consider implementing message processing on the consumer side for a complete message queue system. Happy coding!

Wednesday, January 04, 2023

Azure Series : RabbitMQ - Setup and Use - Install RabbitMQ

Install RabbitMQ

If you plan on to host RabbitMQ locally, you can either install RabbitMQ in your local workspace or run a RabbitMQ Docker container in your local workspace.

To install RabbitMQ in your local workspace:

  1. Make sure you have already installed Chocolatey Package Manager
  2. RunAsAdministrator - Windows Powershell
  3. From the powershell prompt run the following command to install RabbitMQ:
    choco install rabbitmq
  4. Follow the installation prompts
  5. After the installation completes if you have no critical errors, RabbitMQ should now be installed in your local workspace.
  6. To verify RabbitMQ is now running in your local workspace, open a browser window and go to the following URL:
    http://localhost:15672/ 
  7. You should see a login screen for the RabbitMQ web interface. The default RabbitMQ username and password is as follows:


To install RabbitMQ in your docker:

  1. Make sure you have installed Docker. 
  2. RunAsAdministrator - Windows Powershell
  3. First, let’s pull the RabbitMQ docker image. We’ll use the 3-management version, so we get the Management plugin pre-installed.

    docker pull rabbitmq:3.12-management
  4. Now let’s stand it up. We’ll map port 15672 for the management web app and port 5672 for the message broker.

    docker run -it --rm --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:3.12-management

  5. Assuming that ran successfully, you’ve got an instance of RabbitMQ running! Bounce over to http://localhost:15672 to check out the management web app.

You should see a login screen for the RabbitMQ web interface. The default RabbitMQ user name and password is as follows:


username: guest
password: guest


Here you can see an overview of your RabbitMQ instance and the message broker’s basic components: Connections, Channels, Exchanges, and Queues.

Azure Series : Installation - Chocolatey Package Manager

Install Chocolatey Package Manager


What is Chocolatey? 

Chocolatey Official Installation Instructions 

Summary of Installation Steps

  1. RunAsAdministrator - Windows Powershell
  2. From the Powershell prompt install chocolatey using the following command:
    Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
  3. If no errors occurred during installation, you can verify Chocolatey is installed using the following command:
    choco -?

Friday, April 22, 2022

Azure Series - Virtual Machines - Understanding Update Domains and Fault Domains with Visual Examples

In the context of Microsoft Azure, managing the availability and reliability of virtual machines is crucial for a successful cloud deployment. Azure employs the concepts of Update Domains and Fault Domains to ensure high availability and minimize the impact of planned or unplanned hardware maintenance. In this article, we will explore what Update Domains and Fault Domains are and illustrate their significance using visual examples.

Update Domains:

Update Domains are logical groupings of virtual machines within an availability set. Azure uses Update Domains to ensure that during planned maintenance events, not all virtual machines are taken down simultaneously. By dividing VMs into separate Update Domains, the system ensures that only one Update Domain is impacted at a time, while the other VMs remain operational.

Visual Representation of Update Domains:

Consider an availability set with four virtual machines (VM1, VM2, VM3, VM4), and it has three Update Domains (UD1, UD2, UD3).

In the figure below, each square represents an Update Domain, and the virtual machines are distributed across these Update Domains:


During a planned maintenance event, Azure may update VMs one Update Domain at a time. For example, Update Domain 1 (UD1) is updated first, and once it is completed, the system moves on to Update Domain 2 (UD2) and then to Update Domain 3 (UD3). This sequential approach ensures that a minimum number of VMs are affected at any given time, maintaining the availability of the application.

Fault Domains:

Fault Domains are logical groupings of virtual machines that share a common physical infrastructure. Azure uses Fault Domains to ensure that VMs are distributed across separate physical servers to protect against single points of failure. In the event of a hardware failure or outage in one Fault Domain, the VMs in other Fault Domains remain unaffected.

Visual Representation of Fault Domains:

Consider an availability set with four virtual machines (VM1, VM2, VM3, VM4), and it has three Fault Domains (FD1, FD2, FD3).

In the figure below, each rectangle represents a Fault Domain, and the virtual machines are distributed across these Fault Domains:

By distributing VMs across different Fault Domains, Azure ensures that if a hardware failure occurs in one Fault Domain, the VMs in other Fault Domains remain operational. This enhances the fault tolerance of the application and improves overall availability.

Note: 

  • Each virtual machine in your availability set is assigned an update domain and a fault domain by the underlying Azure platform. Each availability set can be configured with up to 3 fault domains and 20 update domains.
  • Availability zones are similar in concept to availability sets. However, there is a distinct difference. While availability sets are used to protect applications from hardware failures within an Azure data center, availability zones, protect applications from complete Azure data center failures.

Conclusion:

Update Domains and Fault Domains are essential concepts in Azure for ensuring high availability and resilience of virtual machines. Update Domains help manage planned maintenance events by updating VMs one domain at a time, while Fault Domains protect against hardware failures by distributing VMs across separate physical servers. By understanding and leveraging these concepts, Azure users can design and deploy robust cloud solutions that deliver consistent performance and availability for their applications.

Wednesday, April 13, 2022

Azure Series - Virtual Machine - Availability and Scale Set with example

Introduction:

Azure Virtual Machines (VMs) provide a scalable and flexible way to deploy and manage applications in the cloud. One of the key features that enhance the reliability and resilience of VMs is the concept of Availability Zones. In this article, we will explore what Availability Zones are in Azure, how they work, and provide examples to illustrate their significance in ensuring high availability for your applications.

What are Availability Zones in Azure?

Availability Zones are physically separate data centers within an Azure region. Each zone is equipped with its power, networking, and cooling infrastructure, ensuring that if one zone experiences an outage, the other zones continue to operate independently. Azure regions typically consist of multiple zones, and these zones are designed to provide redundancy and fault tolerance for your VMs and applications.

Note: To use availability zones, create your virtual machines in a supported Azure region.

Example 1: Single Virtual Machine in One Availability Zone

Let's consider a scenario where you have deployed a single virtual machine in an Azure region that supports Availability Zones. By default, the VM will be provisioned in Zone 1 of that region. This means that your VM is isolated from potential failures in other zones. If Zone 1 experiences any issues, such as hardware failure or network problems, the VM automatically fails over to another zone (e.g., Zone 2) within the same region, ensuring minimal downtime and maintaining the high availability of your application.


Example 2: Virtual Machine Scale Sets Across Availability Zones

In a more complex scenario, suppose you have a web application that requires multiple VM instances to handle incoming traffic. You can use Azure Virtual Machine Scale Sets (VMSS) to deploy and manage a group of identical VMs across different Availability Zones.

For instance, you can configure VMSS to distribute VM instances across three Availability Zones. If Zone 1 becomes unavailable due to maintenance or unforeseen issues, the traffic is automatically redirected to the VM instances in Zones 2 and 3, ensuring that your application remains accessible and resilient.

Example 3: Load Balancing with Availability Zones

Load balancers in Azure can also be configured to distribute incoming traffic across VMs in different Availability Zones. By creating an Azure Load Balancer and associating it with VMs spread across multiple zones, you ensure that if one zone is experiencing high traffic or experiencing an issue, the load balancer automatically directs traffic to the healthy VMs in other zones, thus maintaining consistent performance and availability.

Conclusion:

Availability Zones in Azure Virtual Machines play a vital role in ensuring high availability, fault tolerance, and resilience for your applications. By deploying VMs across multiple physically isolated zones, you minimize the risk of downtime and data loss due to zone-specific failures. Whether you have a single VM or a highly scalable application, leveraging Availability Zones in Azure enables you to provide a robust and reliable cloud infrastructure for your applications, enhancing the overall user experience and peace of mind for you as a developer.