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:

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

Create a table and insert RowBinaryWithNamesAndTypes data:

session_id = "ch-demo-session"

Ch.query!(
  pool,
  "create temporary table demo(id UInt64, text String) engine Memory",
  %{},
  settings: %{"session_id" => session_id}
)

names = ["id", "text"]
types = ["UInt64", "String"]
rows = [[1, "one"], [2, "two"]]

insert = [
  "insert into demo format RowBinaryWithNamesAndTypes\n",
  Ch.RowBinary.encode_names_and_types(names, types)
  | Ch.RowBinary.encode_rows(rows, types)
]

Ch.query!(
  pool,
  :zstd.compress(insert),
  %{},
  settings: %{"session_id" => session_id},
  headers: [{"content-encoding", "zstd"}]
)

About

HTTP ClickHouse client for Elixir

Resources

License

Stars

Watchers

Forks

Contributors

Languages