Skip to content

plausible/ch

Repository files navigation

Ch

Documentation badge Hex.pm badge Coveralls

HTTP ClickHouse client for Elixir.

Used in Ecto ClickHouse adapter.

Key features

  • RowBinary
  • Native query parameters
  • Per query settings

Installation

defp deps do
  [
    {:ch, "~> 0.9.0"}
  ]
end

Usage

Start a pool:

{:ok, pool} = Ch.start_link(url: "http://localhost:8123")

Run a query with named ClickHouse parameters:

%Ch.Result{names: ["number"], rows: [[0], [1] | _rest]} =
  Ch.query!(
    pool,
    "select number from numbers({limit:UInt32})",
    %{"limit" => 100},
    headers: [{"accept-encoding", "zstd"}]
  )

Insert RowBinary data by encoding it explicitly:

rows = [[1, "one"], [2, "two"]]
types = ["UInt8", "String"]
rowbinary = Ch.RowBinary.encode_rows(rows, types)

Ch.query!(pool, [
  "INSERT INTO events FORMAT RowBinary\n",
  rowbinary
])

Compressed inserts use the same shape, with the whole request body compressed:

names = ["id", "name"]
types = ["UInt8", "String"]
rows = [[1, "one"], [2, "two"]]

payload =
  :zstd.compress([
    "INSERT INTO events FORMAT RowBinaryWithNamesAndTypes\n",
    Ch.RowBinary.encode_names_and_types(names, types),
    Ch.RowBinary.encode_rows(rows, types)
  ])

Ch.query!(
  pool,
  payload,
  %{},
  headers: [{"content-encoding", "zstd"}]
)

About

HTTP ClickHouse client for Elixir

Resources

License

Stars

Watchers

Forks

Contributors

Languages