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

查看:31
本文介绍了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+ 提供了与 Kafka 0.8.0(Biks 在他的链接回复中使用的)略有不同的 API.我在我对问题的回复 我们如何从 Kafka 中创建主题IDE 使用 API,Biks 在上面提到.

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天全站免登陆