# `Huginn`
[🔗](https://github.com/nyo16/huginn/blob/v0.6.0/lib/huginn.ex#L1)

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_name`s. 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`.

# `attach_default_logger`

```elixir
@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`

```elixir
@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`

```elixir
@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`

```elixir
@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`

```elixir
@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`

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

Detaches the built-in telemetry logger.

See `Huginn.Clickhouse.Telemetry.detach_default_logger/0`.

# `insert`

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

# `insert_stream`

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

# `ping`

```elixir
@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`

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

# `query!`

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

# `running_queries`

```elixir
@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`

```elixir
@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`

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

# `stream_maps`

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

# `stream_query`

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

# `stream_rows`

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

---

*Consult [api-reference.md](api-reference.md) for complete listing*
