Beispiel #1
0
        private static KustoConnectionStringBuilder CreateKustoConnectionStringBuilder(
            Uri clusterUri,
            TokenProviderParameterization tokenProvider)
        {
            var builder = new KustoConnectionStringBuilder(clusterUri.ToString());

            if (tokenProvider.Login != null)
            {
                return(builder.WithAadApplicationKeyAuthentication(
                           tokenProvider.Login.ClientId,
                           tokenProvider.Login.Secret,
                           tokenProvider.Login.TenantId));
            }
            else if (tokenProvider.Tokens != null)
            {
                var token = tokenProvider.Tokens.Values
                            .Where(t => t.ClusterUri != null)
                            .Where(t => t.ClusterUri !.Trim().ToLower() == clusterUri.ToString().Trim().ToLower())
                            .Select(t => t.Token)
                            .FirstOrDefault();

                if (token != null)
                {
                    return(builder.WithAadUserTokenAuthentication(token));
                }
                else
                {
                    throw new DeltaException($"No token was provided for {clusterUri}");
                }
            }
            else if (tokenProvider.SystemManagedIdentity)
            {
                return(builder.WithAadSystemManagedIdentity());
            }
            else if (tokenProvider.UserManagedIdentity != null)
            {
                return(builder.WithAadUserManagedIdentity(tokenProvider.UserManagedIdentity.ClientId !));
            }
            else if (tokenProvider.UserPrompt != null)
            {
                return(builder.WithAadUserPromptAuthentication(
                           tokenProvider.UserPrompt.TenantId,
                           tokenProvider.UserPrompt.UserId));
            }
            else
            {
                throw new NotSupportedException("Token provider isn't supported");
            }
        }
        static void Main(string[] args)
        {
            if (!ParseCommandLine(args, out var cluster, out var database, out var table))
            {
                Usage();
                return;
            }

            var kcsb = new KustoConnectionStringBuilder
            {
                DataSource = cluster
            };

            kcsb = kcsb.WithAadUserPromptAuthentication();

            CreateJsonMappingIfNotExists(kcsb, database, table);

            // Do ingestion using Kusto.Data client library
            using (var siClient = KustoClientFactory.CreateCslStreamIngestClient(kcsb))
            {
                using (var data = CreateSampleEventLogCsvStream(10))
                {
                    siClient.ExecuteStreamIngest(database, table, data);
                }

                using (var data = CreateSampleEventLogJsonStream(10))
                {
                    siClient.ExecuteStreamIngestAsync(
                        database,
                        table,
                        data,
                        null,
                        Kusto.Data.Common.DataSourceFormat.json,
                        compressStream: false,
                        mappingName: s_jsonMappingName);
                }
            }

            // Do ingestion using Kusto.Ingest client library. The data still goes directly to the engine cluster
            // Just a convenience for applications already using IKustoIngest interface
            using (var ingestClient = KustoIngestFactory.CreateStreamingIngestClient(kcsb))
            {
                using (var data = CreateSampleEventLogCsvStream(10))
                {
                    var ingestProperties = new KustoIngestionProperties(database, table)
                    {
                        Format = DataSourceFormat.csv,
                    };
                    ingestClient.IngestFromStreamAsync(data, ingestProperties);
                }

                using (var data = CreateSampleEventLogJsonStream(10))
                {
                    var ingestProperties = new KustoIngestionProperties(database, table)
                    {
                        Format = DataSourceFormat.json,
                        JSONMappingReference = s_jsonMappingName
                    };

                    ingestClient.IngestFromStreamAsync(data, ingestProperties);
                }
            }
        }