Skip to content

Commit e224d38

Browse files
DOC-4510 new add_field examples
1 parent cf22d0c commit e224d38

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

content/integrate/redis-data-integration/data-pipelines/transform-examples/redis-add-field-example.md

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ database (the optional `db` value here corresponds to the
3333

3434
In the `transform` section, the `add_field` transformation adds an extra field called `localphone`
3535
to the object, which is created by removing the country and area code from the `phone`
36-
field with the `regex_replace()` [JMESPath](https://jmespath.org/) function.
36+
field with the
37+
[JMESPath]({{< relref "/integrate/redis-data-integration/reference/jmespath-custom-functions" >}}) function `regex_replace()`.
3738
You can also specify `sql` as the `language` if you prefer to create the new
3839
field with an [SQL](https://en.wikipedia.org/wiki/SQL) expression.
3940

@@ -86,7 +87,7 @@ see something like the following:
8687
Using the job file above, the data also includes the new `localphone` field:
8788

8889
```
89-
1) "customerid"
90+
1) "customerid"
9091
2) "27"
9192
3) "firstname"
9293
4) "Patrick"
@@ -101,40 +102,44 @@ Using the job file above, the data also includes the new `localphone` field:
101102
## Add multiple fields
102103

103104
The `add_field` transformation can also add multiple fields at the same time
104-
if you specify them under a `fields` subsection. The example below is similar
105-
to the previous one but also adds a `fulladdress` field and uses JSON as the
106-
target datatype, rather than hash:
105+
if you specify them under a `fields` subsection. The example below adds two
106+
fields to the `track` objects. The first new field, `seconds`, is created using a SQL
107+
expression to calculate the duration of the track in seconds from the
108+
`milliseconds` field.
109+
The second new field, `composerlist`, adds a JSON array using the `split()` function
110+
to split the `composer` string field wherever it contains a comma.
107111

108112
```yaml
109113
source:
110114
db: chinook
111-
table: customer
115+
table: track
112116
transform:
113117
- uses: add_field
114118
with:
115119
fields:
116-
- expression: concat(firstname, ' ', lastname)
117-
field: fullname
118-
language: sql
119-
- expression: concat(address, ', ', city, ', ', country, ', ', postalcode)
120-
field: fulladdress
120+
- expression: floor(milliseconds / 1000)
121+
field: seconds
121122
language: sql
123+
- expression: split(composer)
124+
field: composerlist
125+
language: jmespath
122126
output:
123127
- uses: redis.write
124128
with:
125129
connection: target
126130
data_type: json
127131
key:
128-
expression: concat(['cust:', customerid])
132+
expression: concat(['track:', trackid])
129133
language: jmespath
130134
```
131135

132-
You can query the target database to see the new `fulladdress` field in
136+
You can query the target database to see the new fields in
133137
the JSON object:
134138

135-
```
136-
> JSON.GET cust:14 $.fulladdress
137-
"[\"8210 111 ST NW, Edmonton, Canada, T6G 2C7\"]"
139+
```bash
140+
> JSON.GET track:1 $
141+
142+
"[{\"trackid\":1,\"name\":\"For Those About To Rock (We Salute You)\",\"albumid\":1,\"mediatypeid\":1,\"genreid\":1,\"composer\":\"Angus Young, Malcolm Young, Brian Johnson\",\"milliseconds\":343719,\"bytes\":11170334,\"unitprice\":\"0.99\",\"seconds\":343,\"composerlist\":[\"Angus Young\",\" Malcolm Young\",\" Brian Johnson\"]}]"
138143
```
139144

140145
## Using `add_field` with `remove_field`

0 commit comments

Comments
 (0)