First of all, the benefits of partition: partition the benefits of concurrent access to similar data, improve efficiency.
The first step is to implement the Partitioner object.
public
class
ProducerPartitioner
implements
Partitioner<String> {
public
static
final
Logger LOG=LoggerFactory.getLogger(UserInfo.
class
);
@Override
public
int
partition(String key,
int
numPartitions) {
LOG.info(
"ProducerPartitioner key:"
+key+
" partitions:"
+numPartitions);
return
key.length() % numPartitions;
}
}
In the partition method above, it is worth noting that key we set when constructing the data sending object, which is the key to differentiate the storage, for example I want to store my data in different user classes. The second step is to construct the data when it is set:
/**选择用哪个类来进行设置partition*/
props.put(
"partitioner.class"
,
"org.kafka.partitioner.ProducerPartitioner"
);
/** Constructing data sending object **/
ProducerData<String,UserInfo> data=
new
ProducerData<String, UserInfo
>(
"test"
,
"developerTest"
, msg);
The configuration of the partition is added, and the parameters of the producerdata are modified, where the middle is key (Developertest), and if the Partition,kafka is not set, the request is randomly sent to the broker.
sum up so much today!
Simple code implementation of partition allocation strategy