简体   繁体   English

在Apache Kafka中设置多个分区

[英]Setting up multiple partitions in Apache Kafka

Im trying to set the no of partitions to 2 from the code,and i have single node setup, (1 zookeeper, 1kafka). 我试图从代码中将分区号设置为2,并且我有单节点设置(1个zookeeper,1kafka)。 when i consume the message i see that kafka is using only one partition to store the data, Do i need to make any modifications to the setup to have multiple partitions ? 当我使用该消息时,看到kafka仅使用一个分区来存储数据,是否需要对设置进行任何修改以具有多个分区?

 private void setupZookeeper(String[] topicList){

    ZkClient zkClient = null;
    ZkUtils zkUtils = null;
    try {
        String[] zookeeperHosts = {"localhost:2181"}; // If multiple zookeeper then -> String zookeeperHosts = "192.168.20.1:2181,192.168.20.2:2181";
        int sessionTimeOutInMs = 15 * 1000; // 15 secs
        int connectionTimeOutInMs = 10 * 1000; // 10 secs
        //String topicName = "testTopic";
        int noOfPartitions = 2;
        int noOfReplication = 1;

        for(String zookeeper:zookeeperHosts){

            zkClient = new ZkClient(zookeeper, sessionTimeOutInMs, connectionTimeOutInMs, ZKStringSerializer$.MODULE$);
            zkUtils = new ZkUtils(zkClient, new ZkConnection(zookeeper), false);
            for(String topicName: topicList){
                System.out.println("Setting no of partitions ="+noOfPartitions + "for topic" + topicName);
                AdminUtils.createTopic(zkUtils, topicName, noOfPartitions, noOfReplication, 
                         producerConfig(),RackAwareMode.Disabled$.MODULE$);
            }
        }



    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        if (zkClient != null) {
            zkClient.close();
        }
    }

My producerConfig, looks like the following: 我的producerConfig如下所示:

private Properties producerConfig() {
   Properties props = new Properties();
  props.put("bootstrap.servers", "localhost:9092");

  props.put("acks", "all");
  //props.put("retries", 0);
  props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
  props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");

return props;
}

when i consume the message i see that kafka is using only one partition to store the data 当我使用该消息时,我看到kafka仅使用一个分区来存储数据

The default message partitioning strategy as below, "only one partition used" may be caused by constant message key, same hash value calculated and route to only one partition. 如下所示的默认消息分区策略“仅使用一个分区”可能是由恒定的消息密钥,计算出的相同哈希值以及仅路由到一个分区引起的。

  • If a partition is specified in the record, use it; 如果记录中指定了分区,请使用它;
  • If no partition is specified but a key is present choose a partition based on a hash of the key; 如果未指定分区但存在密钥,则根据密钥的哈希值选择一个分区;
  • If no partition or key is present choose a partition in a round-robin fashion. 如果没有分区或密钥,则以循环方式选择一个分区。

you

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM