# QuestDB Sink

> Write messages from Iggy streams to QuestDB tables over QWP, QuestDB's native binary protocol.

Rendered page: https://iggy.apache.org/docs/connectors/sinks/questdb/

Source: https://github.com/apache/iggy-connectors/blob/master/docs/sinks/questdb.mdx

Docs version: the upcoming Apache Iggy release, from the master branches of apache/iggy and apache/iggy-connectors

The QuestDB sink connector consumes messages from Iggy streams and writes them to [QuestDB](https://questdb.com) over QWP, QuestDB's native binary protocol, on a WebSocket transport. Each field of a JSON payload becomes a typed column. The same plugin works with QuestDB open source and QuestDB Enterprise.

This page is a curated subset of the documentation. The canonical reference is the [questdb_sink README](https://github.com/apache/iggy-connectors/tree/master/sinks/questdb_sink) in the `apache/iggy-connectors` repository.

The sink requires QuestDB 10.0 or QuestDB Enterprise 4.0, or newer. `ack_level = "local_durable"` requires QuestDB 10.2.0 or QuestDB Enterprise 4.2.1.

## Configuration

Use the broker credentials and runtime setup from the [sink guide](https://iggy.apache.org/docs/connectors/sinks/sink#configuration). From the repository root, build the plugin:

```bash
cargo build --release -p iggy_connector_questdb_sink
```

The sample expects a local QuestDB with its default HTTP port. Save the configuration below in the runtime's connector directory. QuestDB creates the table and its columns from the first rows it receives.

```toml
type = "sink"
key = "questdb"
enabled = true
version = 0
name = "QuestDB sink"
path = "target/release/libiggy_connector_questdb_sink"
plugin_config_format = "toml"

[[streams]]
stream = "events"
topics = ["questdb_events"]
schema = "json"
batch_length = 1000
consumer_group = "questdb_sink"

[delivery]
guarantee = "at_least_once"

[plugin_config]
connection_string = "ws::addr=localhost:9000;"
table = "iggy_events"
timestamp_source = "message"
symbol_columns = ["region", "device"]
flush_timeout = "30s"
batch_size = 1000
```

Create the Iggy stream and topic, then send a JSON row:

```bash
./target/release/iggy --username iggy --password iggy stream create events
./target/release/iggy --username iggy --password iggy topic create events questdb_events 1 none 1d
./target/release/iggy --username iggy --password iggy message send --partition-id 0 events questdb_events '{"region":"eu","device":"d1","temperature":21.5}'
```

Credentials, TLS, store-and-forward, reconnect and multi-host failover are set in the connect string. Use `wss::` for TLS. See the QuestDB [connect string reference](https://questdb.com/docs/connect/clients/connect-string/).

### Options

| Option | Type | Default | Description |
| ------ | ---- | ------- | ----------- |
| `connection_string` | string | **required** | QuestDB connect string, `ws::` or `wss::`. Treated as a secret |
| `table` | string | **required** | Target table |
| `timestamp_source` | string | `message` | Source of the designated timestamp: `message`, `origin`, `payload`, or `server` |
| `timestamp_field` | string | none | Payload field that holds the timestamp as a number. Required with `timestamp_source = "payload"`, and rejected with any other source |
| `timestamp_unit` | string | `auto` | Unit of the payload timestamp: `auto`, `seconds`, `millis`, `micros`, or `nanos` |
| `symbol_columns` | array | `[]` | Payload fields stored as `SYMBOL` |
| `uuid_columns` | array | `[]` | Payload fields stored as `UUID` |
| `integer_columns` | array | `[]` | Payload fields stored as `LONG` |
| `include_stream_column` | bool | `true` | Write the stream name to `iggy_stream` |
| `include_topic_column` | bool | `true` | Write the topic name to `iggy_topic` |
| `include_partition_column` | bool | `false` | Write the partition to `iggy_partition_id` |
| `include_offset_column` | bool | `false` | Write the offset to `iggy_offset` |
| `include_headers` | bool | `false` | Write each header to a `header_<name>` column |
| `ack_level` | string | from the connect string | `ok`, `durable`, or `local_durable`. If unset, `request_durable_ack` in the connect string decides |
| `flush_timeout` | string | `30s` | How long a chunk waits for its acknowledgement without progress |
| `batch_size` | u32 | `1000` | Maximum rows per chunk |
| `max_flush_bytes` | usize | `1000000` | Flush when the buffer reaches this size, even before `batch_size` rows |
| `numbers_as_double` | bool | `true` | Write every JSON number as a `DOUBLE` |
| `log_rejected_payload` | bool | `false` | Add a truncated payload to the log line of a rejected record |
| `verbose_logging` | bool | `false` | Log per-batch details at info level instead of debug |

Unknown options and invalid values are rejected at startup, and one start reports all of them.

## Types

| JSON | QuestDB |
| ---- | ------- |
| string | `VARCHAR`, or `SYMBOL` / `UUID` when listed in `symbol_columns` / `uuid_columns` |
| number | `DOUBLE`, or `LONG` when listed in `integer_columns` |
| boolean | `BOOLEAN` |
| array of numbers | `DOUBLE[]`, up to 3 dimensions, when every row of a dimension has the same length |
| any other array | not supported, the record is rejected |
| object | `VARCHAR` holding the JSON text |
| `null`, empty array | column omitted |

A payload that is not a JSON object goes into one `VARCHAR` column named `payload`.

An auto-created table names its designated timestamp `timestamp`. A payload field with the same name collides with it, and QuestDB rejects the frame. Create the table first with another timestamp column, or remove the field with the `delete_fields` transform.

## Delivery Semantics

The delivery guarantee is set in the `[delivery]` table of the connector, not in `plugin_config`. With `guarantee = "at_least_once"`, the runtime stores the offsets of a batch only after QuestDB acknowledged every row of it at `ack_level`, and delivers a failed batch again. Use it for every table that must not lose rows. It is also how the official QuestDB clients work. On a disconnect or a failover they send again the rows that the server did not acknowledge yet, and the server removes the duplicates. Without the `[delivery]` table the runtime delivers at most once, which is best effort: a failed write loses the messages of its batch from the first one that QuestDB did not confirm to the end of the batch, and the sink only logs their offsets. The chunks that QuestDB confirmed before the failure stay written.

A retried batch writes the rows that QuestDB already took a second time. You can use the sink without `DEDUP`, and the table then holds those rows twice, which may or may not be a problem for your data. If duplicates do not matter to you, leave `DEDUP` out: ingestion is faster and lighter for the server without it. To keep one copy, let QuestDB remove the duplicates by the `DEDUP` keys of the table. Create the table with keys that identify a row: the designated timestamp, plus the columns of your payload that are unique for it, for example an order id.

```sql
CREATE TABLE orders (
  order_id SYMBOL, amount DOUBLE, timestamp TIMESTAMP
) TIMESTAMP(timestamp) PARTITION BY DAY WAL
DEDUP SKIP KEYS(timestamp, order_id);
```

`DEDUP SKIP KEYS` keeps the first row and needs QuestDB 10.2.0 or QuestDB Enterprise 4.2.1. `DEDUP UPSERT KEYS` replaces the stored row with the later one. With `timestamp_source = "server"`, each copy gets a new timestamp, so the keys cannot match it.

Only if the payload has no unique columns, fall back to the message identity: set `include_partition_column = true` and `include_offset_column = true`, which are off by default, and use `DEDUP SKIP KEYS(timestamp, iggy_stream, iggy_topic, iggy_partition_id, iggy_offset)`.

A record that the sink cannot write is a rejected record, for example one with a value of the wrong type in a listed column. At least once, it fails the batch with a permanent error that names its offset, and the runtime stops the delivery there until an operator skips the record. At most once, the record is logged and the rest of the batch is written.

## Acknowledgements

- **`ok`**: QuestDB accepted the rows.
- **`local_durable`**: QuestDB flushed the rows to its own disk, so they survive a power loss on the QuestDB host. It needs `request_durable_ack=local` in the connect string and `cairo.commit.mode=adaptive` on the server. It works with QuestDB open source.
- **`durable`**: QuestDB Enterprise shipped the write-ahead log of the rows, so they survive a failover. It needs `request_durable_ack=on`, `replicated`, `local,replicated` or `replicated,local` in the connect string and replication on the server.

If `ack_level` is unset, `request_durable_ack` in the connect string selects the level.

With `sf_dir` in the connect string, the client keeps unacknowledged frames on disk across disconnects and restarts.

## Transforms

Transforms can be applied before writing to QuestDB. See the [transforms documentation](https://iggy.apache.org/docs/connectors/transforms) for the available types and their configuration.
