从键中删除字段
默认情况下,RDI 会将字段添加到目标中的哈希或 JSON 对象
database 的 database 中。
以下示例显示了如何使用remove_field转型。
删除单个字段
第一个示例从数据中删除单个字段。
这source部分选择employee的chinookdatabase(可选的dbfield 对应于sources.<source-name>.connection.database字段定义于config.yaml).
在transform部分、remove_field转换会删除hiredate田。
这outputsection 指定hash作为data_type写入目标,该
覆盖target_data_type定义于config.yaml.此外,output.with.keysection 指定表单的自定义键格式emp:<employeeid>.
请注意,您在transform部分不适用于
Key 计算中的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
If you queried the generated target data from the default transformation
using redis-cli, you would
see something like the following:
> 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"
.
.
Using the job file above, the data omits the hiredate field:
> 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 multiple fields
The remove_field transformation can also remove multiple fields at the same time
if you specify them under a fields subsection. The example below is similar
to the previous one but also removes the birthdate field:
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
If you query the data, you can see that it also omits the
birthdate field:
> 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"
.
.
Using remove_field with add_field
The remove_field transformation is very useful in combination with
add_field.
For example, if you use add_field to concatenate a person's first
and last names, you may not need separate firstname and lastname
fields, so you can use remove_field to omit them.
See Using add_field with remove_field
for an example of how to do this.
On this page