java消息分发器_Kafka Producer Java API不会将消息分发到所有主题分区

我是Kafka的新手,今天我尝试创建Java Producer来生成关于不同分区的Kafka主题的消息 .

首先,我创建了一个包 raggieKafka ,在其下创建了两个类: TestProducer 和 SimplePartitioner .

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 prod = new Producer(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 data = new KeyedMessage("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 生产环境 者不会将我的所有消息分发给其他分区吗?

事实上,我看了谷歌,然后再添加一个属性:

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

但仍然 生产环境 者不会为所有主题生成消息 .

请帮忙 .

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值