Azure Data Explorer (ADX) is a fast and highly scalable data analytics service provided by Microsoft Azure. It is designed to help users quickly ingest, analyze, and visualize large volumes of structured and unstructured data from various sources. In this blog post, we will walk through the process of ingesting JSON data into ADX using Azure Functions and .NET SDK.
There are several ways to ingest data into ADX, including batch ingestion, streaming ingestion, and continuous data ingestion. In our example, we will be using the .NET SDK in Azure Functions to ingest JSON data into Azure Data Explorer.
Refer to https://learn.microsoft.com/en-us/azure/data-explorer/ingest-json-formats?tabs=kusto-query-language
public class IngestJSON { private string IngestURI; private string DatabaseName; private string TableName; public IngestJSON(string ingestURI, string databaseName, string tableName) { IngestURI = ingestURI; DatabaseName = databaseName; TableName = tableName; } public async Task IngestJsonPayloadAsync(string jsonPayload) { byte[] jsonPayloadBytes = Encoding.ASCII.GetBytes(jsonPayload); long requestBodySize = jsonPayloadBytes.LongLength; var sourceOptions = new StreamSourceOptions { Size = requestBodySize, SourceId = Guid.NewGuid() }; var prop = CreateIngestionProperties(DataSourceFormat.multijson, "FlatEventMapping"); try { KustoConnectionStringBuilder connectionStringBuilder = new KustoConnectionStringBuilder(IngestURI).WithAadSystemManagedIdentity(); using (var client = KustoIngestFactory.CreateQueuedIngestClient(connectionStringBuilder)) { var result = await client.IngestFromStreamAsync(new MemoryStream(jsonPayloadBytes), prop, sourceOptions); } } catch (Exception ex) { throw ex; } } private KustoIngestionProperties CreateIngestionProperties(DataSourceFormat dataFormat, string mappingName) { var kustoIngestionProperties = new KustoIngestionProperties() { DatabaseName = DatabaseName, TableName = TableName, IngestionMapping = new IngestionMapping() { IngestionMappingReference = mappingName }, Format = dataFormat, }; // Set the operation timeout kustoIngestionProperties.AdditionalProperties.Add("OperationTimeoutMs", "60000"); return kustoIngestionProperties; } }
To handle JSON arrays and store each object in the array as a separate row in the events table, follow these steps:
In conclusion, this blog post has demonstrated how to ingest JSON data into Azure Data Explorer using Azure Functions and .NET SDK. By following the steps outlined above, you can easily ingest, analyze, and visualize JSON data from various sources in Azure Data Explorer.
Legal Stuff