private void CreateMergeKustoTable(ICslAdminProvider admin, IDictionary <string, object> value)
        {
            TableSchema tableSchema = new TableSchema(TableName);

            foreach (var pair in value)
            {
                tableSchema.AddColumnIfMissing(new ColumnSchema(pair.Key, _columnType[pair.Value != null ? pair.Value.GetType() : typeof(string)]));
            }

            string createTable = CslCommandGenerator.GenerateTableCreateMergeCommand(tableSchema);

            admin.ExecuteControlCommand(createTable);

            string enableIngestTime = CslCommandGenerator.GenerateIngestionTimePolicyAlterCommand(TableName, true);

            admin.ExecuteControlCommand(enableIngestTime);
        }
示例#2
0
        private Task WriteTablesAsync(
            string outputFolder,
            IEnumerable <TableSchema> tables,
            IReadOnlyDictionary <string, IngestionMapping[]> ingestionMappingLookup,
            CancellationToken cancellationToken)
        {
            return(WriteSchemaToFileAsync(
                       Path.Combine(outputFolder, "Tables"),
                       tables.ToArray(),
                       t => t.Name,
                       t => t.Folder,
                       t =>
            {
                // Add new lines so we generate a file that will diff nicely in source control
                var createOrMergeTableCommand = CslCommandGenerator
                                                .GenerateTableCreateMergeCommand(t)
                                                .Replace("(", "(\n  ")
                                                .Replace(", ", ",\n  ")
                                                .Replace(")", "\n)");

                var builder = new StringBuilder();
                builder.AppendLine(createOrMergeTableCommand);

                if (ingestionMappingLookup.TryGetValue(t.Name, out var ingestionMappings))
                {
                    foreach (var ingestionMapping in ingestionMappings.OrderBy(m => m.Kind).ThenBy(m => m.Name))
                    {
                        builder.AppendLine();
                        builder.AppendLine();
                        builder.AppendLine($".create-or-alter table {t.Name} ingestion {ingestionMapping.Kind.ToLower()} mapping \"{ingestionMapping.Name}\"");
                        builder.AppendLine("'['");
                        for (var i = 0; i < ingestionMapping.ColumnMappings.Count; i++)
                        {
                            var columnMapping = ingestionMapping.ColumnMappings[i];
                            builder.Append($"'  {JsonConvert.SerializeObject(columnMapping, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore })}");
                            builder.Append(i == ingestionMapping.ColumnMappings.Count - 1 ? "" : ",");
                            builder.AppendLine("'");
                        }
                        builder.AppendLine("']'");
                    }
                }

                return builder.ToString();
            },
                       cancellationToken));
        }