简体   繁体   English

Kafka通过使用TopicCommand创建主题

[英]Kafka create topic by using TopicCommand

I want to creat a topic by using java. 我想使用Java创建一个主题。 There are my codes. 有我的密码。

String s = "--topic pt8 --create --zookeeper 10.11.6.52:2181 --replica-assignment 7";
String[] args2 = s.split(" ");
TopicCommand.main(args2);

But there is an error: 但是有一个错误:

[ZkClient-EventThread-14-10.11.6.52:2181] INFO oIzZkEventThread - Starting ZkClient event thread. [ZkClient-EventThread-14-10.11.6.52:2181]信息oIzZkEventThread-启动ZkClient事件线程。

[main] INFO oIzZkClient - Waiting for keeper state SyncConnected [main-EventThread] INFO oIzZkClient - zookeeper state changed (SyncConnected) [main] INFO oIzZkClient-等待守护者状态SyncConnected [main-EventThread] INFO oIzZkClient-动物园管理员状态已更改(SyncConnected)

Error while executing topic command : java.lang.ExceptionInInitializerError 执行主题命令时出错:java.lang.ExceptionInInitializerError

[ZkClient-EventThread-14-10.11.6.52:2181] INFO oIzZkEventThread - Terminate ZkClient event thread. [ZkClient-EventThread-14-10.11.6.52:2181]信息oIzZkEventThread-终止ZkClient事件线程。

--list --zookeeper 10.11.6.52:2181 can get results. --list --zookeeper 10.11.6.52:2181可以获得结果。 --delete --zookeeper 10.11.6.52:2181 --topic pt7 gets Error while executing topic command : null . --delete --zookeeper 10.11.6.52:2181 --topic pt7 Error while executing topic command : nullError while executing topic command : null

My pom.xml: 我的pom.xml:

        <dependency>
        <groupId>org.apache.kafka</groupId>
        <artifactId>kafka_2.11</artifactId>
        <version>0.10.2.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.kafka</groupId>
        <artifactId>kafka-clients</artifactId>
        <version>0.10.2.1</version>
    </dependency>

Using Admin: 使用管理员:

ZkClient  zkClient = new ZkClient("10.11.6.52:2181", 30000, 30000, ZKStringSerializer$.MODULE$);
ZkUtils zkUtils = ZkUtils.apply(zkClient, false);
AdminUtils.createTopic(zkUtils, "pt8", 1, 1, new Properties(), RackAwareMode.Disabled$.MODULE$);

ERROR: 错误:

Exception in thread "main" kafka.admin.AdminOperationException: java.lang.ExceptionInInitializerError 线程“主”中的异常kafka.admin.AdminOperationException:java.lang.ExceptionInInitializerError

Instead of using a shell command and trying to execute it from JAVA, use the KAFKA Admin client API, which should work with Kafka 0.11+. 可以使用应该与Kafka 0.11+一起使用的KAFKA Admin客户端API,而不是使用shell命令并尝试从JAVA中执行它。

Here is a code snippet: 这是一个代码片段:

void setUpKafkaTopics(KafkaAdminClient kafkaAdminClient) throws ExecutionException, InterruptedException {
  final Map<String, Integer> topics = new HashMap<>();
  topics.put(topicName, numOfPartitions);
  kafkaAdminClient.createTopics(topics, getTopicConfig(), replicationFactor);
}


Map<String, String> getTopicConfig() {
  Map<String, String> topicConfiguration = new HashMap<>();
  topicConfiguration.put(TopicConfig.UNCLEAN_LEADER_ELECTION_ENABLE_CONFIG,
     Boolean.FALSE.toString());
  topicConfiguration.put(TopicConfig.CLEANUP_POLICY_CONFIG,
     TopicConfig.CLEANUP_POLICY_DELETE);
  topicConfiguration.put(TopicConfig.COMPRESSION_TYPE_CONFIG,
     KAFKA_TOPIC_COMPRESSION_TYPE);
  topicConfiguration.put(TopicConfig.MIN_IN_SYNC_REPLICAS_CONFIG,
     KAFKA_TOPIC_MIN_IN_SYNC_REPLICAS.toString()); 
  topicConfiguration.put(TopicConfig.RETENTION_MS_CONFIG,
     KAFKA_TOPIC_RETENTION_MS.toString()); 
  return topicConfiguration;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM