Skip to content

RDSC-3619: Remapping the output #1662

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,48 +19,22 @@ By default, RDI adds fields to
[hash]({{< relref "/develop/data-types/hashes" >}}) or
[JSON]({{< relref "/develop/data-types/json" >}}) objects in the target
database that closely match the columns of the source table.
The examples below show how you can create a completely new object structure
from existing fields using the
If you just want to limit the set fields in the output and/or rename some of them, you can use the
[`output mapping`]({{< relref "/integrate/redis-data-integration/data-pipelines/transform-examples/remapping-the-output" >}}) configuration option.

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
[`map`]({{< relref "/integrate/redis-data-integration/reference/data-transformation/map" >}})
transformation.

## Map to a new JSON structure

The first
[job file]({{< relref "/integrate/redis-data-integration/data-pipelines/data-pipelines#job-files" >}})
example creates a new [JSON]({{< relref "/develop/data-types/json" >}})
object structure to write to the target.
The `source` section selects the `employee` table of the
[`chinook`](https://github.com/Redislabs-Solution-Architects/rdi-quickstart-postgres)
database (the optional `db` value here corresponds to the
`sources.<source-name>.connection.database` value defined in
[`config.yaml`]({{< relref "/integrate/redis-data-integration/data-pipelines/data-pipelines#the-configyaml-file" >}})).

In the `transform` section, the `map` transformation uses a [JMESPath](https://jmespath.org/)
expression to specify the new JSON format. (Note that the vertical bar "|" in the `expression`
line indicates that the following indented lines should be interpreted as a single string.)
The expression resembles JSON notation but with data values supplied from
table fields and
[JMESPath functions]({{< relref "/integrate/redis-data-integration/reference/jmespath-custom-functions" >}}).

Here, we rename the
`employeeid` field to `id` and create two nested objects for the `address`
and `contact` information. The `name` field is the concatenation of the existing
`firstname` and `lastname` fields, with `lastname` converted to uppercase.
In the `contact` subobject, the `email` address is obfuscated slightly, using the
`replace()` function to hide the '@' sign and dots.
transformation. Take a look at the following examples to see how to use the `map` transformation:

In the `output` section of the job file, we specify that we want to write
to a JSON object with a custom key. Note that in the `output` section, you must refer to
fields defined in the `map` transformation, so we use the new name `id`
for the key instead of `employeeid`.
## Creating multilevel JSON objects

The full example is shown below:
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.

```yaml
source:
db: chinook
table: employee

transform:
- uses: map
with:
Expand All @@ -81,16 +55,30 @@ transform:
}
}
language: jmespath

output:
- uses: redis.write
with:
connection: target
data_type: json
key:
expression: concat(['emp:', id])
language: jmespath
```


The example above creates a new JSON object with the following structure:
- A top-level `id` field that is the same as the `employeeid` field in the source table.
- A `name` field that is a concatenation of the `firstname` and `lastname` fields, with the `lastname` converted to uppercase.
- An `address` subobject that contains the `address`, `city`, `state`, `postalcode`, and `country` fields.
- 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.

In the `output` section of the job file, we specify that we want to write
to a JSON object with a custom key. Note that in the `output` section, you must refer to
fields defined in the `map` transformation, so we use the new name `id`
for the key instead of `employeeid`.



If you query one of the new JSON objects, you see output like the following:

```bash
Expand Down Expand Up @@ -118,7 +106,7 @@ Formatted in the usual JSON style, the output looks like the sample below:
}
```

## Map to a hash structure
## Creating hash structure

This example creates a new [hash]({{< relref "/develop/data-types/hashes" >}})
object structure for items from the `track` table. Here, the `map` transformation uses
Expand Down Expand Up @@ -167,4 +155,4 @@ like the following:
6) "3:35.0"
7) "storagesize"
8) "6.71MB"
```
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
Title: Remapping the fields in the output
aliases: null
alwaysopen: false
categories:
- docs
- integrate
- rs
- rdi
description: null
group: di
linkTitle: Remapping the fields in the output
summary: Redis Data Integration keeps Redis in sync with the primary database in near
real time.
type: integration
weight: 40
---

Sometimes, you may want to remap the fields in the output of a data pipeline. This can be done by defining a `mapping` section in the output configuration.

```yaml
source:
table: Customer

output:
- uses: redis.write
with:
data_type: hash
mapping:
- CustomerId: id
- FirstName: first_name
- LastName: last_name
```

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.
You can also use `mapping` to include only the fields you need in the output and exclude the rest.

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" >}}).