Esempio n. 1
0
        public static async Task Run(
            // NOTE: If value is formatted as an '%environmentVariable%', then it will read the value at runtime
            [EventHubTrigger(
                 "", // Not used if the name is in the connection string
                 Connection = Config.EventHubConnectionStringConfigField,
                 ConsumerGroup = Config.ConsumerGroupEnvironmentVariable)] EventData[] messages,
            [CosmosDB(
                 databaseName: Config.CosmosDbIdEnvironmentVariable,
                 collectionName: Config.CosmosDbCollectionEnvironmentVariable,
                 CreateIfNotExists = true,
                 ConnectionStringSetting = Config.CosmosDbConnectionStringConfigField,
                 PartitionKey = "/client_id",
                 UseMultipleWriteLocations = false)] IAsyncCollector <string> collector,
            ILogger log)
        {
            foreach (var message in messages)
            {
                byte[] payload = message.Body.Array;

                if (message.IsGzipCompressed())
                {
                    payload = await BufferUtils.GzipDecompressToArray(message.Body.Array);
                }

                // Payload is expected to be a JSONL (one line per json record) formatted body
                var ev = Encoding.UTF8.GetString(payload);

                var split = ev.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries);

                if (Config.EnableCosmos)
                {
                    foreach (var line in split)
                    {
                        if (String.IsNullOrWhiteSpace(line))
                        {
                            continue;
                        }

                        try
                        {
                            await collector.AddAsync(line);
                        }
                        catch (Exception ex)
                        {
                            log.LogWarning(ex, "Exception occurred while inserting a document.\n" + ex.StackTrace);
                        }
                    }
                    log.LogDebug($"Inserted {split.Length} events.");
                }
            }
        }
        public static async Task Run(
            [EventHubTrigger("",
                             Connection = Config.EventHubConnectionStringConfigField,
                             ConsumerGroup = Config.ConsumerGroupEnvironmentVariable), ] EventData[] messages, ILogger log)
        {
            foreach (var message in messages)
            {
                byte[] payload = message.Body.Array;

                if (message.IsGzipCompressed())
                {
                    payload = await BufferUtils.GzipDecompressToArray(message.Body.Array);
                }

                // Payload is expected to be a JSONL (one line per json record) formatted body
                var ev = Encoding.UTF8.GetString(payload);

                var split = ev.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries);

                if (Config.EnableCosmos)
                {
                    foreach (var line in split)
                    {
                        if (String.IsNullOrWhiteSpace(line))
                        {
                            continue;
                        }

                        try
                        {
                            SimpleEvent simpleEvent = JsonConvert.DeserializeObject <SimpleEvent>(line);

                            await _container.CreateItemAsync <SimpleEvent>(
                                simpleEvent,
                                new PartitionKey(simpleEvent.ClientId),
                                new ItemRequestOptions
                            {
                                //optimize bandwidth for high write volume
                                EnableContentResponseOnWrite = false
                            });
                        }
                        catch (Exception ex)
                        {
                            log.LogWarning(ex, "Exception occurred while inserting a document.\n" + ex.StackTrace);
                        }
                    }
                    log.LogDebug($"Inserted {split.Length} events.");
                }
            }
        }