|
| 1 | +--- |
| 2 | +Title: Set the key name in the target database |
| 3 | +aliases: /integrate/redis-data-integration/ingest/data-pipelines/transform-examples/redis-set-key/ |
| 4 | +alwaysopen: false |
| 5 | +categories: |
| 6 | +- docs |
| 7 | +- integrate |
| 8 | +- rs |
| 9 | +- rdi |
| 10 | +description: null |
| 11 | +group: di |
| 12 | +linkTitle: Set the key name in the target database |
| 13 | +summary: Learn how to customize Redis key names when synchronizing data from your primary database using Redis Data Integration. |
| 14 | +type: integration |
| 15 | +weight: 40 |
| 16 | +--- |
| 17 | + |
| 18 | +## Understanding Default Key Names |
| 19 | + |
| 20 | +When RDI synchronizes data from your primary database to Redis, it automatically generates key names based on a specific pattern. |
| 21 | +By default, RDI creates keys using the following format: |
| 22 | + |
| 23 | +* **Single primary key**: `tablename:primarykeyname:primarykeyvalue` |
| 24 | +* **Composite primary keys**: `tablename:key1name:key1value:key2name:key2value` |
| 25 | + |
| 26 | +Examples |
| 27 | + |
| 28 | +* For a table named `employee` with primary key `employeeid`, a record with `employeeid=1` will have the key `employee:employeeid:1` |
| 29 | +* For a table named `orders` with composite primary keys `orderid` and `customerid`, a record with `orderid=1` and `customerid=2` will have the key `orders:orderid:1:customerid:2` |
| 30 | + |
| 31 | +## Customizing Key Names |
| 32 | + |
| 33 | +While the default key naming convention works for many use cases, you may need custom key formats to: |
| 34 | + |
| 35 | +* Match existing Redis key patterns |
| 36 | +* Create more concise key names |
| 37 | +* Implement application-specific naming schemes |
| 38 | +* Optimize for specific access patterns |
| 39 | + |
| 40 | +To customize key names, use the `key` section within the `redis.write` output configuration. This section requires two parameters: |
| 41 | + |
| 42 | +* `expression`: Defines the custom key format using a supported expression language |
| 43 | +* `language`: Specifies the expression language to use (`jmespath` or `sql`) |
| 44 | + |
| 45 | +### Example |
| 46 | + |
| 47 | + |
| 48 | +```yaml |
| 49 | +source: |
| 50 | + server_name: rdi |
| 51 | + db: inventory |
| 52 | + table: customers |
| 53 | +output: |
| 54 | + - uses: redis.write |
| 55 | + with: |
| 56 | + key: |
| 57 | + expression: concat(['customers', '#', id]) |
| 58 | + language: jmespath |
| 59 | +``` |
| 60 | +
|
| 61 | +## Special Considerations for Full Row Format |
| 62 | +
|
| 63 | +When working with the full row format, you need to handle key generation differently to ensure proper behavior across all operation types (create, update, and delete). You must reference attributes correctly to ensure consistent key generation, especially for delete operations where the "after" state is empty. The example below demonstrates how to handle this: |
| 64 | +
|
| 65 | +```yaml |
| 66 | +source: |
| 67 | + server_name: rdi |
| 68 | + db: inventory |
| 69 | + table: customers |
| 70 | + row_format: full |
| 71 | +output: |
| 72 | + - uses: redis.write |
| 73 | + with: |
| 74 | + key: |
| 75 | + # Here we use the operation code to determine the value of the key to ensure that |
| 76 | + # delete operations will result in the correct key being deleted |
| 77 | + expression: concat(['customers', '#', opcode == 'd' && before.id || after.id]) |
| 78 | + language: jmespath |
| 79 | +``` |
| 80 | +
|
| 81 | +## Summary |
| 82 | +
|
| 83 | +Proper key naming is essential for effective data organization in Redis. RDI provides: |
| 84 | +
|
| 85 | +1. **Default key naming** that follows a consistent pattern based on table and primary key information |
| 86 | +2. **Custom key naming** through the `key` section with `expression` and `language` parameters |
| 87 | +3. **Special handling for full row format** to ensure consistent key generation across all operation types |
| 88 | + |
| 89 | +By understanding and utilizing these options, you can ensure your Redis keys are optimally structured for your specific use case, making data retrieval more efficient and your Redis implementation more maintainable. |
0 commit comments