Skip to content

This nuget package help you to use Octonica.ClickHouseClient with connection pool. You can fully control the concurrent query with "select" I/O mode.

License

Notifications You must be signed in to change notification settings

anyone-developer/anyone-dotnet-use-clickhouse-connection-pool

Repository files navigation

anyone-dotnet-use-clickhouse-connection-pool

This nuget package help you to use Octonica.ClickHouseClient with connection pool. You can fully control the concurrent query with "select" I/O mode. The original client repo is here: https://github.com/Octonica/ClickHouseClient

This repo would bundle nuget package. Anyone can reference it from nuget.org.

nuget package: https://www.nuget.org/packages/AnyoneDeveloper.ClickHouse.ConnectionPool/

If you like my module, please buy me a coffee.

More and more tiny and useful GitHub action modules are on the way. Please donate to me. I accept a part-time job contract. if you need, please contact me: [email protected]

How to use

Inject referenced class

  • Add AnyoneDeveloper.ClickHouse.ConnectionPool package reference.
  • Inject Repository and Connection class
    • Inject Repository class
      services.AddScoped<IClickHouseRepository, ClickHouseRepository>();
    • Inject Connection class
      services.AddSingleton(x => new ClickHouseConnection(_configuration.GetValue<int>("Connection:Count"), _configuration.GetValue<string>("ConnectionStrings:SRV"))
           .InitSemaphore(_configuration.GetValue<int>("Connection:InitialConcurrentCount"), _configuration.GetValue<int>("Connection:MaximumConcurrentCount")));

Set up configuration

  • Set up connection pool size It should greater than concurrent size.
  • Set up SemaphoreSlim InitCount and MaxCount

Define execution method

  private static async Task<IEnumerable<Foo>> GetSomeDataAsync(ClickHouseConnection conn, string sql)
  {
      using var cmd = conn.CreateCommand(sql);
      var items = new List<Foo>();
      using (var reader = await cmd.ExecuteReaderAsync())
      {
          while (await reader.ReadAsync())
          {
              var item = new Foo();
              item.ID = await reader.GetFieldValueAsync<int>(0);
              item.Name = await reader.GetFieldValueAsync<string>(1);
              item.Job = await reader.GetFieldValueAsync<string>(2);

              items.Add(item);
          }
      }
      return items;
  }

Use it as below code:

  var sql = @"SELECT ID, Name, Job FROM one_table";
  var data = repository.GetResultAsync((conn) => GetSomeDataAsync(conn, sql));

You can reference complete code from Demo

Donation

PalPal: https://paypal.me/nzhang4

About

This nuget package help you to use Octonica.ClickHouseClient with connection pool. You can fully control the concurrent query with "select" I/O mode.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

Packages

No packages published

Languages