{
  "id": "commandoverrider",
  "title": "CommandOverrider",
  "url": "https://redis.io/docs/latest/operate/oss_and_stack/stack-with-enterprise/deprecated-features/gears-v1/jvm/classes/readers/commandoverrider/",
  "summary": "Override a Redis command.",
  "tags": [
    "docs",
    "operate",
    "stack"
  ],
  "last_updated": "2026-04-22T11:55:45+02:00",
  "page_type": "content",
  "content_hash": "b1aef0a1dd58043fff36349dc0f7923eaab61a70dca360e44f060c6b09b3a1a3",
  "sections": [
    {
      "id": "overview",
      "title": "Overview",
      "role": "overview",
      "text": "The `CommandOverrider` allows you to override and customize Redis commands.\n\n1. Pass the `CommandOverrider` 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.\n\n\nIf you register code that uses `CommandOverrider`, its `reader` value is `\"CommandReader\"` when you run the `RG.DUMPREGISTRATIONS` command, not `\"CommandOverrider\"`."
    },
    {
      "id": "parameters",
      "title": "Parameters",
      "role": "parameters",
      "text": "| Name | Type | Description |\n|------|------|-------------|\n| command | string | The command to override |\n| prefix | string | Only override the command for keys that start with this string |"
    },
    {
      "id": "output-records",
      "title": "Output records",
      "role": "returns",
      "text": "Outputs a record with the command's name and arguments."
    },
    {
      "id": "example",
      "title": "Example",
      "role": "example",
      "text": "The following example shows how to override the `HSET` command so that it also adds a `last_modified` timestamp for \"user:\" hashes.\n\nAdd some user data as a hash:\n\n[code example]\n\nExample code:\n\n[code example]\n\nAfter you register the previous code with the `RG.JEXECUTE` command, try to update the user's data with `HSET` to test it:\n\n[code example]\n\nNow the user's profile should have the updated value for `posts` and a `last_modified` timestamp:\n\n[code example]"
    }
  ],
  "examples": [
    {
      "id": "example-ex0",
      "language": "sh",
      "code": "redis> HSET user:1 name \"morgan\" posts 201\n(integer) 2",
      "section_id": "example"
    },
    {
      "id": "example-ex1",
      "language": "java",
      "code": "// Create the reader that will pass data to the pipe\nCommandOverrider overrider = new CommandOverrider();\n// Override the HSET command\noverrider.setCommand(\"HSET\");\n// Only override HSET for keys that start with \"user:\"\noverrider.setPrefix(\"user:\");\n\n// Create the data pipe builder\nGearsBuilder.CreateGearsBuilder(overrider).map(r-> {\n    // Get the operation arguments\n    ArrayList<String> operationArguments = new ArrayList<String>();\n    for (int i = 1; i < r.length; i++) {\n        operationArguments.add(new String((byte[]) r[i], StandardCharsets.UTF_8));\n    }\n\n    // Add a last-modified field and a corresponding timestamp to the operation arguments\n    operationArguments.add(\"last-modified\");\n    operationArguments.add(Long.toString(System.currentTimeMillis()));\n\n    // Run the enriched HSET operation\n    Object reply = GearsBuilder.callNext(operationArguments.toArray(new String[0]));\n    \n    return reply.toString();\n}).register(ExecutionMode.SYNC);",
      "section_id": "example"
    },
    {
      "id": "example-ex2",
      "language": "sh",
      "code": "redis> HSET user:1 posts 234\n\"OK\"",
      "section_id": "example"
    },
    {
      "id": "example-ex3",
      "language": "sh",
      "code": "redis> HGETALL user:1\n1) \"name\"\n2) \"morgan\"\n3) \"posts\"\n4) \"234\"\n5) \"last_modified\"\n6) \"1643237927663\"",
      "section_id": "example"
    }
  ]
}
