-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Improve][Connector-V2][Kafka Sink]custom partition #2889
Closed
+192
−6
Closed
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
5b1ee04
custom partition
TaoZex ea581e0
update kafka sink doc
TaoZex 572a9af
update
TaoZex b967635
Merge branch 'dev' into par
TaoZex 8442fd7
update connector V2 common-options
TaoZex 078aabb
Update kafka.md
TaoZex 8614fcc
Merge branch 'apache:dev' into par
TaoZex a461055
Merge branch 'apache:dev' into par
TaoZex de2fe6b
Merge branch 'par' of https://github.com/TaoZex/incubator-seatunnel i…
TaoZex 2f09ed7
update
TaoZex 4f8ec2c
Revert "update connector V2 common-options"
TaoZex c0a54c8
update
TaoZex 0e11275
update
TaoZex 3dfbd08
update
TaoZex 32ca0b3
update
TaoZex 9c3d016
update
TaoZex 90dc08f
Merge branch 'apache:dev' into par
TaoZex eb50ffd
update
TaoZex 3527313
Merge remote-tracking branch 'origin/par' into par
TaoZex 51b9229
Merge branch 'apache:dev' into par
TaoZex 189e987
update
TaoZex 0f92c00
update
TaoZex 9e3a082
update
TaoZex dea8966
update
TaoZex b145fa9
Merge branch 'apache:dev' into par
TaoZex 8cb6087
update
TaoZex dee3a8c
Merge branch 'apache:dev' into par
TaoZex 8ddb6dc
Merge branch 'apache:dev' into par
TaoZex 9e83250
Merge branch 'apache:dev' into par
TaoZex File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# Kafka | ||
|
||
> Kafka sink connector | ||
## Description | ||
|
||
Write Rows to a Kafka topic. | ||
|
||
## Key features | ||
|
||
- [x] [exactly-once](../../concept/connector-v2-features.md) | ||
|
||
By default, we will use 2pc to guarantee the message is sent to kafka exactly once. | ||
|
||
- [ ] [schema projection](../../concept/connector-v2-features.md) | ||
|
||
## Options | ||
|
||
| name | type | required | default value | | ||
| ------------------ | ---------------------- | -------- | ------------- | | ||
| topic | string | yes | - | | ||
| bootstrap.servers | string | yes | - | | ||
| kafka.* | kafka producer config | no | - | | ||
| semantic | string | no | NON | | ||
| partition | int | no | - | | ||
| assign_partitions | list | no | - | | ||
| transaction_prefix | string | no | - | | ||
| common-options | | no | - | | ||
|
||
### topic [string] | ||
|
||
Kafka Topic. | ||
|
||
### bootstrap.servers [string] | ||
|
||
Kafka Brokers List. | ||
|
||
### kafka.* [kafka producer config] | ||
|
||
In addition to the above parameters that must be specified by the `Kafka producer` client, the user can also specify multiple non-mandatory parameters for the `producer` client, covering [all the producer parameters specified in the official Kafka document](https://kafka.apache.org/documentation.html#producerconfigs). | ||
|
||
The way to specify the parameter is to add the prefix `kafka.` to the original parameter name. For example, the way to specify `request.timeout.ms` is: `kafka.request.timeout.ms = 60000` . If these non-essential parameters are not specified, they will use the default values given in the official Kafka documentation. | ||
|
||
### semantic [string] | ||
|
||
Semantics that can be chosen EXACTLY_ONCE/AT_LEAST_ONCE/NON, default NON. | ||
|
||
In EXACTLY_ONCE, producer will write all messages in a Kafka transaction that will be committed to Kafka on a checkpoint. | ||
|
||
In AT_LEAST_ONCE, producer will wait for all outstanding messages in the Kafka buffers to be acknowledged by the Kafka producer on a checkpoint. | ||
|
||
NON does not provide any guarantees: messages may be lost in case of issues on the Kafka broker and messages may be duplicated. | ||
|
||
### partition [int] | ||
|
||
We can specify the partition, all messages will be sent to this partition. | ||
|
||
### assign_partitions [list] | ||
|
||
We can decide which partition to send based on the content of the message. The function of this parameter is to distribute information. | ||
|
||
For example, there are five partitions in total, and the assign_partitions field in config is as follows: | ||
assign_partitions = ["shoe", "clothing"] | ||
|
||
Then the message containing "shoe" will be sent to partition zero ,because "shoe" is subscripted as zero in assign_partitions, and the message containing "clothing" will be sent to partition one.For other messages, the hash algorithm will be used to divide them into the remaining partitions. | ||
|
||
This function by `MessageContentPartitioner` class implements `org.apache.kafka.clients.producer.Partitioner` interface.If we need custom partitions, we need to implement this interface as well. | ||
|
||
### transaction_prefix [string] | ||
|
||
If semantic is specified as EXACTLY_ONCE, the producer will write all messages in a Kafka transaction. | ||
Kafka distinguishes different transactions by different transactionId. This parameter is prefix of kafka transactionId, make sure different job use different prefix. | ||
|
||
### common options | ||
|
||
Sink plugin common parameters, please refer to [Sink Common Options](common-options.md) for details. | ||
|
||
## Examples | ||
|
||
```hocon | ||
sink { | ||
|
||
kafka { | ||
topic = "seatunnel" | ||
bootstrap.servers = "localhost:9092" | ||
partition = 3 | ||
kafka.request.timeout.ms = 60000 | ||
semantics = EXACTLY_ONCE | ||
} | ||
|
||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
.../java/org/apache/seatunnel/connectors/seatunnel/kafka/sink/MessageContentPartitioner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.seatunnel.connectors.seatunnel.kafka.sink; | ||
|
||
import org.apache.kafka.clients.producer.Partitioner; | ||
import org.apache.kafka.common.Cluster; | ||
import org.apache.kafka.common.PartitionInfo; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class MessageContentPartitioner implements Partitioner { | ||
private static List<String> ASSIGNPARTITIONS; | ||
|
||
public static void setAssignPartitions(List<String> assignPartitionList) { | ||
ASSIGNPARTITIONS = assignPartitionList; | ||
} | ||
|
||
@Override | ||
public int partition(String topic, Object key, byte[] keyBytes, Object value, byte[] valueBytes, Cluster cluster) { | ||
List<PartitionInfo> partitions = cluster.partitionsForTopic(topic); | ||
int numPartitions = partitions.size(); | ||
|
||
int assignPartitionsSize = ASSIGNPARTITIONS.size(); | ||
String message = new String(valueBytes); | ||
for (int i = 0; i < assignPartitionsSize; i++) { | ||
if (message.contains(ASSIGNPARTITIONS.get(i))) { | ||
return i; | ||
} | ||
} | ||
//Choose one of the remaining partitions according to the hashcode. | ||
return ((message.hashCode() & Integer.MAX_VALUE) % (numPartitions - assignPartitionsSize)) + assignPartitionsSize; | ||
} | ||
|
||
@Override | ||
public void close() { | ||
|
||
} | ||
|
||
@Override | ||
public void configure(Map<String, ?> map) { | ||
|
||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove