All posts

Hudi key generators: the one decision you cannot change later

The key generator decides what a record's identity is and where it lands on disk. Six of them measured side by side, including the two that produce different keys for the same field, and what happens when you try to switch one on a table that already exists.

7 min read Hudi

TL;DR

  • The key generator turns a row into two things: the record key that decides identity for upserts, and the partition path that decides where it is written.
  • SimpleKeyGenerator and ComplexKeyGenerator produce different keys for the same field. Simple gives 1; Complex gives id:1, with the field name as part of the key.
  • That holds even with a single field, so choosing Complex “in case I add a field later” silently changes your key format from day one.
  • You cannot switch afterwards. Hudi records the generator in table config and refuses a mismatched write outright: Config conflict(key, current value, existing value): KeyGenerator. Without that check, every upsert would insert a new record.
  • hive_style_partitioning changes the path from IN to country=IN. It is a layout decision that affects every engine reading the table afterwards.
  • TimestampBasedKeyGenerator turns an epoch column into a date path (1772323200000 becomes 2026/03/01) and fails the write if you omit its output format.

You can retune compaction or file sizes on a live Hudi table. The key generator is fixed by the first write.

It defines which rows count as the same record and how the directories are laid out, so decide it before that write.

What a key generator produces

Every Hudi record carries metadata columns, and two of them come from the key generator:

An incoming row passes through the key generator to produce _hoodie_record_key and _hoodie_partition_path The key generator turns a row into an identity and a location.

The record key is what the index looks up to decide whether an incoming row is an insert or an update. The partition path is the directory. Get either wrong and the table is wrong in a way that is expensive to fix.

Six configurations, measured

The same three rows, (1, IN, mumbai), (2, IN, delhi) and (3, US, austin), were written six ways, and the two metadata columns were read back.

Configuration _hoodie_record_key _hoodie_partition_path
SimpleKeyGenerator on id, partition country 1 IN
ComplexKeyGenerator on id,country, partition country,city id:1,country:IN IN/mumbai
ComplexKeyGenerator on id alone id:1 IN
NonpartitionedKeyGenerator on id 1 `` (empty)
SimpleKeyGenerator + hive_style_partitioning=true 1 country=IN
TimestampBasedKeyGenerator on an epoch-millis column 1 2026/03/01

Compare the first and third rows: the same field id gives two different keys.

Simple and Complex are not interchangeable

For the same single field id, the two generators write different keys:

SimpleKeyGenerator    ->  '1'
ComplexKeyGenerator   ->  'id:1'

Complex encodes the field name into the key. That lets it compose several fields unambiguously (id:1,country:IN cannot be confused with a different pairing), and it makes the format different from Simple’s, even for one field.

The practical consequence catches people who are being careful. “I will use Complex even though I only have one key field, so I can add another later without changing anything” produces id:1 from the first write onwards. Adding a second field later changes the key again, to id:1,country:IN, so the flexibility you thought you bought is not there either.

Pick Simple for a single key field. Pick Complex when you genuinely have a composite key.

Starting with Complex does not keep your options open: it fixes a different key format from the first write, and adding a field changes the key again anyway.

You cannot change it later

If you write a table with Simple and then upsert the same rows with Complex, Hudi refuses the write:

org.apache.hudi.exception.HoodieException: Config conflict(key, current value, existing value):
KeyGenerator:  org.apache.hudi.keygen.ComplexKeyGenerator  org.apache.hudi.keygen.SimpleKeyGenerator

The generator is stored in hoodie.properties as part of the table’s identity. If the write were allowed, every incoming record would compute a key that matches nothing in the index, so every update would become an insert and the table would quietly double. The error stops that before it happens.

When you do need a different key, write a new table with the new configuration and backfill it. Hudi rejects the change on a normal write, so plan for that cost.

The partition field is part of the same recorded configuration and is also refused. A new partition layout means a new table.

Partition path shapes

Hive-style partitioning changes the directory name from the bare value to a key=value pair:

hive_style_partitioning = false   ->   IN/
hive_style_partitioning = true    ->   country=IN/

The key=value form is what Hive, Spark and Trino discover automatically when reading a directory tree directly, so it is the friendlier layout for anything reading outside Hudi’s own catalog integration. Like the generator, it is a property of how the table was written; changing it later means new partitions in one shape and old ones in another.

Nonpartitioned gives an empty partition path and a flat table. A table of a few million rows with no natural low-cardinality partition column is better flat than partitioned on a high-cardinality one, which creates many small files.

Multiple partition fields nest, in the order given: partitionpath.field = country,city yields IN/mumbai.

Partitioning on a high-cardinality column is the more common and more expensive mistake. A flat table is fine for many workloads.

Timestamps into dates

To partition by day from a timestamp column, use TimestampBasedKeyGenerator:

"hoodie.datasource.write.partitionpath.field": "event_ms",
"hoodie.datasource.write.keygenerator.class":
    "org.apache.hudi.keygen.TimestampBasedKeyGenerator",
"hoodie.keygen.timebased.timestamp.type": "EPOCHMILLISECONDS",
"hoodie.keygen.timebased.output.dateformat": "yyyy/MM/dd",
"hoodie.keygen.timebased.timezone": "UTC",
1772323200000  ->  2026/03/01
1772409600000  ->  2026/03/02

Check these settings:

  • timestamp.type must match the column. EPOCHMILLISECONDS, UNIX_TIMESTAMP (seconds), DATE_STRING, SCALAR and so on. Feeding seconds to a milliseconds configuration produces dates in 1970 with no error.
  • output.dateformat is required. Omitting it fails the write rather than producing something unusable, which is the better outcome but surprises people who expect a default.
  • Set timezone explicitly. Otherwise the partition a row lands in depends on the JVM’s default timezone, so the same data written from two clusters can land in different days. The symptom is rows appearing in the previous day’s partition.

Which one to use

Situation Generator
Single key field, single partition field SimpleKeyGenerator
Composite key, or multiple partition fields ComplexKeyGenerator
No sensible partition column NonpartitionedKeyGenerator
Partition by day from a timestamp TimestampBasedKeyGenerator
Partition transform that none of the above expresses CustomKeyGenerator, or your own

CustomKeyGenerator gives each field a type, simple or timestamp, so one generator can produce country/2026/03/01. In a test with country:SIMPLE,event_ms:TIMESTAMP, the write succeeded but a default read failed with Failed to parse partition column values from the partition-path; reading with hoodie.datasource.read.file.index.listing.mode=eager returned the rows. Writing your own is supported and rarely necessary.

A note on global versus non-global indexes, because the two decisions interact. With a non-global index, uniqueness of the record key is only enforced within a partition, so the same key can exist in two partitions and an update that changes the partition value produces a duplicate rather than a move. A global index enforces uniqueness across the whole table and can handle the move, at the cost of a more expensive lookup. If your partition column can change for a given record, that pushes you toward a global index, which is a key design decision as much as an index one.

Make that choice together with the key design.

Key takeaways

The key generator fixes what counts as the same record and what the directory tree looks like. Hudi rejects a change on a later write, because every update would otherwise become an insert.

Before the first write, if you have one key field, use SimpleKeyGenerator. Reaching for ComplexKeyGenerator to keep options open gives you id:1 instead of 1 permanently and does not actually keep the options open.

References

Trademarks

Apache Hudi, Apache Spark, Apache Hive, Apache Iceberg, Apache and the Apache feather logo are either registered trademarks or trademarks of The Apache Software Foundation in the United States and other countries. Trino is a trademark of the Trino Software Foundation.

Found this useful?

These posts and tools are free. If one saved you an afternoon, you can buy me a coffee.

Buy me a coffee

Comments

Questions, corrections and reviews are welcome. Sign in with GitHub to post; comments are stored in this site's public GitHub Discussions.