NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

using Azure.Storage.Files.DataLake;
using Azure.Storage.Files.DataLake.Models;
using Microsoft.Azure.ServiceBus;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using System;
using System.IO;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

public static class ServiceBusTrigger
{
[FunctionName("Topic1TriggerFunction")]
public static async Task RunTopic1(
[ServiceBusTrigger("%ServiceBusTopics:Topic1:Name%", "%SubscriptionName%", Connection = "ServiceBusConnectionString")]
Message message,
string topicName,
string subscriptionName,
string serviceBusConnectionString,
string adlsAccountName,
string adlsFileSystemName,
string adlsConnectionString,
string environment,
ILogger log,
ExecutionContext context)
{
await ProcessMessage(message, topicName, subscriptionName, serviceBusConnectionString, adlsAccountName, adlsFileSystemName, adlsConnectionString, environment, log, context);
}

[FunctionName("Topic2TriggerFunction")]
public static async Task RunTopic2(
[ServiceBusTrigger("%ServiceBusTopics:Topic2:Name%", "%SubscriptionName%", Connection = "ServiceBusConnectionString")]
Message message,
string topicName,
string subscriptionName,
string serviceBusConnectionString,
string adlsAccountName,
string adlsFileSystemName,
string adlsConnectionString,
string environment,
ILogger log,
ExecutionContext context)
{
await ProcessMessage(message, topicName, subscriptionName, serviceBusConnectionString, adlsAccountName, adlsFileSystemName, adlsConnectionString, environment, log, context);
}

[FunctionName("Topic3TriggerFunction")]
public static async Task RunTopic3(
[ServiceBusTrigger("%ServiceBusTopics:Topic3:Name%", "%SubscriptionName%", Connection = "ServiceBusConnectionString")]
Message message,
string topicName,
string subscriptionName,
string serviceBusConnectionString,
string adlsAccountName,
string adlsFileSystemName,
string adlsConnectionString,
string environment,
ILogger log,
ExecutionContext context)
{
await ProcessMessage(message, topicName, subscriptionName, serviceBusConnectionString, adlsAccountName, adlsFileSystemName, adlsConnectionString, environment, log, context);
}

private static async Task ProcessMessage(Message message, string topicName, string subscriptionName, string serviceBusConnectionString, string adlsAccountName, string adlsFileSystemName, string adlsConnectionString, string environment, ILogger log, ExecutionContext context)
{
// Récupérer l'identifiant du message
string messageId = message.MessageId;

// Récupérer la date d'envoi du message
DateTime enqueuedTimeUtc = message.SystemProperties.EnqueuedTimeUtc;

// Récupérer le contenu du message
string messageBody = Encoding.UTF8.GetString(message.Body);

// Convertir le contenu du message en JSON
string messageJson = JsonSerializer.Serialize(messageBody);

// Enregistrer le message au format JSON dans ADLS Gen2
await SaveMessageToADLS(messageId, enqueuedTimeUtc, messageJson, adlsAccountName, adlsFileSystemName, topicName, environment, adlsConnectionString, log, context);

// Traiter le message reçu
log.LogInformation($"Message reçu depuis le topic '{topicName}' et la souscription '{subscriptionName}': {messageBody}");
log.LogInformation($"Identifiant du message : {messageId}");
log.LogInformation($"Date d'envoi du message : {enqueuedTimeUtc}");
}

private static async Task SaveMessageToADLS(string messageId, DateTime enqueuedTimeUtc, string messageJson, string accountName, string fileSystemName, string topicName, string environment, string storageConnectionString, ILogger log, ExecutionContext context)
{
// Configuration de l'accès à ADLS Gen2
string year = enqueuedTimeUtc.Year.ToString();
string month = enqueuedTimeUtc.Month.ToString("D2");
string day = enqueuedTimeUtc.Day.ToString("D2");

string directoryName = $"{environment}/silver/{topicName}/{year}/{month}/{day}"; // Utiliser la structure : environment/silver/topicName/année/mois/jour
string fileName = $"{messageId}_{enqueuedTimeUtc:yyyyMMddHHmmss}.json"; // Nom du fichier basé sur l'identifiant du message et la date d'envoi

// Créer un client pour accéder à l'ADLS Gen2
DataLakeFileSystemClient fileSystemClient = new DataLakeFileSystemClient(storageConnectionString, accountName, fileSystemName);

// Créer un répertoire s'il n'existe pas déjà
await fileSystemClient.CreateDirectoryAsync(directoryName);

// Créer ou écrire dans le fichier avec le contenu du message JSON
DataLakeFileClient fileClient = fileSystemClient.GetFileClient($"{directoryName}/{fileName}");
using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(messageJson)))
{
await fileClient.UploadAsync(stream, true);
}
}
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.