HOOK()
All functions > METADATA > HOOK()
Runs a prepared statement on each record of a stream, or after each hook run of a prepared statement, and writes its value to hot sinks for serving to read.
Signatures
Returns: A hook the hooks service runs
HOOK( RUN prepared_statement ON trigger [ AS alias ] [ WHERE condition ] USING value, ( value, ... ), ... [ TO sink, ... ] [ WITH ( START = start ) ] ) → HOOK
| Parameter | Type | Required | Description |
|---|---|---|---|
prepared_statement | FEATURE | Yes | The PREPARED_STATEMENT the hook runs: its output goes TO the sinks |
trigger | FEATURE | Yes | A stream's record (EXTERNAL_COLUMNS over TABLE(STREAM[topic]) of a SOURCE_STREAM), named by AS; or a PREPARED_STATEMENT, whose hook runs fire the hook |
condition | BOOLEAN | Yes | Which trigger rows fire the hook; reads the trigger's row only |
value | ANY | Yes | The statement's USING, by position and in its grammar, binders removed: a value per input, a parenthesized group per table of several inputs |
sink | SINK | Yes | Hot sinks only (Redis, PostgreSQL, MySQL): latest values and the event index |
start | VARCHAR | Yes | Stream trigger only, for a new consumer group: 'latest' (default), 'earliest' or an INTERVAL back from now |
Notes
- On a stream, each record is one call of the statement;
REQUEST_TIME()is the record's Kafka timestamp - On a stream, AS names the record and USING / WHERE read it through the alias (
p[payment_id]); on a prepared statement, they read its inputs, output and captures by feature name - AT, QUEUE, POLICY, EVERY and EXPIRE (scheduled hooks) are not supported yet
- A hook writes hot sinks only: history goes through a LOGGER on its statement
Examples
An event hook: run a prepared statement on each stream event
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 scheduled hook: run at each minute close
FeatureQL
CREATE OR REPLACE FEATURES IN FM.TUTORIALS.HOOK_REF AS
PERSIST
mm := INPUT(ROW<merchant_id BIGINT, minute TIMESTAMP(3)>),
n_doubled := mm[merchant_id] * 2,
minute_ps := PREPARED_STATEMENT(n_doubled USING mm),
per_minute := SINK(
'postgresql://fm@postgres:5432/fm'
TABLE 'per_minute' KEY (MERCHANT_ID, MINUTE)
WITH (
MODE = 'latest',
PASSWORD = SECRET('env', 'PG_PASSWORD')
)
),
minute_queue := SINK(
'postgresql://fm@postgres:5432/fm'
TABLE 'minute_queue'
WITH (
PASSWORD = SECRET('env', 'PG_PASSWORD')
)
),
rollup_minute := HOOK(
RUN minute_ps
ON pay_ps
AT minute_close
USING (minute_ref)
TO per_minute
QUEUE minute_queue
WITH (
POLICY = 'earliest',
EVERY = INTERVAL '10 SECOND',
EXPIRE = INTERVAL '23 HOUR'
)
)
;Result
| feature_name VARCHAR | status VARCHAR | message VARCHAR |
|---|---|---|
| FM.TUTORIALS.HOOK_REF.MINUTE_PS | CREATED | Feature created as not exists |
| FM.TUTORIALS.HOOK_REF.MINUTE_QUEUE | CREATED | Feature created as not exists |
| FM.TUTORIALS.HOOK_REF.MM | CREATED | Feature created as not exists |
| FM.TUTORIALS.HOOK_REF.N_DOUBLED | CREATED | Feature created as not exists |
| FM.TUTORIALS.HOOK_REF.PER_MINUTE | CREATED | Feature created as not exists |
| FM.TUTORIALS.HOOK_REF.ROLLUP_MINUTE | CREATED | Feature created as not exists |