Package org.apache.flink.table.api
Class InsertConflictStrategy
java.lang.Object
org.apache.flink.table.api.InsertConflictStrategy
- All Implemented Interfaces:
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classBuilder for creatingInsertConflictStrategyinstances.static enumDefines the basic conflict resolution behaviors. -
Method Summary
Modifier and TypeMethodDescriptionstatic InsertConflictStrategyCreates a strategy that maintains the full history of changes for each primary key to support rollback on retraction.booleanstatic InsertConflictStrategyerror()Creates a strategy that throws an exception when multiple distinct records arrive for the same primary key after watermark compaction.Returns the conflict behavior of this strategy.inthashCode()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.toString()
-
Method Details
-
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
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
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
Creates a new builder for constructing anInsertConflictStrategy.- Returns:
- a new builder instance
-
getBehavior
Returns the conflict behavior of this strategy.- Returns:
- the conflict behavior
-
equals
-
hashCode
public int hashCode() -
toString
-