问题
I am new to Kafka and working with new KafkaProducer and KafkaConsumer, version : 0.9.0.1
Is there any way in java to alter/update the number of partitions for a specific topic after it has been created.
I am not using zookeeper to create topic. My KafkaProducer is automatically creating topics when publish request arrives.
I can also provide more details if these are not enough
回答1:
Yes, it's possible. You have to access the AdminUtils
scala class in kafka_2.11-0.9.0.1.jar
to add partitions.
AdminUtils
supports the number of partitions in the topic can only be increased. You may need kafka_2.11-0.9.0.1.jar
, zk-client-0.8.jar
, scala-library-2.11.8.jar
and scala-parser-combinators_2.11-1.0.4.jar
jars in your classpath.
Portions of the below code is borrowed / inspired from kafka-cloudera examples.
package org.apache.kafka.examples;
import java.io.Closeable;
import org.I0Itec.zkclient.ZkClient;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import kafka.admin.AdminOperationException;
import kafka.admin.AdminUtils;
import kafka.admin.RackAwareMode.Enforced$;
import kafka.utils.ZKStringSerializer$;
import kafka.utils.ZkUtils;
public class Test {
static final Logger logger = LogManager.getLogger();
public Test() {
// TODO Auto-generated constructor stub
}
public static void addPartitions(String zkServers, String topic, int partitions) {
try (AutoZkClient zkClient = new AutoZkClient(zkServers)) {
ZkUtils zkUtils = ZkUtils.apply(zkClient, false);
if (AdminUtils.topicExists(zkUtils, topic)) {
logger.info("Altering topic {}", topic);
try {
AdminUtils.addPartitions(zkUtils, topic, partitions, "", true, Enforced$.MODULE$);
logger.info("Topic {} altered with partitions : {}", topic, partitions);
} catch (AdminOperationException aoe) {
logger.info("Error while altering partitions for topic : {}", topic, aoe);
}
} else {
logger.info("Topic {} doesn't exists", topic);
}
}
}
// Just exists for Closeable convenience
private static final class AutoZkClient extends ZkClient implements Closeable {
static int sessionTimeout = 30_000;
static int connectionTimeout = 6_000;
AutoZkClient(String zkServers) {
super(zkServers, sessionTimeout, connectionTimeout, ZKStringSerializer$.MODULE$);
}
}
public static void main(String[] args) {
addPartitions("localhost:2181", "hello", 20);
}
}
来源:https://stackoverflow.com/questions/37437301/kafka-alter-number-of-partitions-for-a-specific-topic-using-java