← Back to Pull requests

Kafka input format: ingest every field of a structured key with keyColumnPrefix #20839

Open
Mohith26 opened this pull request with 409 lines diff across 7 files
View on GitHub
Mohith26 Mohith26 commented on Nov 04, 2026 17:18 UTC
Author

Fixes #18578.

Description

KafkaInputFormat parses the Kafka key with keyFormat, but only keeps the first field of the parsed key (as keyColumnName). Topics with structured keys, such as a JSON or Avro key with a tenant id and an entity id, lose every field after the first one, so users have to duplicate key fields into the payload or pre-process the topic.

This adds an optional keyColumnPrefix property to the kafka input format. When it is set, every field of the parsed key is ingested as its own column, named keyColumnPrefix + fieldName:

"inputFormat": {
  "type": "kafka",
  "keyFormat": { "type": "json" },
  "keyColumnPrefix": "kafka.key.",
  "valueFormat": { "type": "json" }
}

A key of {"tenant": "acme", "userId": 42} becomes the columns kafka.key.tenant and kafka.key.userId.

Behavior details:

  • Not set (default): unchanged. Only the first key field is used, as keyColumnName. Specs that don't set the property serialize exactly as before (@JsonInclude(NON_NULL)).

  • A flattenSpec on keyFormat is applied first, so flattened fields are included.

  • Precedence matches the existing key column: header and record-metadata columns win over key columns with the same name, and payload columns win over both.

  • Prefixed key columns can be used as the timestampSpec column.

  • sample() returns the same key columns as read() (it uses the parsed key row, so flattened fields show up in the sampler too).

  • Null keys add no key columns.
  • Alternative considered: changing the default to ingest all key fields. I kept the default as is because it would change the schema of existing datasources on upgrade, and an empty prefix ("") already allows unprefixed key columns for users who want them.

    Release note

    The Kafka input format has a new optional keyColumnPrefix property. When set, Druid ingests every field of a structured Kafka key (for example a multi-field JSON or Avro key) as its own column instead of only the first field.

    <hr>

    ##### Key changed/added classes in this PR
    KafkaInputFormat
    KafkaInputReader

    <hr>

    This PR has:

  • been self-reviewed.

  • added documentation for new or modified features or behaviors.

  • a release note entry in the PR description.

  • added Javadocs for most classes and all non-trivial methods. Linked related entities via Javadoc links.

  • added comments explaining the "why" and the intent of the code wherever would not be obvious for an unfamiliar reader.

  • added unit tests or modified existing tests to cover new code paths, ensuring the threshold for code coverage is met.
  • Testing: mvn -pl extensions-core/kafka-indexing-service test (checkstyle and forbidden-apis pass). KafkaInputFormatTest has 9 new tests: serde, multi-field JSON key, key flattenSpec, CSV key, timestamp from a prefixed key field, precedence, null key, and sampling with and without a flattenSpec. The Testcontainers-based tests in the module (KafkaIndexTaskTest, KafkaSamplerSpecTest, the embedded supervisor tests) need Docker, which I don't have locally, so those rely on CI. The web console doesn't expose the new field yet; I can follow up with that if this approach looks right.

    This change was written with help from an AI coding assistant; I reviewed the code and ran the tests above.

    Diff: 409 lines (+387 / -22) across 7 files Reviewer: FrankChen021 Review duration: 8559m
    All Vouch review confidence checks passed
    Residual risk 0.05 = Change Risk (0.48) × (1 − Review Confidence (0.91)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.48 · 409 lines diff across 7 files
    0.48
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.93 · 2 substantive comment(s)
    0.93
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    90%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.360
    Real PR Conversation Timeline
    4 items
    FrankChen021 FrankChen021 commented on code on Nov 05, 2026 12:43 UTC
    Code Review
    extensions-core/kafka-indexing-service/src/main/java/org/apache/druid/data/input/kafkainput/KafkaInputFormat.java:1
    @@ -73,7 +76,8 @@ public KafkaInputFormat(
           @JsonProperty("timestampColumnName") @Nullable String timestampColumnName,
           @JsonProperty("topicColumnName") @Nullable String topicColumnName,
           @JsonProperty("partitionColumnName") @Nullable String partitionColumnName,
    -      @JsonProperty("offsetColumnName") @Nullable String offsetColumnName
    +      @JsonProperty("offsetColumnName") @Nullable String offsetColumnName,
    +      @JsonProperty("keyColumnPrefix") @Nullable String keyColumnPrefix

    [P2] Preserve the existing public constructor

    Finding: Appending keyColumnPrefix removes the previous public KafkaInputFormat constructor signature, so Java callers compiled against the prior release can fail with NoSuchMethodError or fail to compile even when they do not use the new option. The corresponding public KafkaInputReader constructor is changed in the same way.

    Suggestion: Retain overloads for the previous KafkaInputFormat and KafkaInputReader signatures, delegating to the new constructors with a null prefix.

    FrankChen021 FrankChen021 commented on Nov 05, 2026 12:43 UTC
    Reviewer

    🟡 Changes recommended

    Preserve the existing public constructor signatures before merging. Adding the optional prefix currently breaks direct Java or custom-extension callers even when keyColumnPrefix is unset.

    Reviewed 7 of 7 changed files; findings in 1 file.

    Validation: git diff --no-ext-diff --check 7e300b9ccf6cca26d7bbe632f790415b31f123c1 d2ff79db70f2b5c398a10247e23ee6cf77b2094b passed. No build or tests were run.

    | Severity | Findings |
    | --- | ---: |
    | P0 | 0 |
    | P1 | 0 |
    | P2 | 1 |
    | P3 | 0 |
    | Total | 1 |

    <!-- mergelens:review -->

    ---

    This is an automated review by Codex GPT-5.6-Luna(max)

    After addressing the findings or replying to the comments, you can request another review from me to trigger a new automated review.

    FrankChen021 FrankChen021 commented on code on Nov 05, 2026 13:31 UTC
    Code Review
    extensions-core/kafka-indexing-service/src/main/java/org/apache/druid/data/input/kafkainput/KafkaInputFormat.java:1
    @@ -73,7 +76,8 @@ public KafkaInputFormat(
           @JsonProperty("timestampColumnName") @Nullable String timestampColumnName,
           @JsonProperty("topicColumnName") @Nullable String topicColumnName,
           @JsonProperty("partitionColumnName") @Nullable String partitionColumnName,
    -      @JsonProperty("offsetColumnName") @Nullable String offsetColumnName
    +      @JsonProperty("offsetColumnName") @Nullable String offsetColumnName,
    +      @JsonProperty("keyColumnPrefix") @Nullable String keyColumnPrefix

    can ignore this finding from AI

    Reviewers
    Assignees
    Mohith26 Mohith26
    Labels
    risk: low confidence: 90.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 47.8% (MEDIUM) · Higher = larger blast radius
    47.8%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 90.5% · Lower = rushed or shallow review
    90.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 4.5% · Safe to merge
    4.5%
    Repository