redis.write

写入 Redis Enterprise 数据库

写入 Redis Enterprise 数据库

性能

名字 类型 描述 必填
连接 string 中指定的 Redis 连接的名称config.yaml.
默认为名为
target.
data_type
string Redis 目标数据结构的类型。
枚举:
hash(默认)、json,set,sorted_set,stream,string.
优先于系统属性
target_data_type.
object 将 (embed) 对象嵌套在不同的 key 中。
如果指定了 nesting,则忽略以下参数:
key,argson_update.
钥匙 object 目标 Redis key 的定义。
是的
表达式 string 用于计算目标键的表达式。 是的
语言 string 用于定义表达式的语言。
枚举:
jmespath,sql.
是的
参数 object 用于修改目标键的参数。
特定于数据类型。
映射 array 字段数组(或field: aliaspairs) 写入 Redis 键。
仅支持哈希、json 文档和流。
on_update string 目标密钥更新策略
Enum:
merge,replace(默认)。
到期 integer TTL(以秒为单位),以便修改后的密钥过期。
如果未指定(或
expire: 0),则目标密钥将永不过期。

笔记:

  • 作业参数始终覆盖系统属性。特别data_type将覆盖target_data_typeon_update将覆盖json_update_strategyproperties 分别。
  • JSON 文档的映射支持嵌套路径(例如path.to.field),这会导致在 Redis 键中创建嵌套元素。当字段名称中使用点时,必须使用反斜杠对其进行转义(例如path\.to\.field).哈希和流不支持嵌套路径。
  • 对于字符串,Write-behind 将自动假定on_update: replace无论作业文件中声明了什么。当前不支持 appends 和 increments。
  • 对于Stream,Write-behind 将忽略on_update属性,因为它们只是 Append 的。

笔记:

  • 作业参数始终覆盖系统属性。特别data_type将覆盖target_data_typeon_update将覆盖json_update_strategyproperties 分别。
  • JSON 文档的映射支持嵌套路径(例如path.to.field),这会导致在 Redis 键中创建嵌套元素。当字段名称中使用点时,必须使用反斜杠对其进行转义(例如path\.to\.field).哈希和流不支持嵌套路径。

source:
  server_name: chinook
  schema: public
  table: invoice
output:
  # this block will use the default connection: target - since no explicit connection is specified,
  # the data will be written in a JSON format as the data_type: json is specified for the block
  - uses: redis.write
    with:
      data_type: json
      key:
        expression: concat(['invoice_id:', InvoiceId])
        language: jmespath
      mapping: # only the fields listed below will be written to a JSON document
        - InvoiceId: id # this will create an element with a different name
        - InvoiceDate: date
        - BillingAddress: address.primary.street # this will create a nested element in the JSON document
        - BillingCity: "address.primary.city name" # this will create a nested element with a space in the name
        - BillingState: address.primary.state
        - BillingPostalCode: "address.primary.zip\\.code" # this will create a nested element with a dot in the name
        - Total # this will create an element with the same name as the original field
      on_update: merge
  # this block will use the explicitly specified connection: target1 - it must be defined in config.yaml
  # the data will be written to the corresponding Redis set, based on a value of the key expression
  - uses: redis.write
    with:
      connection: target
      data_type: set
      key:
        expression: concat(['invoices:', BillingCountry])
        language: jmespath
      args:
        member: InvoiceId
  # this block will use the explicitly specified connection: target1 - it must be defined in config.yaml
  # the data will be written to the Redis sorted set named invoices:sorted as specified in the key expression
  - uses: redis.write
    with:
      connection: target1
      data_type: sorted_set
      key:
        expression: "`invoices:sorted`"
        language: jmespath
      args:
        score: Total
        member: InvoiceId
  # this block will use the specified connection: target2 - this, again, has to be defined in config.yaml
  # the data will be written to a Redis stream named invoice:events as specified in the key expression
  - uses: redis.write
    with:
      connection: target2
      data_type: stream
      key:
        expression: "`invoice:events`"
        language: jmespath
      mapping: # only the fields listed below will be written to a stream message, with two of them renamed as message_id and country
        - InvoiceId: message_id
        - BillingCountry: country
        - Total
  # this block will use the default connection: target - since no explicit connection is specified,
  # the data will be written to a Redis string as the data_type: string is specified for the block
  - uses: redis.write
    with:
      data_type: string
      key:
        expression: concat(['Invoice:', InvoiceId])
        language: jmespath
      args:
        value: Total # only the Total field will be written to a string
      expire: 100 # the key will expire in 100 seconds

args: object

Arguments for modifying the target key

Properties

Name Type Description Required
score string Field name used as a score for sorted sets.
Valid for sorted sets only.
yes
member string Field name used as a member for sets and sorted sets.
Valid for sets and sorted sets only.
yes
value string Field name used as a value for strings.
Valid for strings only.
yes

nest: object

Nest (embed) object within a different key

Properties

Name Type Description Required
parent object Parent object definition. yes
server_name string Server name. no
schema string Schema name. no
table string Parent table. yes
parent_key string Field name used to identify the parent key (usually FK). yes
child_key string Optional field name used to identify the parent key value (if different from parent_key) in the child record. If not specified, then the field name defined in parent_key is used to lookup the value. no
nesting_key string Field name used to create the nesting key (usually PK). yes
path string Path, where the nested object should reside in a parent document.
Must start with the root (e.g. $.<children-elements-here>)
yes
structure string Data structure used to represent the object in a parent document (map is the only supported value). no

Notes:

  • When nest object is defined, Write-behind will automatically assume data_type: json and on_update: merge regardless of what was declared in the job file.
  • Nesting job cannot be used together with any of the these properties: key, args. The key is automatically calculated based on the following template: <parent_table>:<parent_key>:<parent_key.value | child_key.value>.
  • When expire is specified, it will be applied to the parent key. Therefore all nested objects will expire together with the parent key.

Example

source:
  server_name: chinook
  schema: public
  table: InvoiceLine
output:
  - uses: redis.write
    with:
      nest:
        parent:
          # server_name: chinook
          # schema: public
          table: Invoice
        nesting_key: InvoiceLineId
        parent_key: InvoiceId
        # child_key: ParentInvoiceId
        path: $.InvoiceLineItems
        # structure: map

Note: In the example above child_key is not needed, because the FK in the child table InvoiceLine is defined using the same field name InvoiceId as in the parent table Invoice. If instead a FK was defined differently (e.g. InvoiceLine.ParentInvoivceId = Invoice.InvoiceId), then child_key parameter would be required to describe this relationship in the child job.