关于 scala:如何在 Java 中创建 Kafka ZKStringSerializer?

 2022-01-07 

How create Kafka ZKStringSerializer in Java?

在搜索如何通过API创建Kafka主题的过程中,我在Scala中找到了这个例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import kafka.admin.AdminUtils
import kafka.utils.ZKStringSerializer
import org.I0Itec.zkclient.ZkClient

// Create a ZooKeeper client
val sessionTimeoutMs = 10000
val connectionTimeoutMs = 10000
val zkClient = new ZkClient("zookeeper1:2181", sessionTimeoutMs,
                            connectionTimeoutMs, ZKStringSerializer)

// Create a topic with 8 partitions and a replication factor of 3
val topicName ="myTopic"
val numPartitions = 8
val replicationFactor = 3
val topicConfig = new Properties
AdminUtils.createTopic(zkClient, topicName,
                       numPartitions, replicationFactor, topicConfig)

来源:https://stackoverflow.com/a/23360100/871012

最后一个参数 ZKStringSerializer 显然是一个 Scala 对象。我不清楚如何使这个例子在 Java 中工作。

这篇文章 How to create a scala object in clojure 在 Clojure 中提出了同样的问题,答案是:

1
ZKStringSerializer$/MODULE$

在 Java 中(我认为)翻译成:

1
ZKStringSerializer$.MODULE$

但是当我尝试那个(或任何数量的其他变体)时,它们都没有编译。
编译错误为:

1
2
3
KafkaTopicCreator.java:[16,18] cannot find symbol
symbol:   variable ZKStringSerializer$
location: class org.sample.KafkaTopicCreator

我正在使用 kafka_2.9.2-0.8.1.1 和 Java 8.


对于java,请尝试以下操作,

首先导入下面的语句

1
import kafka.utils.ZKStringSerializer$;

通过以下方式为ZkClient创建对象,

1
2
3
String zkHosts ="127.0.0.1:2181"; //If more than one zookeeper then"127.0.0.1:2181,127.0.0.2:2181"
ZkClient zkClient = new ZkClient(zkHosts, 10000, 10000, ZKStringSerializer$.MODULE$);
AdminUtils.createTopic(zkClient, myTopic, 10, 1, new Properties());

The above code won't work for kafka > 0.9 since the api has been changed,
Use the below code for kafka > 0.9

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import java.util.Properties;
import kafka.admin.AdminUtils;
import kafka.utils.ZKStringSerializer$;
import kafka.utils.ZkUtils;
import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.ZkConnection;

public class KafkaTopicCreationInJava
{
    public static void main(String[] args) throws Exception {
        ZkClient zkClient = null;
        ZkUtils zkUtils = null;
        try {
            String zookeeperHosts ="192.168.20.1:2181"; // If multiple zookeeper then -> String zookeeperHosts ="192.168.20.1:2181,192.168.20.2:2181";
            int sessionTimeOutInMs = 15 * 1000; // 15 secs
            int connectionTimeOutInMs = 10 * 1000; // 10 secs

            zkClient = new ZkClient(zookeeperHosts, sessionTimeOutInMs, connectionTimeOutInMs, ZKStringSerializer$.MODULE$);
            zkUtils = new ZkUtils(zkClient, new ZkConnection(zookeeperHosts), false);

            String topicName ="testTopic";
            int noOfPartitions = 2;
            int noOfReplication = 3;
            Properties topicConfiguration = new Properties();

            AdminUtils.createTopic(zkUtils, topicName, noOfPartitions, noOfReplication, topicConfiguration);

        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (zkClient != null) {
                zkClient.close();
            }
        }
    }
}