Huginn (Huginn v0.6.0)

Copy Markdown View Source

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: 5

Several 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

attach_default_logger(level \\ :info)

@spec attach_default_logger(Logger.level()) :: :ok | {:error, :already_exists}

Attaches the built-in Logger handler for Huginn telemetry events.

See Huginn.Clickhouse.Telemetry.attach_default_logger/1.

await_ready(pool_name \\ Config.default_pool(), timeout \\ 10000)

@spec await_ready(atom(), pos_integer()) :: :ok | {:error, Huginn.Error.t()}

Blocks until the pool has a connected channel, or timeout milliseconds pass.

See Huginn.Clickhouse.Pool.await_ready/2.

cancel(query_id, opts \\ [])

@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.

cancel_where(condition, opts \\ [])

@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.

child_spec(opts)

@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.

detach_default_logger()

@spec detach_default_logger() :: :ok | {:error, :not_found}

Detaches the built-in telemetry logger.

See Huginn.Clickhouse.Telemetry.detach_default_logger/0.

insert(sql, data, opts \\ [])

Inserts already encoded data using a single request.

See Huginn.Clickhouse.Client.insert/3 for options.

insert_stream(sql, data, opts \\ [])

Inserts an enumerable of rows or encoded chunks as a stream.

See Huginn.Clickhouse.Client.insert_stream/3 for options.

ping(opts \\ [])

@spec ping(Huginn.Clickhouse.Client.query_opts()) :: :ok | {:error, Huginn.Error.t()}

Pings the ClickHouse server.

See Huginn.Clickhouse.Client.ping/1 for options.

query(sql, opts \\ [])

Executes a query and returns the result.

See Huginn.Clickhouse.Client.query/2 for options.

query!(sql, opts \\ [])

Executes a query and raises Huginn.Error on failure.

See Huginn.Clickhouse.Client.query!/2 for options.

running_queries(opts \\ [])

@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.

start_link(opts)

@spec start_link(keyword() | map() | Huginn.Clickhouse.Config.t()) ::
  Supervisor.on_start()

Starts a connection pool linked to the caller.

See Huginn.Clickhouse.Pool.start_link/1.

stream_io(sql, input, opts \\ [])

Runs a query over streamed input and returns a lazy stream of result chunks.

See Huginn.Clickhouse.Client.stream_io/3 for options.

stream_maps(sql, opts \\ [])

Streams rows as maps from a query result. Raises Huginn.Error on failure.

See Huginn.Clickhouse.Client.stream_maps/2 for options.

stream_query(sql, opts \\ [])

Executes a query and returns a lazy stream of result chunks.

See Huginn.Clickhouse.Client.stream_query/2 for options.

stream_rows(sql, opts \\ [])

Streams rows from a query result. Raises Huginn.Error on failure.

See Huginn.Clickhouse.Client.stream_rows/2 for options.