Original: http://blog.csdn.net/changong28/article/details/39325079
With Kafka, we know that each time we create a Kafka theme (Topic), we can specify the number of partitions and the number of copies, and if these properties are configured in the Server.properties file, the themes generated by the subsequent call to the Java API will use the default values. Change first need to use command bin/kafka-topics.sh--zookeeper localhost:2181--alter--topic my-topic--config max.message.bytes= 128000 of the changes shown, we also want this process to be set through the API before the producer call, without having to use a script before or after this article. View source discovery, in fact, all of the implementation is through the Topiccommand Main method, in this record two ways:
1. Create a Theme (Topic)
"Command mode": bin/kafka-topics.sh--zookeeper zk_host:port/chroot--create--topic my_topic_name--partitions 20-- Replication-factor 3--config x=y
"JAVA API Mode":
- string[] options = new string[]{
- "--create",
- "--zookeeper",
- "Zk_host:port/chroot",
- "--partitions",
- "",
- "--topic",
- "My_topic_name",
- "--replication-factor",
- "3",
- "--config",
- "X=y"
- };
- Topiccommand.main (options);
2. View all Topics
"Command mode": bin/kafka-topics.sh--list--zookeeper localhost:2181
"JAVA API Mode":
- string[] options = new string[]{
- "--list",
- "--zookeeper",
- "localhost:2181"
- };
- Topiccommand.main (options);
3. View the specified topic:
"Command mode": bin/kafka-topics.sh--describe--zookeeper localhost:2181--topic my-replicated-topic
"JAVA API Mode":
- string[] options = new string[]{
- "--describe",
- "--zookeeper",
- "localhost:2181",
- "--topic",
- "My-replicated-topic",
- };
- Topiccommand.main (options);
4, modify the theme:
"Command mode": bin/kafka-topics.sh--zookeeper zk_host:port/chroot--alter--topic my_topic_name--deleteConfig x
"JAVA API Mode":
- string[] options = new string[]{
- "--alter",
- "--zookeeper",
- "Zk_host:port/chroot",
- "--topic",
- "My_topic_name",
- "--deleteconfig",
- "x"
- };
- Topiccommand.main (options);
5. Delete the quiz:
"Command mode": None
"JAVA API Mode":
- string[] options = new string[]{
- "--zookeeper",
- "Zk_host:port/chroot",
- "--topic",
- "My_topic_name"
- };
- Deletetopiccommand.main (options);
Another: The following Kafka Delete topic method (from the "Light Blog" blog, source http://caiguangguang.blog.51cto.com/1652935/1548069)
0.8 of official documents provide a command to delete topic:
Kafka-topics.sh--delete but will not find this method at run time.
Kafka-topics.sh finally ran the class Kafka.admin.TopicCommand, in 0.8 of the source code in this class did not find the delete topic related codes.
Under the Kafka Admin package, a Deletetopiccommand class is provided to remove the topic functionality.
Kafka.admin.DeleteTopicCommand
The specific implementation code for removing topic is as follows:
1234567891011121314151617181920 |
import
org.I0Itec.zkclient.ZkClient
import
kafka.utils.{Utils, ZKStringSerializer, ZkUtils}
.......
val topic = options.valueOf(topicOpt)
val zkConnect = options.valueOf(zkConnectOpt)
var zkClient: ZkClient =
null
try
{
zkClient =
new
ZkClient(zkConnect,
30000
,
30000
, ZKStringSerializer)
zkClient.deleteRecursive(ZkUtils.getTopicPath(topic))
//其实最终还是通过删除zk里面对应的路径来实现删除topic的功能
println(
"deletion succeeded!"
)
}
catch
{
case
e: Throwable =>
println(
"delection failed because of "
+ e.getMessage)
println(Utils.stackTrace(e))
}
finally
{
if
(zkClient !=
null
)
zkClient.close()
}
|
Because this command will only delete the information in ZK, the real data is still not deleted, so you need to login to each broker, the corresponding topic partition data directory deleted, it may also be because of this, The delete command is not integrated into the Kafka.admin.TopicCommand class.
Using Java API creation (create), view (describe), list (list), delete Kafka theme (Topic)--Reprint