{
  "id": "redis-lookup-example",
  "title": "Denormalization with redis.lookup",
  "url": "https://redis.io/docs/latest/integrate/redis-data-integration/data-pipelines/transform-examples/redis-lookup-example/",
  "summary": "",
  "tags": [
    "docs",
    "integrate",
    "rs",
    "rdi"
  ],
  "last_updated": "2026-05-12T09:07:59-04:00",
  "page_type": "content",
  "content_hash": "be6a80f686a8d078897b02ba90f0d6271cb7c873c35c4cde5107b7074e0616bf",
  "sections": [
    {
      "id": "overview",
      "title": "Overview",
      "role": "overview",
      "text": "You can use the\n[`redis.lookup`](https://redis.io/docs/latest/integrate/redis-data-integration/reference/data-transformation/lookup)\ntransformation to read existing data from Redis during the `transform` stage of a\njob. This is useful when you want to denormalize incoming change data by enriching\na record with values that RDI has already written to Redis from another table\n(see [Data denormalization](https://redis.io/docs/latest/integrate/redis-data-integration/data-pipelines/data-denormalization) for more information about this technique).\n\nFor example, a pipeline for the Chinook database might write `artist` records to\nRedis, then use `redis.lookup` in\nan `album` table job to add selected artist details\nto each album record before writing it to the target database. This lets you\ndesign the structure of your Redis data to fit the read patterns of your\napplication while still keeping the source database normalized."
    },
    {
      "id": "denormalizing-a-hash",
      "title": "Denormalizing a hash",
      "role": "content",
      "text": "The `redis.lookup` transformation works by executing a Redis command and adding the\nresult to the record. You specify the command and its arguments in the\n`transform` configuration with the `cmd` and `args` properties. For example, the\nfollowing transformation job uses the\n[`HGET`](https://redis.io/docs/latest/commands/hget) command to read the `name` field from an\nartist [hash](https://redis.io/docs/latest/develop/data-types/hashes) and adds it to the\nalbum record under the `artist` field. A particularly important thing to note\nhere is that the `args` elements are all interpreted as [JMESPath](https://jmespath.org/)\nexpressions, but YAML syntax allows for each element to be a quoted string. This means that\nyou must *double quote* any string arguments that you want to be treated as\nliteral strings (as with `name` below), otherwise JMESPath will try to interpret\nthem as field names, which will generally give the wrong result. Specifically, use\na different quote character for the outer quotes and the inner quotes.\n\n[code example]\n\nWithout denormalization, the album hash object contains only the `artistid` field to\nreference the artist:\n\n[code example]\n\nAfter running the job specified above, querying one of the album hash objects shows the\nextra `artist` field obtained by looking up the artist with the `artistid`:\n\n[code example]"
    },
    {
      "id": "denormalizing-a-json-document",
      "title": "Denormalizing a JSON document",
      "role": "content",
      "text": "If you are using [JSON](https://redis.io/docs/latest/develop/data-types/json) objects,\nyou can denormalize to include the whole of one object\nas a field of another. The following example shows how to do this using a temporary field\nto hold the result of the `redis.lookup` command. It then uses\n[`add_field`](https://redis.io/docs/latest/integrate/redis-data-integration/reference/data-transformation/add_field)\nto insert the new field and\n[`remove_field`](https://redis.io/docs/latest/integrate/redis-data-integration/reference/data-transformation/remove_field)\nto remove the temporary field and the now-redundant `artistid` field before writing the album object.\n\n[code example]\n\nAfter running this job, the album JSON object includes the artist object\nin a new `artist` field:\n\n[code example]"
    }
  ],
  "examples": [
    {
      "id": "denormalizing-a-hash-ex0",
      "language": "yaml",
      "code": "source:\n  table: album\ntransform:\n  - uses: redis.lookup\n    with:\n      connection: target\n      cmd: HGET\n      args:\n        - concat(['artist:artistid:', artistid])\n        - '`name`'\n      language: jmespath\n      field: artist\noutput:\n  - uses: redis.write\n    with:\n      connection: target\n      data_type: hash\n      key:\n        expression: concat(['album:albumid:', albumid])\n        language: jmespath",
      "section_id": "denormalizing-a-hash"
    },
    {
      "id": "denormalizing-a-hash-ex1",
      "language": "bash",
      "code": "> hgetall album:albumid:1\n1) \"albumid\"\n2) \"1\"\n3) \"title\"\n4) \"For Those About To Rock We Salute You\"\n5) \"artistid\"\n6) \"1\"",
      "section_id": "denormalizing-a-hash"
    },
    {
      "id": "denormalizing-a-hash-ex2",
      "language": "bash",
      "code": "> hgetall album:albumid:1\n1) \"albumid\"\n2) \"1\"\n3) \"title\"\n4) \"For Those About To Rock We Salute You\"\n5) \"artistid\"\n6) \"1\"\n7) \"artist\"\n8) \"AC/DC\"",
      "section_id": "denormalizing-a-hash"
    },
    {
      "id": "denormalizing-a-json-document-ex0",
      "language": "yaml",
      "code": "source:\n  table: album\ntransform:\n  - uses: redis.lookup\n    with:\n      connection: target\n      cmd: JSON.GET\n      args:\n        - concat(['artist:artistid:', artistid])\n      language: jmespath\n      field: artiststring\n  - uses: add_field\n    with:\n      field: artist\n      language: jmespath\n      expression: json_parse(artiststring)\n  - uses: remove_field\n    with:\n      fields:\n        - field: artistid\n        - field: artiststring\noutput:\n  - uses: redis.write\n    with:\n      connection: target\n      data_type: json\n      key:\n        expression: concat(['album:albumid:', albumid])\n        language: jmespath",
      "section_id": "denormalizing-a-json-document"
    },
    {
      "id": "denormalizing-a-json-document-ex1",
      "language": "json",
      "code": "{\n  \"albumid\": 239,\n  \"title\": \"War\",\n  \"artist\": {\n    \"artistid\": 150,\n    \"name\": \"U2\"\n  }\n}",
      "section_id": "denormalizing-a-json-document"
    }
  ]
}
