ClickHouse client for Elixir using gRPC.
Huginn provides a simple interface for querying ClickHouse databases over gRPC with connection pooling and streaming support.
Setup
Add a pool to your supervision tree:
children = [
{Huginn,
host: "localhost",
port: 9100,
database: "default",
auth: {:password, "default", ""},
pool_size: 5}
]or let Huginn's own application start it from config:
config :huginn, :clickhouse,
host: "localhost",
port: 9100,
database: "default",
auth: {:password, "default", ""},
pool_size: 5Several pools can run side by side under different :pool_names. Select
one per call with the :pool option. See Huginn.Clickhouse.Pool.
Usage
# Simple query
{:ok, result} = Huginn.query("SELECT * FROM system.tables LIMIT 10")
# Get results as maps
maps = Huginn.Clickhouse.Result.to_maps(result)
# Insert pre-encoded data
{:ok, _} = Huginn.insert("INSERT INTO users (name, age) FORMAT TabSeparated", "john\t25\n")
# Insert rows, streamed and encoded for you
{:ok, _} =
Huginn.insert_stream("INSERT INTO users (name, age) FORMAT TabSeparated", [
["john", 25],
["jane", 30]
])
# Stream large results row by row
"SELECT * FROM large_table"
|> Huginn.stream_rows()
|> Enum.each(&process_row/1)Errors
Failures are returned as {:error, %Huginn.Error{}}. Bang and row-streaming
functions raise Huginn.Error. See Huginn.Error for the reasons.
Telemetry
Requests emit [:huginn, :query, :start | :stop | :exception] events, plus
[:huginn, :query, :retry] before each retry. Attach the built-in logger
with attach_default_logger/1, or your own handler. See
Huginn.Clickhouse.Telemetry for the full event reference.
Retries
All request functions accept :retries, :retry_backoff and
:retry_max_backoff to retry connection failures with capped, jittered
exponential backoff. Retries are off by default. ClickHouse errors and
timeouts are never retried, and inserts are never re-sent. See
Huginn.Clickhouse.Retry.
Summary
Functions
Attaches the built-in Logger handler for Huginn telemetry events.
Blocks until the pool has a connected channel, or timeout milliseconds
pass.
Cancels a running query by ID.
Cancels queries matching a condition. condition must be trusted input.
Returns a child spec that starts a connection pool.
Detaches the built-in telemetry logger.
Inserts already encoded data using a single request.
Inserts an enumerable of rows or encoded chunks as a stream.
Pings the ClickHouse server.
Executes a query and returns the result.
Executes a query and raises Huginn.Error on failure.
Lists currently running queries.
Starts a connection pool linked to the caller.
Runs a query over streamed input and returns a lazy stream of result chunks.
Streams rows as maps from a query result. Raises Huginn.Error on failure.
Executes a query and returns a lazy stream of result chunks.
Streams rows from a query result. Raises Huginn.Error on failure.
Functions
@spec attach_default_logger(Logger.level()) :: :ok | {:error, :already_exists}
Attaches the built-in Logger handler for Huginn telemetry events.
@spec await_ready(atom(), pos_integer()) :: :ok | {:error, Huginn.Error.t()}
Blocks until the pool has a connected channel, or timeout milliseconds
pass.
@spec cancel(String.t(), Huginn.Clickhouse.Client.query_opts()) :: :ok | {:error, Huginn.Error.t()}
Cancels a running query by ID.
See Huginn.Clickhouse.Client.cancel/2 for options.
@spec cancel_where(String.t(), Huginn.Clickhouse.Client.query_opts()) :: :ok | {:error, Huginn.Error.t()}
Cancels queries matching a condition. condition must be trusted input.
See Huginn.Clickhouse.Client.cancel_where/2 for options.
@spec child_spec(keyword() | map() | Huginn.Clickhouse.Config.t()) :: Supervisor.child_spec()
Returns a child spec that starts a connection pool.
Takes the options of Huginn.Clickhouse.Config.new/1. See
Huginn.Clickhouse.Pool.child_spec/1.
@spec detach_default_logger() :: :ok | {:error, :not_found}
Detaches the built-in telemetry logger.
@spec insert(String.t(), binary(), Huginn.Clickhouse.Client.query_opts()) :: {:ok, Huginn.Clickhouse.Result.t()} | {:error, Huginn.Error.t()}
Inserts already encoded data using a single request.
See Huginn.Clickhouse.Client.insert/3 for options.
@spec insert_stream(String.t(), Enumerable.t(), Huginn.Clickhouse.Client.query_opts()) :: {:ok, Huginn.Clickhouse.Result.t()} | {:error, Huginn.Error.t()}
Inserts an enumerable of rows or encoded chunks as a stream.
See Huginn.Clickhouse.Client.insert_stream/3 for options.
@spec ping(Huginn.Clickhouse.Client.query_opts()) :: :ok | {:error, Huginn.Error.t()}
Pings the ClickHouse server.
See Huginn.Clickhouse.Client.ping/1 for options.
@spec query(String.t(), Huginn.Clickhouse.Client.query_opts()) :: {:ok, Huginn.Clickhouse.Result.t()} | {:error, Huginn.Error.t()}
Executes a query and returns the result.
See Huginn.Clickhouse.Client.query/2 for options.
@spec query!(String.t(), Huginn.Clickhouse.Client.query_opts()) :: Huginn.Clickhouse.Result.t()
Executes a query and raises Huginn.Error on failure.
See Huginn.Clickhouse.Client.query!/2 for options.
@spec running_queries(Huginn.Clickhouse.Client.query_opts()) :: {:ok, Huginn.Clickhouse.Result.t()} | {:error, Huginn.Error.t()}
Lists currently running queries.
See Huginn.Clickhouse.Client.running_queries/1 for options.
@spec start_link(keyword() | map() | Huginn.Clickhouse.Config.t()) :: Supervisor.on_start()
Starts a connection pool linked to the caller.
@spec stream_io(String.t(), Enumerable.t(), Huginn.Clickhouse.Client.query_opts()) :: Enumerable.t(Huginn.Clickhouse.Client.stream_element())
Runs a query over streamed input and returns a lazy stream of result chunks.
See Huginn.Clickhouse.Client.stream_io/3 for options.
@spec stream_maps(String.t(), Huginn.Clickhouse.Client.query_opts()) :: Enumerable.t(map())
Streams rows as maps from a query result. Raises Huginn.Error on failure.
See Huginn.Clickhouse.Client.stream_maps/2 for options.
@spec stream_query(String.t(), Huginn.Clickhouse.Client.query_opts()) :: Enumerable.t(Huginn.Clickhouse.Client.stream_element())
Executes a query and returns a lazy stream of result chunks.
See Huginn.Clickhouse.Client.stream_query/2 for options.
@spec stream_rows(String.t(), Huginn.Clickhouse.Client.query_opts()) :: Enumerable.t(Huginn.Clickhouse.Result.row())
Streams rows from a query result. Raises Huginn.Error on failure.
See Huginn.Clickhouse.Client.stream_rows/2 for options.