Java Kafka使用TopicCommand创建主题
我想用java创建一个主题。这是我的密码Java Kafka使用TopicCommand创建主题,java,apache-kafka,Java,Apache Kafka,我想用java创建一个主题。这是我的密码 String s = "--topic pt8 --create --zookeeper 10.11.6.52:2181 --replica-assignment 7"; String[] args2 = s.split(" "); TopicCommand.main(args2); 但有一个错误: [ZkClient-EventThread-14-10.11.6.52:2181]INFO o.I.z.ZkEventThread-启动ZkClient事
String s = "--topic pt8 --create --zookeeper 10.11.6.52:2181 --replica-assignment 7";
String[] args2 = s.split(" ");
TopicCommand.main(args2);
但有一个错误:
[ZkClient-EventThread-14-10.11.6.52:2181]INFO o.I.z.ZkEventThread-启动ZkClient事件线程
[main]INFO o.I.z.ZkClient-正在等待状态同步连接
[main EventThread]信息o.I.z.ZkClient-zookeeper状态已更改
(同步连接)
执行主题命令时出错:
java.lang.ExceptionInInitializeError
[ZkClient-EventThread-14-10.11.6.52:2181]INFO o.I.z.ZkEventThread-
终止ZkClient事件线程
--列表--zookeeper 10.11.6.52:2181
可以得到结果。
--delete--zookeeper 10.11.6.52:2181--topic pt7
执行topic命令时出错:null
My 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>
错误:
线程“main”kafka.admin.AdminOperationException中的异常:java.lang.ExceptionInInitializeError
不要使用shell命令并试图从JAVA执行它,而是使用KAFKA管理客户端API,它应该与KAFKA 0.11+一起使用 以下是一段代码片段:
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;
}
void setUpKafkaTopics(KafkaAdminClient KafkaAdminClient)引发ExecutionException、InterruptedException{
最终映射主题=新HashMap();
topics.put(topicName,numOfPartitions);
kafkaAdminClient.createTopics(topics,getTopicConfig(),replicationFactor);
}
映射getTopicConfig(){
Map topicConfiguration=new HashMap();
topicConfiguration.put(TopicConfig.UNCLEAN_LEADER_ELECTION_ENABLE_CONFIG,
Boolean.FALSE.toString());
topicConfiguration.put(TopicConfig.CLEANUP\u POLICY\u CONFIG,
TopicConfig.CLEANUP\u POLICY\u DELETE);
topicConfiguration.put(TopicConfig.COMPRESSION\u TYPE\u CONFIG,
卡夫卡(主题压缩型);
topicConfiguration.put(TopicConfig.MIN)在同步副本配置中,
KAFKA_TOPIC_MIN_IN_SYNC_replications.toString());
topicConfiguration.put(TopicConfig.RETENTION\u MS\u CONFIG,
卡夫卡的主题是保留;
返回主题配置;
}
Zookeeper正在运行吗?我正在使用群集Zookeeper IP<代码>--列表--zookeeper 10.11.6.52:2181运行良好。我认为0.10.2.1
依赖项不可用。是的,很抱歉,我刚才看到你在使用0.10.2.1
,管理员客户端出现0.11.x
注意:这不是“我的”问题
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;
}