{
  "id": "commandreader",
  "title": "CommandReader",
  "url": "https://redis.io/docs/latest/operate/oss_and_stack/stack-with-enterprise/deprecated-features/gears-v1/jvm/classes/readers/commandreader/",
  "summary": "Run RedisGears functions on command.",
  "tags": [
    "docs",
    "operate",
    "stack"
  ],
  "last_updated": "2026-04-22T11:55:45+02:00",
  "page_type": "content",
  "content_hash": "02f0c1a8f72d451f32b3ce29a16a1c21dc7227dd294104bf31312d284d515c8f",
  "sections": [
    {
      "id": "overview",
      "title": "Overview",
      "role": "overview",
      "text": "The `CommandReader` allows you to run RedisGears functions on command when you:\n\n1. Pass the `CommandReader` to the [`GearsBuilder.CreateGearsBuilder()`](https://redis.io/docs/latest/operate/oss_and_stack/stack-with-enterprise/deprecated-features/gears-v1/jvm/classes/gearsbuilder/creategearsbuilder) function in your Java code.\n1. Call the [`register()`](https://redis.io/docs/latest/operate/oss_and_stack/stack-with-enterprise/deprecated-features/gears-v1/jvm/classes/gearsbuilder/register) function.\n1. Run `RG.JEXECUTE` to register your code.\n1. Use `RG.TRIGGER` to run your code on command:\n\n    [code example]"
    },
    {
      "id": "parameters",
      "title": "Parameters",
      "role": "parameters",
      "text": "| Name | Type | Description |\n|------|------|-------------|\n| trigger | string | The command name that triggers the registered RedisGears functions to run |"
    },
    {
      "id": "output-records",
      "title": "Output records",
      "role": "returns",
      "text": "Outputs a record with the command trigger's name and arguments."
    },
    {
      "id": "example",
      "title": "Example",
      "role": "example",
      "text": "The following example shows how to create a custom command to update an item's stock. It also adds a timestamp to track when the last restock occurred.\n\nAdd a hash to the database that represents an inventory item:\n\n[code example]\n\nExample code:\n\n[code example]\n\nAfter you register the previous code with the `RG.JEXECUTE` command, run `RG.TRIGGER` to test it:\n\n[code example]\n\nThe item now has the updated value for `stock` and a `last_restocked` timestamp:\n\n[code example]"
    }
  ],
  "examples": [
    {
      "id": "overview-ex0",
      "language": "sh",
      "code": "RG.TRIGGER <Trigger name> [arg1 arg2 ...]",
      "section_id": "overview"
    },
    {
      "id": "example-ex0",
      "language": "sh",
      "code": "redis> HSET inventory:headphones:1 color \"blue\" stock 5 price 30.00\n(integer) 3",
      "section_id": "example"
    },
    {
      "id": "example-ex1",
      "language": "java",
      "code": "// Create the reader that will pass data to the pipe\nCommandReader reader = new CommandReader();\n// Set the name of the custom command\nreader.setTrigger(\"Restock\");\n        \n// Create the data pipe builder\nGearsBuilder.CreateGearsBuilder(reader).map(r-> {\n    // Parse the command arguments to get the key name and new stock value\n    String itemKey = new String((byte[]) r[1], StandardCharsets.UTF_8);\n    String newStock = new String((byte[]) r[2], StandardCharsets.UTF_8);\n        \t\n    // Update the item's stock and add a timestamp\n    GearsBuilder.execute(\"HSET\", itemKey , \"stock\", newStock,\n        \t\t\t\"last_restocked\", Long.toString(System.currentTimeMillis()));\n        \t\n    return \"OK restocked \" + itemKey;\n}).register();",
      "section_id": "example"
    },
    {
      "id": "example-ex2",
      "language": "sh",
      "code": "redis> RG.TRIGGER Restock inventory:headphones:1 20\n1) \"OK restocked inventory:headphones:1\"",
      "section_id": "example"
    },
    {
      "id": "example-ex3",
      "language": "sh",
      "code": "redis> HGETALL inventory:headphones:1\n1) \"color\"\n2) \"blue\"\n3) \"stock\"\n4) \"20\"\n5) \"price\"\n6) \"30.00\"\n7) \"last_restocked\"\n8) \"1643232394078\"",
      "section_id": "example"
    }
  ]
}
