Simple code implementation of partition allocation strategy

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.