Skip to content

Reading functions in the Tributary DuckDB extension

Function category

Reading

1 function

tributary_scan_topic

Table function Reading
Signature
tributary_scan_topic( topic_name: VARCHAR, ... := VARCHAR, bootstrap.servers := VARCHAR, group.id := VARCHAR, sasl.mechanism := VARCHAR, sasl.password := VARCHAR, sasl.username := VARCHAR, schema.registry.url := VARCHAR, security.protocol := VARCHAR ) → TABLE
Arguments
Argument topic_name Type VARCHAR Mode Positional Description Kafka topic to read. Write it as kafka://<name>/<topic> to use the tributary_cluster secret scoped to kafka://<name>.
Argument bootstrap.servers Type VARCHAR Mode Named Description Comma-separated host:port list of brokers to bootstrap from. Required unless it comes from a tributary_cluster secret.
Argument group.id Type VARCHAR Mode Named Description Consumer group id passed to librdkafka. The scan reads explicit partition ranges and never commits offsets, so it doesn't move a group's position.
Argument security.protocol Type VARCHAR Mode Named Description PLAINTEXT, SSL, SASL_PLAINTEXT, or SASL_SSL. See Kafka security.
Argument sasl.mechanism Type VARCHAR Mode Named Description SASL mechanism — PLAIN, SCRAM-SHA-256, SCRAM-SHA-512, etc. See Kafka SASL.
Argument sasl.username Type VARCHAR Mode Named Description SASL username (PLAIN / SCRAM).
Argument sasl.password Type VARCHAR Mode Named Description SASL password (PLAIN / SCRAM). Keep it in a tributary_cluster secret, or pass it with getvariable() from a variable set with getenv(), rather than inlining it.
Argument schema.registry.url Type VARCHAR Mode Named Description Schema Registry URL. When set, the latest JSON schemas of the <topic>-key and <topic>-value subjects are used to return key and value as JSON. Avro and Protobuf subjects are an error.
Argument ... Type VARCHAR Mode Named Description Varargs Any other librdkafka global (client) property from its CONFIGURATION reference — ssl.ca.location, client.id, debug, etc. — plus schema.registry.url to decode JSON-schema topics. Names are checked when the query is bound (a misspelling is a Binder Error); topic-level properties such as auto.offset.reset are not accepted. Values pass to librdkafka unchanged.
Returns

A table with the following columns:

Column topic Type VARCHAR Description Topic the message was read from.
Column partition Type INTEGER Description Kafka partition id the message lives in.
Column offset Type BIGINT Description Per-partition offset of the message.
Column key Type BLOB Description Raw message key, NULL when the message has none. JSON when schema.registry.url is set and the <topic>-key subject has a JSON schema.
Column message Type BLOB Description Raw message payload. Use decode(message) for UTF-8 text such as JSON (not message::VARCHAR, which escapes quotes in DuckDB 2.0). When schema.registry.url is set and the <topic>-value subject has a JSON schema, this column is named value and has type JSON.
Description
1 Snapshot a topic into a local table
CREATE OR REPLACE TABLE orders_snapshot AS
SELECT *
FROM tributary_scan_topic('orders',
                          "bootstrap.servers" := 'kafka:9092');
2 Decode JSON messages on the way in
SELECT
  partition,
  "offset",
  json_extract_string(decode(message), '$.user_id') AS user_id,
  json_extract_string(decode(message), '$.event')   AS event
FROM tributary_scan_topic('events',
                          "bootstrap.servers" := 'kafka:9092');
3 SASL-authenticated consume through a cluster secret
CREATE SECRET prod_kafka (
  TYPE tributary_cluster,
  SCOPE 'kafka://prod',
  "bootstrap.servers" 'broker.example.com:9093',
  "security.protocol" 'SASL_SSL',
  "sasl.mechanism"    'SCRAM-SHA-512',
  "sasl.username"     getenv('KAFKA_USER'),
  "sasl.password"     getenv('KAFKA_PASSWORD')
);

SELECT * FROM tributary_scan_topic('kafka://prod/orders');