• kafka系列六、java管理kafka Topic


    package com.example.demo.topic;
    
    import kafka.admin.AdminUtils;
    import kafka.admin.RackAwareMode;
    import kafka.server.ConfigType;
    import kafka.utils.ZkUtils;
    import org.apache.kafka.common.requests.MetadataResponse;
    import org.apache.kafka.common.security.JaasUtils;
    import scala.collection.JavaConversions;
    
    import java.util.List;
    import java.util.Map;
    import java.util.Properties;
    
    public class KafkaTopic {
        public static void main(String[] args) {
            //createTopic();
            //deleteTopic();
            //listAllTopic();
            // getTopic();
            listTopicAllConfig();
        }
    
        /**
        * 创建主题
        * kafka-topics.sh --zookeeper localhost:2181 --create --topic kafka-action --replication-factor 2 --partitions 3
        */
        private static void createTopic() {
            ZkUtils zkUtils = ZkUtils.apply("47.52.199.51:2181", 30000, 30000, JaasUtils.isZkSecurityEnabled());
            // 创建一个单分区单副本名为t1的topic
            AdminUtils.createTopic(zkUtils, "topic-20", 3, 1, new Properties(), RackAwareMode.Enforced$.MODULE$);
            zkUtils.close();
        }
    
        /**
         * 除某主题
         * kafka-topics.sh --zookeeper localhost:2181 --topic kafka-action --delete
         */
        private static void deleteTopic() {
            ZkUtils zkUtils = ZkUtils.apply("47.52.199.51:2181", 30000, 30000, JaasUtils.isZkSecurityEnabled());
            // 删除topic 't1'
            AdminUtils.deleteTopic(zkUtils, "topic-19");
            zkUtils.close();
        }
    
        /**
         * 修改主题配置     kafka-config --zookeeper localhost:2181 --entity-type topics --entity-name kafka-action     
    * --alter --add-config max.message.bytes=202480 --alter --delete-config flush.messages
    */ private static void updateTopic() { ZkUtils zkUtils = ZkUtils.apply("47.52.199.51:2181", 30000, 30000, JaasUtils.isZkSecurityEnabled()); Properties props = AdminUtils.fetchEntityConfig(zkUtils, ConfigType.Topic(), "topic-19"); // 增加topic级别属性 props.put("min.cleanable.dirty.ratio", "0.3"); // 删除topic级别属性 props.remove("max.message.bytes"); // 修改topic 'test'的属性 AdminUtils.changeTopicConfig(zkUtils, "test", props); zkUtils.close(); } /** * * 查看所有主题 kafka-topics.sh --zookeeper localhost:2181 --list */ public static void listAllTopic() { ZkUtils zkUtils = null; try { zkUtils = ZkUtils.apply("47.52.199.51:2181", 30000, 30000, JaasUtils.isZkSecurityEnabled()); List<String> topics = JavaConversions.seqAsJavaList(zkUtils.getAllTopics()); topics.forEach(System.out::println); } catch (Exception e) { e.printStackTrace(); } finally { if (zkUtils != null) { zkUtils.close(); } } } /** * 得到所有topic的配置信息 kafka-configs.sh --zookeeper localhost:2181 --entity-type topics --describe */ public static void listTopicAllConfig() { ZkUtils zkUtils = null; try { zkUtils = ZkUtils.apply("47.52.199.51:2181", 30000, 30000, JaasUtils.isZkSecurityEnabled()); Map<String, Properties> configs = JavaConversions.mapAsJavaMap(AdminUtils.fetchAllTopicConfigs(zkUtils)); // 获取特定topic的元数据 MetadataResponse.TopicMetadata topicMetadata = AdminUtils.fetchTopicMetadataFromZk("topic-19",zkUtils); // 获取特定topic的配置信息 Properties properties = AdminUtils.fetchEntityConfig(zkUtils,"topics","kafka-test"); for (Map.Entry<String, Properties> entry : configs.entrySet()) { System.out.println("key=" + entry.getKey() + " ;value= " + entry.getValue()); } } catch (Exception e) { e.printStackTrace(); } finally { if (zkUtils != null) { zkUtils.close(); } } } }
  • 相关阅读:
    redis 安装和运行
    Django:django-debug-toolbar模块
    Django 的 logging日志文件配置
    Github之利用SSH完成Git与GitHu 的绑定
    4.输出1-100内的所有偶数
    3.输出1-100内的所有奇数
    2.求1-100的所有整数的和
    1.使用while循环输出1.2.3.4.5.6.....8.9.10
    将前台JS数组序列化后提交,后台反序列化对象
    div模拟下拉框
  • 原文地址:https://www.cnblogs.com/wangzhuxing/p/10105961.html
Copyright © 2020-2023  润新知