【问题标题】:Kafka Producer Java API is not distributing messages to all topic partitionsKafka Producer Java API 未将消息分发到所有主题分区
【发布时间】:2016-10-15 08:20:18
【问题描述】:

我对 Kafka 非常陌生,今天我尝试创建 Java Producer 以在不同分区上生成有关 Kafka 主题的消息。

首先我创建了一个包raggieKafka,在该包下我创建了两个类:TestProducerSimplePartitioner

TestProducer 类有如下代码:

package raggieKafka;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
import kafka.javaapi.producer.Producer;
import kafka.producer.KeyedMessage;
import kafka.producer.ProducerConfig;

public class TestProducer{

    public static void main(String args[]) throws Exception
    {
        long events = 0;

        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        events = Integer.parseInt(reader.readLine());
        Random rnd = new Random();

        Properties props = new Properties();
        props.put("metadata.broker.list", "localhost:9092");
        props.put("topic.metadata.refresh.interval.ms", "1");
        props.put("serializer.class", "kafka.serializer.StringEncoder");
        props.put("partitioner.class", "raggieKafka.SimplePartitioner");
        props.put("request.required.acks", "1");

        ProducerConfig config = new ProducerConfig(props);
        Producer<String, String> prod = new Producer<String, String>(config);

        for(long i = 0; i < events; i++)
        {
            long runtime = new Date().getTime();
            String ip = "192.168.2." + rnd.nextInt(255);
            String msg = runtime + ",www.example.com, " + ip;
            KeyedMessage<String,String> data = new KeyedMessage<String, String>("page_visits", ip, msg);
            prod.send(data);
        }
        prod.close();
    }
}

SimplePartitioner 类有以下代码:

package raggieKafka;

import kafka.producer.Partitioner;
import kafka.utils.VerifiableProperties;

public class SimplePartitioner implements Partitioner{

    public SimplePartitioner(VerifiableProperties props)
    {

    }

    public int partition(Object Key, int a_numPartitions)
    {
        int partition = 0;
        String stringKey = (String) Key;
        int offset = stringKey.indexOf(stringKey);

        if(offset > 0)
        {
            partition = Integer.parseInt(stringKey.substring(offset+1)) % a_numPartitions;
        }
        return partition;
    }   
}

在编译这些 Java 程序之前,我在 Kafka Broker 上创建了主题:

C:\kafka_2.11-0.9.0.1>.\bin\windows\kafka-topics.bat --create --topic page_visit
s --zookeeper localhost:2181 --partitions 5 --replication-factor 1
WARNING: Due to limitations in metric names, topics with a period ('.') or under
score ('_') could collide. To avoid issues it is best to use either, but not bot
h.
Created topic "page_visits".

现在,当我编译 java 程序时,它会将所有消息仅放入 1 个分区,即 page_visits-0,所有消息都发布在该分区下,但其余所有其他分区保持为空。

谁能告诉我为什么我的 Java 生产者没有将我所有的消息分发到其他分区?

事实上,我在 google 上查了一下,然后又添加了一个属性:

props.put("topic.metadata.refresh.interval.ms", "1");

但 Producer 仍然没有为所有主题生成消息。

请帮忙。

【问题讨论】:

    标签: apache-kafka kafka-producer-api


    【解决方案1】:

    您的 SimplePartitioner 代码在以下行中有错误

    int offset = stringKey.indexOf(stringKey);
    

    它总是返回0,所以你的偏移量总是等于0,因为它永远不会大于0,你的if块将不会被执行。最后它总是返回你的分区0

    解决方案:由于您的密钥是 ip 地址,因此以下更改可以按预期工作。

    int offset = stringKey.lastIndexOf('.');
    

    希望这会有所帮助!

    【讨论】:

    • 非常感谢 avr。我犯了一个多么愚蠢的错误,几乎让我发疯。再次感谢您的指正。干杯。
    猜你喜欢
    • 2020-01-27
    • 1970-01-01
    • 2019-02-14
    • 2018-10-27
    • 1970-01-01
    • 2016-04-18
    • 2017-04-29
    • 2019-08-18
    • 1970-01-01
    相关资源
    最近更新 更多