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
ParameterTypeRequiredDescription
prepared_statementFEATUREYesThe PREPARED_STATEMENT the hook runs: its output goes TO the sinks
triggerFEATUREYesA 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
conditionBOOLEANYesWhich trigger rows fire the hook; reads the trigger's row only
valueANYYesThe statement's USING, by position and in its grammar, binders removed: a value per input, a parenthesized group per table of several inputs
sinkSINKYesHot sinks only (Redis, PostgreSQL, MySQL): latest values and the event index
startVARCHARYesStream 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 VARCHARstatus VARCHARmessage VARCHAR
FM.TUTORIALS.HOOK_REF.AMOUNTCREATEDFeature created as not exists
FM.TUTORIALS.HOOK_REF.EVENT_TIMECREATEDFeature created as not exists
FM.TUTORIALS.HOOK_REF.MERCHANT_IDCREATEDFeature created as not exists
FM.TUTORIALS.HOOK_REF.MINUTE_CLOSECREATEDFeature created as not exists
FM.TUTORIALS.HOOK_REF.MINUTE_REFCREATEDFeature created as not exists
FM.TUTORIALS.HOOK_REF.ON_PAYMENTCREATEDFeature created as not exists
FM.TUTORIALS.HOOK_REF.PAYMENTSCREATEDFeature created as not exists
FM.TUTORIALS.HOOK_REF.PAYMENT_IDCREATEDFeature created as not exists
FM.TUTORIALS.HOOK_REF.PAY_INDEXCREATEDFeature created as not exists
FM.TUTORIALS.HOOK_REF.PAY_PSCREATEDFeature created as not exists
FM.TUTORIALS.HOOK_REF.PAY_ROWCREATEDFeature created as not exists
FM.TUTORIALS.HOOK_REF.STREAMCREATEDFeature 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 VARCHARstatus VARCHARmessage VARCHAR
FM.TUTORIALS.HOOK_REF.MINUTE_PSCREATEDFeature created as not exists
FM.TUTORIALS.HOOK_REF.MINUTE_QUEUECREATEDFeature created as not exists
FM.TUTORIALS.HOOK_REF.MMCREATEDFeature created as not exists
FM.TUTORIALS.HOOK_REF.N_DOUBLEDCREATEDFeature created as not exists
FM.TUTORIALS.HOOK_REF.PER_MINUTECREATEDFeature created as not exists
FM.TUTORIALS.HOOK_REF.ROLLUP_MINUTECREATEDFeature created as not exists