Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Changes per Jun's code review. Clarified logic when key is provided but no overloaded partitioner.class

...

The third property  "partitioner.class" defines what class to use to determine which Partition in the Topic the message is to be sent to. This is optional, but for any non-trivial implementation you are going to want to implement a partitioning scheme. More about the implementation of this class later. By default if you don't include If you include a value for the key but haven't defined a partitioner.class Kafka will randomly will use the default partitioner. If the key is null, then the Producer will assign the message to a partitionrandom Partition.

The last property "request.required.acks" tells Kafka that you want your Producer to require an acknowledgement from the Broker that the message was received. Without this setting the Producer will 'fire and forget' possibly leading to data loss.

...

Note that the Producer is a Java Generic and you need to tell it the type of two parameters. The first is the type of the messagePartition key, the second the type of the Partition keymessage. In this example they are both Strings, which also matches to what we defined in the Properties above.

...