从键中移除字段

默认情况下,RDI 会为源表的每一列在目标数据库的 hashJSON 对象中添加字段。 以下示例展示了如何使用 remove_field 转换从目标数据中省略某些字段。

删除单个字段

第一个示例从数据中移除单个字段。 source 部分选择了 chinook 数据库的 employee 表(这里的可选 db 字段对应于在 config.yaml 中定义的 sources..connection.database 字段)。

transform部分,remove_field转换移除了hiredate字段。

output 部分指定了 hash 作为写入目标的 data_type,这将覆盖在 config.yaml 中定义的 target_data_type 的默认设置。此外,output.with.key 部分指定了一个自定义的键格式,形式为 emp:。请注意,在 transform 部分中删除的任何字段在 output 部分的键计算中不可用。

完整的示例如下所示:

source:
  db: chinook
  table: employee
transform:
  - uses: remove_field
    with:
      field: hiredate
output:
  - uses: redis.write
    with:
      connection: target
      data_type: hash
      key:
        expression: concat(['emp:', employeeid])
        language: jmespath

如果你使用redis-cli查询从默认转换生成的目数据,你会看到类似以下内容:

> hgetall emp:8
 1) "employeeid"
 2) "8"
 3) "lastname"
 4) "Callahan"
 5) "firstname"
 6) "Laura"
 7) "title"
 8) "IT Staff"
 9) "reportsto"
10) "6"
11) "birthdate"
12) "-62467200000000"
13) "hiredate"
14) "1078358400000000"
15) "address"
16) "923 7 ST NW"
.
.

使用上面的工作文件,数据省略了hiredate字段:

 > hgetall emp:8
 1) "employeeid"
 2) "8"
 3) "lastname"
 4) "Callahan"
 5) "firstname"
 6) "Laura"
 7) "title"
 8) "IT Staff"
 9) "reportsto"
10) "6"
11) "birthdate"
12) "-62467200000000"
13) "address"
14) "923 7 ST NW"
.
.

删除多个字段

remove_field 转换也可以同时删除多个字段,如果你在 fields 子部分中指定它们。下面的示例与前面的示例类似,但还删除了 birthdate 字段:

source:
  db: chinook
  table: employee
transform:
  - uses: remove_field
    with:
      fields:
        - field: hiredate
        - field: birthdate
output:
  - uses: redis.write
    with:
      connection: target
      data_type: hash
      key:
        expression: concat(['emp:', employeeid])
        language: jmespath

如果您查询数据,您可以看到它也省略了 birthdate 字段:

> hgetall emp:8
 1) "employeeid"
 2) "8"
 3) "lastname"
 4) "Callahan"
 5) "firstname"
 6) "Laura"
 7) "title"
 8) "IT Staff"
 9) "reportsto"
10) "6"
11) "address"
12) "923 7 ST NW"
.
.

使用 remove_fieldadd_field

remove_field 转换与 add_field 结合使用时非常有用。 例如,如果您使用 add_field 来连接一个人的名字和姓氏,您可能不再需要单独的 firstnamelastname 字段,因此您可以使用 remove_field 来省略它们。 请参阅 使用 add_fieldremove_field 以了解如何执行此操作的示例。

RATE THIS PAGE
Back to top ↑