Package org.apache.flink.table.api
Class InsertConflictStrategy
- java.lang.Object
-
- org.apache.flink.table.api.InsertConflictStrategy
-
- All Implemented Interfaces:
Serializable
@PublicEvolving public final class InsertConflictStrategy extends Object implements Serializable
Defines the conflict resolution strategies for INSERT INTO statements when the query's upsert key differs from the target table's primary key.The upsert key is derived from the query's semantics (e.g., GROUP BY columns). When it differs from the target table's primary key, multiple records from the query may map to the same primary key, causing a conflict. For example:
CREATE TABLE user_totals ( user_id BIGINT, category STRING, total DECIMAL(10, 2), PRIMARY KEY (user_id) NOT ENFORCED ); -- Upsert key is (user_id, category), but primary key is just (user_id). -- Updates for (user_id=1, category='A') and (user_id=1, category='B') -- both target the same primary key row, causing a conflict. INSERT INTO user_totals SELECT user_id, category, SUM(amount) as total FROM orders GROUP BY user_id, category ON CONFLICT DO NOTHING;These strategies are used with the ON CONFLICT clause:
ON CONFLICT DO ERROR- Throw an exception on primary key constraint violationON CONFLICT DO NOTHING- Keep the first record, ignore subsequent conflictsON CONFLICT DO DEDUPLICATE- Maintain history for rollback (current behavior)
Use the static factory methods to create instances:
InsertConflictStrategy.error() InsertConflictStrategy.nothing() InsertConflictStrategy.deduplicate()Or use the builder for more complex configurations:
InsertConflictStrategy.newBuilder() .withBehavior(ConflictBehavior.DEDUPLICATE) .build()- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classInsertConflictStrategy.BuilderBuilder for creatingInsertConflictStrategyinstances.static classInsertConflictStrategy.ConflictBehaviorDefines the basic conflict resolution behaviors.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static InsertConflictStrategydeduplicate()Creates a strategy that maintains the full history of changes for each primary key to support rollback on retraction.booleanequals(Object o)static InsertConflictStrategyerror()Creates a strategy that throws an exception when multiple distinct records arrive for the same primary key after watermark compaction.InsertConflictStrategy.ConflictBehaviorgetBehavior()Returns the conflict behavior of this strategy.inthashCode()static InsertConflictStrategy.BuildernewBuilder()Creates a new builder for constructing anInsertConflictStrategy.static InsertConflictStrategynothing()Creates a strategy that keeps the first record that arrives for a given primary key and discards subsequent conflicts.StringtoString()
-
-
-
Method Detail
-
error
public static InsertConflictStrategy error()
Creates a strategy that throws an exception when multiple distinct records arrive for the same primary key after watermark compaction.- Returns:
- the ERROR conflict strategy
-
nothing
public static InsertConflictStrategy nothing()
Creates a strategy that keeps the first record that arrives for a given primary key and discards subsequent conflicts.- Returns:
- the NOTHING conflict strategy
-
deduplicate
public static InsertConflictStrategy deduplicate()
Creates a strategy that maintains the full history of changes for each primary key to support rollback on retraction. This is the current default behavior.- Returns:
- the DEDUPLICATE conflict strategy
-
newBuilder
public static InsertConflictStrategy.Builder newBuilder()
Creates a new builder for constructing anInsertConflictStrategy.- Returns:
- a new builder instance
-
getBehavior
public InsertConflictStrategy.ConflictBehavior getBehavior()
Returns the conflict behavior of this strategy.- Returns:
- the conflict behavior
-
-