Kafka 0.8,是否可以使用Java代码通过分区和复制来创建主题? [英] Kafka 0.8, is it possible to create topic with partition and replication using java code?

查看:52
本文介绍了Kafka 0.8,是否可以使用Java代码通过分区和复制来创建主题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Kafka 0.8beta中,可以使用如下所述的命令来创建主题,如此处

In Kafka 0.8beta a topic can be created using a command like below as mentioned here

    bin/kafka-create-topic.sh --zookeeper localhost:2181 --replica 2 --partition 3 --topic test

以上命令将创建一个名为"test"的主题,该主题具有3个分区,每个分区2个副本.

the above command will create a topic named "test" with 3 partitions and 2 replicas per partition.

我可以使用Java做同样的事情吗?

Can I do the same thing using Java ?

到目前为止,我发现正在使用Java,我们可以创建一个生产器,如下所示

So far what I found is using Java we can create a producer as seen below

    Producer<String, String> producer = new Producer<String, String>(config);
    producer.send(new KeyedMessage<String, String>("mytopic", msg));

这将创建一个名为"mytopic"的主题,该主题具有使用"num.partitions"属性指定的分区数,并开始制作.

但是有没有办法定义分区和复制呢?我找不到任何这样的例子.如果不能,那意味着我们总是需要先创建具有分区和复制的主题(根据我们的要求),然后使用生产者在该主题内生成消息.例如,如果我想以相同的方式创建"mytopic",但使用不同数量的分区(覆盖num.partitions属性),是否有可能?

This will create a topic named "mytopic" with the number of partition specified using the "num.partitions" attribute and start producing.

But is there a way to define the partition and replication also ? I couldn't find any such example. If we can't then does that mean we always need to create topic with partitions and replication (as per our requirement) before and then use the producer to produce message within that topic. For example will it be possible if I want to create the "mytopic" the same way but with different number of partition (overriding the num.partitions attribute) ?

推荐答案

注意:我的回答涵盖的是Kafka 0.8.1+,即截至2014年4月的最新稳定版本.

是的,您可以通过Kafka API以编程方式创建主题.是的,您可以指定所需的分区数以及该主题的复制因子.

Yes, you can create a topic programatically via the Kafka API. And yes, you can specify the desired number of partitions as well as the replication factor for the topic.

请注意,最近发布的Kafka 0.8.1+提供的API与Kafka 0.8.0(Biks在其链接回复中使用的API)略有不同.我在回答问题在Kafka 0.8.1+ 中创建主题的代码示例.https://stackoverflow.com/questions/16946778/how-can-we-create-a-topic-in-kafka-from-the-ide-using-api/>如何从使用Biks上面提到的API 的IDE.

Note that the recently released Kafka 0.8.1+ provides a slightly different API than Kafka 0.8.0 (which was used by Biks in his linked reply). I added a code example to create a topic in Kafka 0.8.1+ to my reply to the question How Can we create a topic in Kafka from the IDE using API that Biks was referring to above.

这篇关于Kafka 0.8,是否可以使用Java代码通过分区和复制来创建主题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆