2014-09-24 138 views

回答

1

似乎有那麼你必須使用主題自動創建的或命令行工具沒有卡夫卡服務器API來創建一個話題:

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

看起來你可以可以使用下面的方法確保你的話題已經存在(我假設你正在使用以下kafka python實現):

client = KafkaClient(...) 
producer = KafkaProducer(...) 
client.ensure_topic_exists('my_new_topic') 
producer.send_messages('my_new_topic', ...) 
+3

這是行不通的。 'ensure_topic_exists'只適用於啓用自動主題創建。 https://github.com/mumrah/kafka-python/blob/cd81cf0ec8c1b7e7651374c5d1cbd105d003d352/kafka/client.py#L305-L306 – zackdever 2015-05-15 00:48:01

0

這已經太晚了。我不知道有關顯式創建主題的命令,但以下內容會創建並添加這些消息。

我創建了一個python卡夫卡製作:

prod = KafkaProducer(bootstrap_servers='localhost:9092') 
for i in xrange(1000): 
    prod.send('xyz', str(i)) 

在卡夫卡主題xyz是不存在先前的列表。當我做了上面的方法時,Python-kafka客戶端創建了它並添加了消息。

+1

實際上,代理人創建了主題,只是因爲auto.topic.create.enable被設置爲「true」 。以這種方式創建的所有主題將具有默認配置,可能會或可能不會對您的用例有好處。 – 2017-06-21 06:11:05

0

做編程主題創建和配置所需的的AdminClient API卡夫卡0.11剛剛添加(最初爲Java)

https://cwiki.apache.org/confluence/display/KAFKA/KIP-117%3A+Add+a+public+AdminClient+API+for+Kafka+admin+operations

預計非Java客戶端庫將添加此功能隨着時間的推移。請與您正在使用的卡夫卡Python客戶端的作者(有幾個),看看是否以及何時KIP-4管理協議支持將是在API中

https://cwiki.apache.org/confluence/display/KAFKA/KIP-4+-+Command+line+and+centralized+administrative+operations

0
from kafka import KafkaProducer 

producer = KafkaProducer(bootstrap_servers=['localhost:9092']) 
topic = 'topic-name' 

producer.send(topic, final_list[0]).get(timeout=10)