Skip to content

Commit dd730fd

Browse files
Merge pull request #1662 from ilianiliev-redis/RDSC-3619-remapping-the-output
RDSC-3619: Remapping the output
2 parents 2816f49 + 3b18fb3 commit dd730fd

File tree

2 files changed

+64
-38
lines changed

2 files changed

+64
-38
lines changed

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

Lines changed: 26 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -19,48 +19,22 @@ By default, RDI adds fields to
1919
[hash]({{< relref "/develop/data-types/hashes" >}}) or
2020
[JSON]({{< relref "/develop/data-types/json" >}}) objects in the target
2121
database that closely match the columns of the source table.
22-
The examples below show how you can create a completely new object structure
23-
from existing fields using the
22+
If you just want to limit the set fields in the output and/or rename some of them, you can use the
23+
[`output mapping`]({{< relref "/integrate/redis-data-integration/data-pipelines/transform-examples/remapping-the-output" >}}) configuration option.
24+
25+
For situations where you want to create a new object structure with multiple levels or use calculations for the field values, you can use the
2426
[`map`]({{< relref "/integrate/redis-data-integration/reference/data-transformation/map" >}})
25-
transformation.
26-
27-
## Map to a new JSON structure
28-
29-
The first
30-
[job file]({{< relref "/integrate/redis-data-integration/data-pipelines/transform-examples" >}})
31-
example creates a new [JSON]({{< relref "/develop/data-types/json" >}})
32-
object structure to write to the target.
33-
The `source` section selects the `employee` table of the
34-
[`chinook`](https://github.com/Redislabs-Solution-Architects/rdi-quickstart-postgres)
35-
database (the optional `db` value here corresponds to the
36-
`sources.<source-name>.connection.database` value defined in
37-
[`config.yaml`]({{< relref "/integrate/redis-data-integration/data-pipelines/pipeline-config" >}})).
38-
39-
In the `transform` section, the `map` transformation uses a [JMESPath](https://jmespath.org/)
40-
expression to specify the new JSON format. (Note that the vertical bar "|" in the `expression`
41-
line indicates that the following indented lines should be interpreted as a single string.)
42-
The expression resembles JSON notation but with data values supplied from
43-
table fields and
44-
[JMESPath functions]({{< relref "/integrate/redis-data-integration/reference/jmespath-custom-functions" >}}).
45-
46-
Here, we rename the
47-
`employeeid` field to `id` and create two nested objects for the `address`
48-
and `contact` information. The `name` field is the concatenation of the existing
49-
`firstname` and `lastname` fields, with `lastname` converted to uppercase.
50-
In the `contact` subobject, the `email` address is obfuscated slightly, using the
51-
`replace()` function to hide the '@' sign and dots.
52-
53-
In the `output` section of the job file, we specify that we want to write
54-
to a JSON object with a custom key. Note that in the `output` section, you must refer to
55-
fields defined in the `map` transformation, so we use the new name `id`
56-
for the key instead of `employeeid`.
27+
transformation, as described in the following sections.
5728

58-
The full example is shown below:
29+
## Creating multilevel JSON objects
30+
31+
You can use the `map` transformation to create a new structure for the output data, which can include nested objects and calculated fields. The `map` transformation allows you to define a new structure using an expression language, such as SQL or JavaScript.
5932

6033
```yaml
6134
source:
6235
db: chinook
6336
table: employee
37+
6438
transform:
6539
- uses: map
6640
with:
@@ -81,16 +55,30 @@ transform:
8155
}
8256
}
8357
language: jmespath
58+
8459
output:
8560
- uses: redis.write
8661
with:
87-
connection: target
8862
data_type: json
8963
key:
9064
expression: concat(['emp:', id])
9165
language: jmespath
9266
```
9367
68+
69+
The example above creates a new JSON object with the following structure:
70+
- A top-level `id` field that is the same as the `employeeid` field in the source table.
71+
- A `name` field that is a concatenation of the `firstname` and `lastname` fields, with the `lastname` converted to uppercase.
72+
- An `address` subobject that contains the `address`, `city`, `state`, `postalcode`, and `country` fields.
73+
- A `contact` subobject that contains the `phone` field and a modified version of the `email` field, where the '@' sign and dots are replaced with '_at_' and '_dot_' respectively.
74+
75+
The `output` section of the file configures the job to write
76+
to a JSON object with a custom key. Note that in the `output` section, you must refer to
77+
fields defined in the `map` transformation, so we use the new name `id`
78+
for the key instead of `employeeid`.
79+
80+
81+
9482
If you query one of the new JSON objects, you see output like the following:
9583

9684
```bash
@@ -118,7 +106,7 @@ Formatted in the usual JSON style, the output looks like the sample below:
118106
}
119107
```
120108

121-
## Map to a hash structure
109+
## Creating hash structure
122110

123111
This example creates a new [hash]({{< relref "/develop/data-types/hashes" >}})
124112
object structure for items from the `track` table. Here, the `map` transformation uses
@@ -167,4 +155,4 @@ like the following:
167155
6) "3:35.0"
168156
7) "storagesize"
169157
8) "6.71MB"
170-
```
158+
```
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
Title: Remapping the fields in the output
3+
aliases: null
4+
alwaysopen: false
5+
categories:
6+
- docs
7+
- integrate
8+
- rs
9+
- rdi
10+
description: null
11+
group: di
12+
linkTitle: Remapping the fields in the output
13+
summary: Redis Data Integration keeps Redis in sync with the primary database in near
14+
real time.
15+
type: integration
16+
weight: 40
17+
---
18+
19+
Sometimes, you may want to remap the fields in the output of a data pipeline. You can do this by defining a `mapping` section in the output configuration.
20+
21+
```yaml
22+
source:
23+
table: Customer
24+
25+
output:
26+
- uses: redis.write
27+
with:
28+
data_type: hash
29+
mapping:
30+
- CustomerId: id
31+
- FirstName: first_name
32+
- LastName: last_name
33+
```
34+
35+
The example above remaps the `CustomerId` field to `id`, `FirstName` to `first_name`, and `LastName` to `last_name` in the output. This allows you to customize the field names in the Redis data store according to your application's requirements.
36+
You can also use `mapping` to include only the fields you need in the output and exclude the rest.
37+
38+
Mapping only allows you to rename fields and limit the output to specific fields and define a single level structure. To create nested structures and/or perform operations on the field values you can use the [map transformation]({{< relref "/integrate/redis-data-integration/data-pipelines/transform-examples/map-example" >}}).

0 commit comments

Comments
 (0)