11---
2- Title : Add a new field to a hash/JSON key
2+ Title : Add new fields to a key
33alwaysopen : false
44categories :
55- docs
@@ -8,33 +8,38 @@ categories:
88- rdi
99description : null
1010group : di
11- linkTitle : Add a new hash/JSON field
11+ linkTitle : Add new fields
1212summary : Redis Data Integration keeps Redis in sync with the primary database in near
1313 real time.
1414type : integration
15- weight : 30
15+ weight : 40
1616---
1717
1818By default, RDI adds fields to
1919[ hash] ({{< relref "/develop/data-types/hashes" >}}) or
2020[ 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
21+ database that match the columns of the source table.
22+ The examples below show how to add extra fields to the target data with the
2323[ ` add_field ` ] ({{< relref "/integrate/redis-data-integration/reference/data-transformation/add_field" >}}) transformation.
2424
25+ ## Add a single field
26+
27+ The first example adds a single field to the data.
2528The ` source ` section selects the ` customer ` table of the
2629[ ` 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
30+ database (the optional ` db ` value here corresponds to the
31+ ` sources.<source-name>.connection.database ` value defined in
2932[ ` config.yaml ` ] ({{< relref "/integrate/redis-data-integration/data-pipelines/data-pipelines#the-configyaml-file" >}})).
3033
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
34+ In the ` transform ` section, the ` add_field ` transformation adds an extra field called ` fullname `
35+ to the object, which is created by concatenating the existing ` firstname ` and ` lastname ` fields using
3336an [ SQL expression] ( https://www.simplilearn.com/tutorials/sql-tutorial/concat-function-in-sql ) .
37+ You can also specify ` jmespath ` as the ` language ` if you prefer to create the new
38+ field with a [ JMESPath] ( https://jmespath.org/ ) expression
3439
3540The ` output ` section specifies ` hash ` as the ` data_type ` to write to the target, which
3641overrides 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> ` .
42+ ` output.with. key ` section specifies a custom key format of the form ` cust:<customerid> ` .
3843
3944The full example is shown below:
4045
@@ -58,10 +63,11 @@ output:
5863 language : jmespath
5964` ` `
6065
61- If you queried the generated target data from the default transformation, you would
66+ If you queried the generated target data from the default transformation
67+ using [` redis-cli`]({{< relref "/develop/connect/cli" >}}), you would
6268see something like the following :
6369
64- ` ` ` bash
70+ ` ` `
6571> hgetall cust:14
6672 1) "customerid"
6773 2) "14"
@@ -79,7 +85,7 @@ see something like the following:
7985
8086Using the job file above, the data also includes the new `fullname` field :
8187
82- ``` bash
88+ ` ` `
8389 1) "customerid"
8490 2) "14"
8591 3) "firstname"
@@ -92,6 +98,8 @@ Using the job file above, the data also includes the new `fullname` field:
929828) "Mark Philips"
9399` ` `
94100
101+ # # Add multiple fields
102+
95103The `add_field` transformation can also add multiple fields at the same time
96104if you specify them under a `fields` subsection. The example below is similar
97105to the previous one but also adds a `fulladdress` field and uses JSON as the
@@ -124,15 +132,18 @@ output:
124132You can query the target database to see the new `fullname` field in
125133the JSON object :
126134
127- ` ` ` bash
135+ ` ` `
128136> JSON.GET cust:14 $.fulladdress
129137"[\" 8210 111 ST NW, Edmonton, Canada, T6G 2C7\" ]"
130138` ` `
131139
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 :
140+ # # Using `add_field` with `remove_field`
141+
142+ You can use the `add_field` and
143+ [`remove_field`]({{< relref "/integrate/redis-data-integration/data-pipelines/transform-examples/redis-remove-field-example" >}})
144+ transformations together to completely replace fields from the source. For example,
145+ if you add a new `fullname` field, you might not need the separate `firstname` and
146+ `lastname` fields. You can remove them with a job file like the following :
136147
137148` ` ` yaml
138149source:
0 commit comments