Skip to content

Commit 15fb199

Browse files
Merge pull request #1647 from ilianiliev-redis/RDSC-3626-adding-opcode-example
RDSC RDSC-3626 - adding opcode examples
2 parents e5ff335 + 8f2917d commit 15fb199

File tree

2 files changed

+70
-32
lines changed

2 files changed

+70
-32
lines changed
Lines changed: 69 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
Title: Add the opcode to the Redis output
2+
Title: Using the operation code
33
alwaysopen: false
44
categories:
55
- docs
@@ -8,43 +8,90 @@ categories:
88
- rdi
99
description: null
1010
group: di
11-
linkTitle: Add the opcode to the Redis output
11+
linkTitle: Using the operation code
1212
summary: Redis Data Integration keeps Redis in sync with the primary database in near
1313
real time.
1414
type: integration
1515
weight: 100
1616
---
1717

18-
In the example below, the data is captured from the source table named `employee` and is written to the Redis database as a JSON document. When you specify the `data_type` parameter for the job, it overrides the system-wide setting `target_data_type` defined in `config.yaml`.
18+
The operation code (`opcode`) is a metadata field that indicates the type of operation that generated the change in the source database. It can be useful for tracking changes and understanding the context of the data being processed.
19+
20+
The opcode is only available in the [full row format]({{< relref "/integrate/redis-data-integration/data-pipelines/transform-examples/redis-row-format#full" >}}), and can be accessed in the `transform` and `output` sections of the job file.
21+
22+
It has one of the following values:
23+
24+
- r - Read (applies to only snapshots)
25+
- c - Create
26+
- u - Update
27+
- d - Delete
28+
- t = Truncate (PostgreSQL specific)
29+
- m = Message (PostgreSQL specific)
30+
31+
32+
You can add the value of the operation code to the output, and also use it in a conditional expression to modify the behavior of the job. The following examples demonstrate the different use-cases.
33+
34+
### Adding the operation code to the output
35+
36+
Use the `add_field` transformation to add a new field that contains the value of the `opcode` field from the source data. Note that the fields must be prefixed with `after` to be included in the output.
1937

20-
Here, the result will be Redis JSON documents with fields captured from the source table
21-
(`employeeid`, `firstname`, `lastname`) and also with
22-
an extra field `my_opcode` added using the `merge` update strategy (see the
23-
[JSON job example]({{< relref "/integrate/redis-data-integration/data-pipelines/transform-examples/redis-json-example" >}})
24-
for more information). The `opcode` expression refers to the operation code captured from
25-
the source. This is a database-specific value that indicates which type of operation generated
26-
the change (insert, update, etc).
2738

2839
```yaml
2940
source:
3041
schema: public
3142
table: employee
3243
row_format: full
44+
3345
transform:
46+
# add the operation code to the data
3447
- uses: add_field
3548
with:
36-
field: after.my_opcode
49+
field: after.operation_code
3750
expression: opcode
3851
language: jmespath
39-
output:
40-
- uses: redis.write
52+
```
53+
54+
55+
### Filtering operation by output code.
56+
57+
In some cases you may want to ignore certain operations (for example, you may not be interested in deletions). Use the `filter` transformation to filter out any operations you don't need to process.
58+
59+
```yaml
60+
source:
61+
schema: public
62+
table: employee
63+
row_format: full
64+
65+
transform:
66+
- uses: filter
67+
with:
68+
expression: opcode != 'd'
69+
language: jmespath
70+
```
71+
72+
### Modifying the output based on the operation code
73+
74+
The previous example filters out specific operations, but you can also modify the output based on the operation code. For example, you can add a new field that tracks the status of the record based on the operation code.
75+
76+
Note that when a source record is deleted, you must modify the value of the `opcode` field if you want to prevent the corresponding record in the target database from being removed automatically.
77+
78+
```yaml
79+
source:
80+
schema: public
81+
table: employee
82+
row_format: full
83+
84+
transform:
85+
- uses: add_field
4186
with:
42-
data_type: json
43-
mapping:
44-
- employeeid
45-
- firstname
46-
- lastname
47-
- my_opcode
48-
connection: target
49-
on_update: merge
50-
```
87+
fields:
88+
# Here you set the value of the field based on the value of the opcode field
89+
- field: after.status
90+
expression: opcode == 'd' && 'inactive' || 'active'
91+
language: jmespath
92+
93+
# You have to change the value of the opcode field to prevent deletion
94+
- field: opcode
95+
expression: opcode == 'd' && 'u' || opcode
96+
language: jmespath
97+
```

content/integrate/redis-data-integration/data-pipelines/transform-examples/redis-row-format.md

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ With `row_format: full` the input value is a JSON object with the following stru
6363
- `key` - An object containing the attributes of the primary key. For example, `key.id` will give you the value of the `id` column as long as it is part of the primary key.
6464
- `before` - An object containing the previous value of the row.
6565
- `after` - An object containing the current value of the row.
66-
- `opcode` - The operation code. Different databases use different values for the operation code. See [operation code values]({{< relref "#operation-codes" >}}) below for more information.
66+
- `opcode` - The operation code. See [Using the operation code]({{< relref "/integrate/redis-data-integration/data-pipelines/transform-examples/redis-opcode-example" >}}) for more information about the possible opcode values and how to use them.
6767
- `db` - The database name.
6868
- `table` - The table name.
6969
- `schema` - The schema name.
@@ -102,12 +102,3 @@ output:
102102
expression: concat(['addresses-full', '#', values(key)[0]])
103103
language: jmespath
104104
```
105-
106-
## Operation code values {#operation-codes}
107-
108-
- r - Read (applies to only snapshots)
109-
- c - Create
110-
- u - Update
111-
- d - Delete
112-
- t = truncate (PostgreSQL specific)
113-
- m = message (PostgreSQL specific)

0 commit comments

Comments
 (0)