Skip to content

Commit 46615e7

Browse files
DOC-4510 added add_field example
1 parent 37f2a24 commit 46615e7

File tree

1 file changed

+160
-0
lines changed

1 file changed

+160
-0
lines changed
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
---
2+
Title: Add a new field to a hash/JSON key
3+
alwaysopen: false
4+
categories:
5+
- docs
6+
- integrate
7+
- rs
8+
- rdi
9+
description: null
10+
group: di
11+
linkTitle: Add a new hash/JSON field
12+
summary: Redis Data Integration keeps Redis in sync with the primary database in near
13+
real time.
14+
type: integration
15+
weight: 30
16+
---
17+
18+
By default, RDI adds fields to
19+
[hash]({{< relref "/develop/data-types/hashes" >}}) or
20+
[JSON]({{< relref "/develop/data-types/json" >}}) objects in the target
21+
database that closely correspond to the columns of the source table.
22+
This example shows how to add extra fields to the target data with the
23+
[`add_field`]({{< relref "/integrate/redis-data-integration/reference/data-transformation/add_field" >}}) transformation.
24+
25+
The `source` section selects the `customer` table of the
26+
[`chinook`](https://github.com/Redislabs-Solution-Architects/rdi-quickstart-postgres)
27+
database (the optional `db` field here corresponds to the
28+
`sources.<source-name>.connection.database` field defined in
29+
[`config.yaml`]({{< relref "/integrate/redis-data-integration/data-pipelines/data-pipelines#the-configyaml-file" >}})).
30+
31+
In the `transform` section, the `add_field` transformation adds an extra field called `fullname`,
32+
which is created by concatenating the existing `firstname` and `lastname` fields using
33+
an [SQL expression](https://www.simplilearn.com/tutorials/sql-tutorial/concat-function-in-sql).
34+
35+
The `output` section specifies `hash` as the `data_type` to write to the target, which
36+
overrides the default setting of `target_data_type` defined in `config.yaml`. Also, the
37+
`output.key` section specifies a custom key format of the form `cust:<customerid>`.
38+
39+
The full example is shown below:
40+
41+
```yaml
42+
source:
43+
db: chinook
44+
table: customer
45+
transform:
46+
- uses: add_field
47+
with:
48+
expression: concat(firstname, ' ', lastname)
49+
field: fullname
50+
language: sql
51+
output:
52+
- uses: redis.write
53+
with:
54+
connection: target
55+
data_type: hash
56+
key:
57+
expression: concat(['cust:', customerid])
58+
language: jmespath
59+
```
60+
61+
If you queried the generated target data from the default transformation, you would
62+
see something like the following:
63+
64+
```bash
65+
> hgetall cust:14
66+
1) "customerid"
67+
2) "14"
68+
3) "firstname"
69+
4) "Mark"
70+
5) "lastname"
71+
6) "Philips"
72+
7) "company"
73+
8) "Telus"
74+
9) "address"
75+
10) "8210 111 ST NW"
76+
.
77+
.
78+
```
79+
80+
Using the job file above, the data also includes the new `fullname` field:
81+
82+
```bash
83+
1) "customerid"
84+
2) "14"
85+
3) "firstname"
86+
4) "Mark"
87+
5) "lastname"
88+
6) "Philips"
89+
.
90+
.
91+
27) "fullname"
92+
28) "Mark Philips"
93+
```
94+
95+
The `add_field` transformation can also add multiple fields at the same time
96+
if you specify them under a `fields` subsection. The example below is similar
97+
to the previous one but also adds a `fulladdress` field and uses JSON as the
98+
target datatype, rather than hash:
99+
100+
```yaml
101+
source:
102+
db: chinook
103+
table: customer
104+
transform:
105+
- uses: add_field
106+
with:
107+
fields:
108+
- expression: concat(firstname, ' ', lastname)
109+
field: fullname
110+
language: sql
111+
- expression: concat(address, ', ', city, ', ', country, ', ', postalcode)
112+
field: fulladdress
113+
language: sql
114+
output:
115+
- uses: redis.write
116+
with:
117+
connection: target
118+
data_type: json
119+
key:
120+
expression: concat(['cust:', customerid])
121+
language: jmespath
122+
```
123+
124+
You can query the target database to see the new `fullname` field in
125+
the JSON object:
126+
127+
```bash
128+
> JSON.GET cust:14 $.fulladdress
129+
"[\"8210 111 ST NW, Edmonton, Canada, T6G 2C7\"]"
130+
```
131+
132+
You can use the `add_field` and `remove_field` transformations together
133+
to completely replace fields from the source. For example, if you add
134+
a new `fullname` field, you might not need the separate `firstname` and `lastname`
135+
fields. You can remove them with a job file like the following:
136+
137+
```yaml
138+
source:
139+
db: chinook
140+
table: customer
141+
transform:
142+
- uses: add_field
143+
with:
144+
expression: concat(firstname, ' ', lastname)
145+
field: fullname
146+
language: sql
147+
- uses: remove_field
148+
with:
149+
fields:
150+
- field: firstname
151+
- field: lastname
152+
output:
153+
- uses: redis.write
154+
with:
155+
connection: target
156+
data_type: hash
157+
key:
158+
expression: concat(['cust:', customerid])
159+
language: jmespath
160+
```

0 commit comments

Comments
 (0)