SOURCE_STREAM()
All functions > SOURCE > SOURCE_STREAM()
A stream's broker and record format. Its records are declared with EXTERNAL_COLUMNS(... FROM TABLE(STREAM[topic])) and read only by a HOOK's ON.
Signatures
Returns: A stream for hooks
SOURCE_STREAM(
dsn
[ WITH ( FORMAT = 'json'
[, SECURITY_PROTOCOL = 'plaintext' | 'ssl' | 'sasl_plaintext' | 'sasl_ssl' ]
[, SASL_MECHANISM = 'plain' | 'scram-sha-256' | 'scram-sha-512', USERNAME = ..., PASSWORD = SECRET(...) ]
[, SSL_CA = ..., SSL_CERTIFICATE = ..., SSL_KEY = ..., SSL_KEY_PASSWORD = SECRET(...) ] ) ]
) → STREAMSOURCE sql
| Parameter | Type | Required | Description |
|---|---|---|---|
dsn | VARCHAR | Yes | kafka://broker:9092, or several brokers: kafka://a:9092,b:9092 |
format | VARCHAR | No | WITH property: 'json' (the default and only format for now) |
security_protocol | VARCHAR | No | WITH property: 'plaintext' (the default), 'ssl', 'sasl_plaintext' or 'sasl_ssl' |
sasl_mechanism | VARCHAR | No | WITH property, with a SASL protocol: 'plain', 'scram-sha-256' or 'scram-sha-512' |
username | VARCHAR | No | WITH property, with a SASL protocol: the SASL user |
password | VARCHAR | No | WITH property, with a SASL protocol: the SASL password, written as SECRET(...) so the registry never holds it |
ssl_ca | VARCHAR | No | WITH property, with a TLS protocol: the CA bundle's path on the serving host (the system store when absent) |
ssl_certificate | VARCHAR | No | WITH property, for mutual TLS: the client certificate's path |
ssl_key | VARCHAR | No | WITH property, for mutual TLS: the client key's path |
ssl_key_password | VARCHAR | No | WITH property, for mutual TLS: the client key's password, as SECRET(...) |
Examples
An event hook on a stream (SOURCE_STREAM)
FeatureQL
CREATE OR REPLACE FEATURES IN FM.TUTORIALS.HOOK_REF AS
PERSIST
stream := SOURCE_STREAM('kafka://broker:9092' WITH (FORMAT = 'json')),
payments := EXTERNAL_COLUMNS(
payment_id BIGINT,
merchant_id BIGINT,
amount DOUBLE,
event_time TIMESTAMP(3)
FROM TABLE(stream[payments])
),
payment_id := INPUT(BIGINT),
merchant_id := INPUT(BIGINT),
amount := INPUT(DOUBLE),
event_time := INPUT(TIMESTAMP(3)),
pay_row := ROW(
event_time AS event_time,
payment_id AS payment_id,
amount AS amount
),
minute_ref := ROW(
merchant_id AS merchant_id,
DATE_TRUNC(event_time, 'minute') AS minute
),
minute_close := (DATE_TRUNC(event_time, 'minute') + INTERVAL '70 SECOND')::TIMESTAMP(0),
pay_ps := PREPARED_STATEMENT(
pay_row
USING (payment_id, merchant_id, amount, event_time)
CAPTURE (minute_ref, minute_close)
),
pay_index := SINK(
'redis://redis:6379'
KEY 'pay:{' || merchant_id::VARCHAR || '}'
INDEXED ON event_time UNIQUE ON payment_id
WITH (
FORMAT = 'msgpack',
MODE = 'append',
SHAPE = 'sorted_set',
RETENTION = INTERVAL '1 DAY'
)
),
on_payment := HOOK(
RUN pay_ps
ON payments AS p
WHERE p[amount] > 0
USING (p[payment_id], p[merchant_id], p[amount], p[event_time])
TO pay_index
WITH (
START = 'earliest'
)
)
;Result
| feature_name VARCHAR | status VARCHAR | message VARCHAR |
|---|---|---|
| FM.TUTORIALS.HOOK_REF.AMOUNT | CREATED | Feature created as not exists |
| FM.TUTORIALS.HOOK_REF.EVENT_TIME | CREATED | Feature created as not exists |
| FM.TUTORIALS.HOOK_REF.MERCHANT_ID | CREATED | Feature created as not exists |
| FM.TUTORIALS.HOOK_REF.MINUTE_CLOSE | CREATED | Feature created as not exists |
| FM.TUTORIALS.HOOK_REF.MINUTE_REF | CREATED | Feature created as not exists |
| FM.TUTORIALS.HOOK_REF.ON_PAYMENT | CREATED | Feature created as not exists |
| FM.TUTORIALS.HOOK_REF.PAYMENTS | CREATED | Feature created as not exists |
| FM.TUTORIALS.HOOK_REF.PAYMENT_ID | CREATED | Feature created as not exists |
| FM.TUTORIALS.HOOK_REF.PAY_INDEX | CREATED | Feature created as not exists |
| FM.TUTORIALS.HOOK_REF.PAY_PS | CREATED | Feature created as not exists |
| FM.TUTORIALS.HOOK_REF.PAY_ROW | CREATED | Feature created as not exists |
| FM.TUTORIALS.HOOK_REF.STREAM | CREATED | Feature created as not exists |
A secured stream with credentials from secrets
FeatureQL
CREATE OR REPLACE FEATURES IN FM.TUTORIALS.HOOK_REF AS
PERSIST
secure_stream := SOURCE_STREAM(
'kafka://broker:9093'
WITH (
FORMAT = 'json',
SECURITY_PROTOCOL = 'sasl_ssl',
SASL_MECHANISM = 'scram-sha-512',
USERNAME = 'hooks',
PASSWORD = SECRET('env', 'KAFKA_PASSWORD'),
SSL_CA = '/etc/kafka/ca.crt'
)
)
;Result
| feature_name VARCHAR | status VARCHAR | message VARCHAR |
|---|---|---|
| FM.TUTORIALS.HOOK_REF.SECURE_STREAM | CREATED | Feature created as not exists |