Exemple #1
0
    static async Task Main()
    {
        var masterAddresses = "localhost:7051,localhost:7151,localhost:7251";
        var tableName       = $"test_table_{Guid.NewGuid():N}";
        var numRows         = 10000;

        var loggerFactory = LoggerFactory.Create(builder => builder
                                                 .SetMinimumLevel(LogLevel.Trace)
                                                 .AddConsole());

        await using var client = KuduClient.NewBuilder(masterAddresses)
                                 .SetLoggerFactory(loggerFactory)
                                 .Build();

        var tableBuilder = new TableBuilder(tableName)
                           .AddColumn("host", KuduType.String, opt => opt.Key(true))
                           .AddColumn("metric", KuduType.String, opt => opt.Key(true))
                           .AddColumn("timestamp", KuduType.UnixtimeMicros, opt => opt.Key(true))
                           .AddColumn("value", KuduType.Double)
                           .SetNumReplicas(1)
                           .SetRangePartitionColumns("host", "metric", "timestamp");

        var table = await client.CreateTableAsync(tableBuilder);

        Console.WriteLine($"Created table {tableName}");

        var batches     = CreateRows(table, numRows).Chunk(2000);
        var writtenRows = 0;

        foreach (var batch in batches)
        {
            await client.WriteAsync(batch);

            writtenRows += batch.Length;
            Console.WriteLine($"Wrote {writtenRows} rows");
        }
    }