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
| Parameter | Type | Required | Description |
|---|---|---|---|
source | KVSOURCE | Yes | Connection feature defined with SOURCE_KV() |
key | VARCHAR | Yes | Redis key to fetch |
field | VARCHAR | No | Optional field name for Redis Hash lookups |
as_type | TYPE | No | AS type: the value is JSON (after FORMAT), returned in this type |
indexed_on | FIELD | No | INDEXED 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) |
format | VARCHAR | No | WITH (FORMAT = ...): how the value is stored: 'text' (default), 'json', 'msgpack' or 'float32' (little-endian vector); binary formats are returned as JSON text |
compression | VARCHAR | No | WITH (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 forJSON_PARSE_AS() - Typed values:
AS typeparses the value (JSON text, afterFORMAT) into the type, asJSON_PARSE_AS()would - Sorted sets:
AS ARRAY(ROW(...)) INDEXED ON fieldreads the members in score order, one row each: the score fills the field (Unix seconds for aTIMESTAMP), 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()andROWS_AT_TIME()on it read only the members they need from Redis - A read is a ROW:
ROW(value VARCHAR)withoutAS,ROW(value type)for any otherAStype (histories included), read with[value]; anAS 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
| Status | Backends |
|---|---|
| Available | serving |
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 BIGINT | LAST_ORDER_ID BIGINT |
|---|---|
| 1 | 101 |
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 BIGINT | LAST_ORDER_ID VARCHAR |
|---|---|
| 1 | 101 |