-
Notifications
You must be signed in to change notification settings - Fork 102
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
produce messages partitioned by value #115
Merged
Merged
Changes from 3 commits
Commits
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 |
---|---|---|
@@ -1,2 +1,4 @@ | ||
/kt | ||
/quickfix | ||
.idea/* | ||
.vscode/* |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,7 +53,7 @@ func (cmd *produceCmd) read(as []string) produceArgs { | |
flags.BoolVar(&args.literal, "literal", false, "Interpret stdin line literally and pass it as value, key as null.") | ||
flags.StringVar(&args.version, "version", "", "Kafka protocol version") | ||
flags.StringVar(&args.compression, "compression", "", "Kafka message compression codec [gzip|snappy|lz4] (defaults to none)") | ||
flags.StringVar(&args.partitioner, "partitioner", "", "Optional partitioner to use. Available: hashCode") | ||
flags.StringVar(&args.partitioner, "partitioner", "", "Optional partitioner to use. Available: hashCode, hashCodeByValue") | ||
flags.StringVar(&args.decodeKey, "decodekey", "string", "Decode message value as (string|hex|base64), defaults to string.") | ||
flags.StringVar(&args.decodeValue, "decodevalue", "string", "Decode message value as (string|hex|base64), defaults to string.") | ||
flags.IntVar(&args.bufferSize, "buffersize", 16777216, "Buffer size for scanning stdin, defaults to 16777216=16*1024*1024.") | ||
|
@@ -320,11 +320,16 @@ func (cmd *produceCmd) deserializeLines(in chan string, out chan message, partit | |
} | ||
|
||
var part int32 = 0 | ||
if msg.Key != nil && cmd.partitioner == "hashCode" { | ||
part = hashCodePartition(*msg.Key, partitionCount) | ||
} | ||
if msg.Partition == nil { | ||
if msg.Value != nil && cmd.partitioner == "hashCodeByValue" { | ||
part = hashCodePartition(*msg.Value, partitionCount) | ||
msg.Partition = &part | ||
}else { | ||
if msg.Key != nil && cmd.partitioner == "hashCode" { | ||
part = hashCodePartition(*msg.Key, partitionCount) | ||
} | ||
if msg.Partition == nil { | ||
msg.Partition = &part | ||
} | ||
} | ||
|
||
out <- msg | ||
|
@@ -519,6 +524,12 @@ like the following: | |
In case the input line cannot be interpeted as a JSON object the key and value | ||
both default to the input line and partition to 0. | ||
|
||
If you don't want to specify key for single message, in other words, it doesn't matter that a message goes | ||
to a random paritition (with equal probability), you can set the flag '-partitioner' with 'hashCodeByValue'. | ||
That will tell kt to take the value of a message to calculate a hashcode deciding which paritition it will go to. | ||
This can be helpful when you just want there are many messages distributed in partitions of a topic, and don't | ||
care about what the content is. | ||
|
||
Examples: | ||
|
||
Send a single message with a specific key: | ||
|
@@ -529,6 +540,11 @@ Send a single message with a specific key: | |
$ kt consume -topic greetings -timeout 1s -offsets 0:3- | ||
{"partition":0,"offset":3,"key":"id-23","message":"ola"} | ||
|
||
Send a single message without specified key: | ||
$ echo 'no key specified message' | kt produce -topic greetings -partitioner hashCodeByValue | ||
Sent message to a partition decided by your case | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. awesome! thanks for adding an example and doc in general! |
||
Keep reading input from stdin until interrupted (via ^C). | ||
|
||
$ kt produce -topic greetings | ||
|
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.
could you please run gofmt or your alternative on this? 🙏
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.
and would it make sense to group the guards slightly differently?
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.
ok, I will do it