Skip to content

Microsoft .NET (Octonica)

Overview⚓︎

Query your Hydrolix cluster with .NET and the Octonica ClickHouse client.

Prerequisites⚓︎

Before you begin, be sure to have the following:

  • The .NET SDK installed, version 6.0 or higher
  • The Octonica ClickHouse client added to the project
  • A working Hydrolix cluster
  • The host address, port, and database name
  • A valid username and password for authentication
  • A local environment that allows outbound connections to the ClickHouse native interface port; default is 9440 with TLS

Query with .NET example⚓︎

This example sets TLSMode=Require to enforce a secure connection. It connects with a Hydrolix cluster, runs a query, and outputs the results.

Replace the following fields with your cluster information:

  • Host
  • Database
  • User
  • Password

    using System;
    using System.Threading.Tasks;
    using Octonica.ClickHouseClient;
    using Octonica.ClickHouseClient.Exceptions;
    
    internal static class Program
    {
        public static async Task Main()
        {
            var connStr =
                "Host=<your-cluster-host>;" +
                "Port=9440;" +
                "TLSMode=Require;" +
                "Database=<your-database>;" +
                "User=<your-user>;" +
                "Password=<your-password>;";
    
            try
            {
                await using var conn = new ClickHouseConnection(connStr);
                await conn.OpenAsync();
    
                // Optional: Set logging level to reduce noise
                await using (var s = conn.CreateCommand("SET send_logs_level='fatal'"))
                    await s.ExecuteNonQueryAsync();
    
                const string sql = @"
    SELECT toStartOfHour(timestamp), count()
    FROM   logs
    WHERE  timestamp >= now() - INTERVAL 1 DAY
    GROUP  BY 1
    ORDER  BY 1 DESC";
    
                await using var cmd = conn.CreateCommand(sql);
                await using var r = await cmd.ExecuteReaderAsync();
    
                while (await r.ReadAsync())
                {
                    for (int i = 0; i < r.FieldCount; i++)
                    {
                        Console.Write(i == 0 ? r.GetValue(i) : $"\t{r.GetValue(i)}");
                    }
                    Console.WriteLine();
                }
            }
            catch (ClickHouseException db)
            {
                Console.Error.WriteLine($"Hydrolix/ClickHouse: {db.Message}");
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine(ex);
            }
        }
    }