EXTERNAL_KV()

All functions > SOURCE > EXTERNAL_KV()

Returns the value of a key (or of a field of a hash) from a key-value source

Signatures

Returns: ROW(value VARCHAR), ROW(value type) for an AS type, or the fields of an AS ROW(...) type; plus source_status on serving

EXTERNAL_KV(
  KEY key
  [ FIELD field ]
  FROM source
  [ AS type [ INDEXED ON field ] ]
  [ WITH ( [ FORMAT = format ] [, COMPRESSION = compression ] ) ]
) → ROW
ParameterTypeRequiredDescription
sourceKVSOURCEYesConnection feature defined with SOURCE_KV()
keyVARCHARYesRedis key to fetch
fieldVARCHARNoOptional field name for Redis Hash lookups
as_typeTYPENoAS type: the value is JSON (after FORMAT), returned in this type
indexed_onFIELDNoINDEXED ON field: the key is a sorted set read as an ARRAY(ROW(...)), one row per member, its score the field (a TIMESTAMP scored in Unix seconds, or a number)
formatVARCHARNoWITH (FORMAT = ...): how the value is stored: 'text' (default), 'json', 'msgpack' or 'float32' (little-endian vector); binary formats are returned as JSON text
compressionVARCHARNoWITH (COMPRESSION = ...): 'none' (default) or 'lz4' (LZ4 block, little-endian u32 size first)

Notes

  • Fetches value from Redis with low latency
  • Supports Redis Strings (key only) and Hashes (key + field)
  • For Strings: EXTERNAL_KV(KEY key FROM source)
  • For Hashes: EXTERNAL_KV(KEY key FIELD field FROM source)
  • Binary values: WITH (FORMAT = 'msgpack' | 'float32' | 'json', COMPRESSION = 'lz4'); the value is decoded to JSON text for JSON_PARSE_AS()
  • Typed values: AS type parses the value (JSON text, after FORMAT) into the type, as JSON_PARSE_AS() would
  • Sorted sets: AS ARRAY(ROW(...)) INDEXED ON field reads the members in score order, one row each: the score fills the field (Unix seconds for a TIMESTAMP), a JSON object member fills the other fields, any other member the one other field. A missing key is an empty array
  • An indexed history reads like any array of rows: ROW_AT_TIME() and ROWS_AT_TIME() on it read only the members they need from Redis
  • A read is a ROW: ROW(value VARCHAR) without AS, ROW(value type) for any other AS type (histories included), read with [value]; an AS ROW(...) read keeps its fields
  • Every read row also has source_status (OK, MISS, TIMEOUT, UNAVAILABLE, ERROR) on serving: reading it guards the read (on a failed key the value is NULL, the status says why, the call succeeds); unguarded, a failed read fails the call
  • Online-only restriction (for real-time serving)
  • To treat keys as rows, use EXTERNAL_COLUMNS(... FROM TABLE(source[key_pattern]))

Availability

StatusBackends
Availableserving

See also

Examples

Read a string value by key

FeatureQL
DEFINE
    customer_id := INPUT(BIGINT),
    redis_key := 'serving_slt:customer:' || customer_id::VARCHAR,
    customer_details_json := EXTERNAL_KV(
        KEY redis_key
        FROM fm.tutorials.external.redis_src
    )[value],
    customer_details := JSON_PARSE_AS(
        customer_details_json AS ROW<customer_id BIGINT, last_order_id BIGINT>
    )
RETURN
    customer_id,
    last_order_id := customer_details[last_order_id]
FOR
    customer_id := BIND_VALUE(1)
;
Result
CUSTOMER_ID BIGINTLAST_ORDER_ID BIGINT
1101

Read one field of a hash (KEY ... FIELD ...)

FeatureQL
DEFINE
    customer_id := INPUT(BIGINT),
    redis_key := 'serving_slt:customer_hash:' || customer_id::VARCHAR
RETURN
    customer_id,
    last_order_id := EXTERNAL_KV(
        KEY redis_key
        FIELD 'last_order_id'
        FROM fm.tutorials.external.redis_src
    )[value]
FOR
    customer_id := BIND_VALUE(1)
;
Result
CUSTOMER_ID BIGINTLAST_ORDER_ID VARCHAR
1101