|NO.Z.00046|——————————|BigDataEnd|——|Hadoop&kafka.V31|——|kafka.v31|kafkaAdminclient应用.v02|

一、KafkaAdminClient应用实现流程
### --- 主要操作步骤:

~~~     客户端根据方法的调用创建相应的协议请求,
~~~     比如创建Topic的createTopics方法,其内部就是发送CreateTopicRequest请求。
~~~     客户端发送请求至Kafka Broker。
~~~     Kafka Broker处理相应的请求并回执,比如与CreateTopicRequest对应的是CreateTopicResponse。
~~~     客户端接收相应的回执并进行解析处理。
~~~     和协议有关的请求和回执的类基本都在org.apache.kafka.common.requests包中,
~~~     AbstractRequest和AbstractResponse是这些请求和响应类的两个父类。
### --- 综上,如果要自定义实现一个功能,只需要三个步骤:

~~~     自定义XXXOptions;
~~~     自定义XXXResult返回值;
~~~     自定义Call,然后挑选合适的XXXRequest和XXXResponse来实现Call类中的3个抽象方法。
二、创建一个maven项目
### --- 创建一个maven项目:demo-03-kafka-adminclient
### --- 添加pom.xml依赖

    <dependencies>
        <dependency>
            <groupId>org.apache.kafka</groupId>
            <artifactId>kafka-clients</artifactId>
            <version>1.0.2</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>
三、KafkaAdminClient编程实现
### --- 编程代码实现

package com.yanqi.kafka.demo;

import org.apache.kafka.clients.admin.*;
import org.apache.kafka.common.TopicPartition;
import org.apache.kafka.common.requests.DescribeLogDirsResponse;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

public class MyAdminClient {

    private KafkaAdminClient client;

    @Before
    public void before() {

        Map<String, Object> configs = new HashMap<>();
        configs.put("bootstrap.servers", "node1:9092");
        configs.put("client.id", "admin_001");

        client = (KafkaAdminClient) KafkaAdminClient.create(configs);
    }

    @After
    public void after() {
        // 关闭admin客户端
        client.close();
    }

    @Test
    public void testListTopics() throws ExecutionException, InterruptedException {
        // 列出主题
//        final ListTopicsResult listTopicsResult = client.listTopics();

        ListTopicsOptions options = new ListTopicsOptions();
        // 列出内部主题
        options.listInternal(true);
        // 设置请求超时时间,单位是毫秒
        options.timeoutMs(500);

        final ListTopicsResult listTopicsResult = client.listTopics(options);

//        final Set<String> strings = listTopicsResult.names().get();
//
//        strings.forEach(name -> {
//            System.out.println(name);
//        });

        // 将请求变成同步的请求,直接获取结果
        final Collection<TopicListing> topicListings = listTopicsResult.listings().get();

        topicListings.forEach(new Consumer<TopicListing>() {
            @Override
            public void accept(TopicListing topicListing) {

                // 该主题是否是内部主题
                final boolean internal = topicListing.isInternal();
                // 该主题的名字
                final String name = topicListing.name();


                System.out.println("主题是否是内部主题:" + internal);
                System.out.println("主题的名字:" + name);
                System.out.println(topicListing);
                System.out.println("=====================================");
            }
        });

    }

    @Test
    public void testDescribeLogDirs() throws ExecutionException, InterruptedException {
        final DescribeLogDirsResult describeLogDirsResult = client.describeLogDirs(Collections.singleton(0));

        final Map<Integer, Map<String, DescribeLogDirsResponse.LogDirInfo>> integerMapMap
                = describeLogDirsResult.all().get();

        integerMapMap.forEach(new BiConsumer<Integer, Map<String, DescribeLogDirsResponse.LogDirInfo>>() {
            @Override
            public void accept(Integer integer, Map<String, DescribeLogDirsResponse.LogDirInfo> stringLogDirInfoMap) {
                System.out.println("broker.id = " + integer);
//                log.dirs可以设置多个目录
                stringLogDirInfoMap.forEach(new BiConsumer<String, DescribeLogDirsResponse.LogDirInfo>() {
                    @Override
                    public void accept(String s, DescribeLogDirsResponse.LogDirInfo logDirInfo) {
                        System.out.println("logdir = " + s);
                        final Map<TopicPartition, DescribeLogDirsResponse.ReplicaInfo> replicaInfos = logDirInfo.replicaInfos;

                        replicaInfos.forEach(new BiConsumer<TopicPartition, DescribeLogDirsResponse.ReplicaInfo>() {
                            @Override
                            public void accept(TopicPartition topicPartition, DescribeLogDirsResponse.ReplicaInfo replicaInfo) {
                                System.out.println("主题分区:" + topicPartition.partition());
                                System.out.println("主题:" + topicPartition.topic());
//                                final boolean isFuture = replicaInfo.isFuture;
//                                final long offsetLag = replicaInfo.offsetLag;
//                                final long size = replicaInfo.size;
                            }
                        });

                    }
                });
            }
        });


    }
}
四、编译打印
### --- kafka下主题查看

[root@hadoop ~]# kafka-topics.sh --zookeeper localhost:2181/myKafka --list
__consumer_offsets
topic_x
topic_y
topic_z
### --- 编译打印

D:\JAVA\jdk1.8.0_231\bin\java.exe -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\lib\idea_rt.jar=58797:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\bin" -Dfile.encoding=UTF-8 -classpath "D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\lib\idea_rt.jar;D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\plugins\junit\lib\junit5-rt.jar;D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\plugins\junit\lib\junit-rt.jar;D:\JAVA\jdk1.8.0_231\jre\lib\charsets.jar;D:\JAVA\jdk1.8.0_231\jre\lib\deploy.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\access-bridge-64.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\cldrdata.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\dnsns.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\jaccess.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\jfxrt.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\localedata.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\nashorn.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\sunec.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\sunjce_provider.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\sunmscapi.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\sunpkcs11.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\zipfs.jar;D:\JAVA\jdk1.8.0_231\jre\lib\javaws.jar;D:\JAVA\jdk1.8.0_231\jre\lib\jce.jar;D:\JAVA\jdk1.8.0_231\jre\lib\jfr.jar;D:\JAVA\jdk1.8.0_231\jre\lib\jfxswt.jar;D:\JAVA\jdk1.8.0_231\jre\lib\jsse.jar;D:\JAVA\jdk1.8.0_231\jre\lib\management-agent.jar;D:\JAVA\jdk1.8.0_231\jre\lib\plugin.jar;D:\JAVA\jdk1.8.0_231\jre\lib\resources.jar;D:\JAVA\jdk1.8.0_231\jre\lib\rt.jar;E:\NO.Z.10000——javaproject\NO.Z.00002.Hadoop\kafka_demo\demo-03-kafka-adminclient\target\classes;C:\Users\Administrator\.m2\repository\org\apache\kafka\kafka-clients\1.0.2\kafka-clients-1.0.2.jar;C:\Users\Administrator\.m2\repository\org\lz4\lz4-java\1.4\lz4-java-1.4.jar;C:\Users\Administrator\.m2\repository\org\xerial\snappy\snappy-java\1.1.4\snappy-java-1.1.4.jar;C:\Users\Administrator\.m2\repository\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar;C:\Users\Administrator\.m2\repository\junit\junit\4.12\junit-4.12.jar;C:\Users\Administrator\.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar" com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 com.yanqi.kafka.demo.MyAdminClient
主题是否是内部主题:false
主题的名字:topic_x
(name=topic_x, internal=false)
=====================================
主题是否是内部主题:false
主题的名字:topic_y
(name=topic_y, internal=false)
=====================================
主题是否是内部主题:false
主题的名字:topic_z
(name=topic_z, internal=false)
=====================================
主题是否是内部主题:true
主题的名字:__consumer_offsets
(name=__consumer_offsets, internal=true)
=====================================
broker.id = 0
logdir = /opt/yanqi/servers/kafka/kafka-logs
主题分区:13
主题:__consumer_offsets
主题分区:46
主题:__consumer_offsets
主题分区:9
主题:__consumer_offsets
主题分区:42
主题:__consumer_offsets
主题分区:21
主题:__consumer_offsets
主题分区:17
主题:__consumer_offsets
主题分区:3
主题:topic_z
主题分区:30
主题:__consumer_offsets
主题分区:26
主题:__consumer_offsets
主题分区:5
主题:__consumer_offsets
主题分区:38
主题:__consumer_offsets
主题分区:1
主题:__consumer_offsets
主题分区:34
主题:__consumer_offsets
主题分区:16
主题:__consumer_offsets
主题分区:45
主题:__consumer_offsets
主题分区:12
主题:__consumer_offsets
主题分区:41
主题:__consumer_offsets
主题分区:24
主题:__consumer_offsets
主题分区:20
主题:__consumer_offsets
主题分区:49
主题:__consumer_offsets
主题分区:0
主题:topic_x
主题分区:0
主题:__consumer_offsets
主题分区:0
主题:topic_y
主题分区:29
主题:__consumer_offsets
主题分区:0
主题:topic_z
主题分区:25
主题:__consumer_offsets
主题分区:8
主题:__consumer_offsets
主题分区:37
主题:__consumer_offsets
主题分区:4
主题:__consumer_offsets
主题分区:33
主题:__consumer_offsets
主题分区:4
主题:topic_z
主题分区:15
主题:__consumer_offsets
主题分区:48
主题:__consumer_offsets
主题分区:11
主题:__consumer_offsets
主题分区:44
主题:__consumer_offsets
主题分区:23
主题:__consumer_offsets
主题分区:19
主题:__consumer_offsets
主题分区:1
主题:topic_x
主题分区:1
主题:topic_y
主题分区:32
主题:__consumer_offsets
主题分区:1
主题:topic_z
主题分区:28
主题:__consumer_offsets
主题分区:7
主题:__consumer_offsets
主题分区:40
主题:__consumer_offsets
主题分区:3
主题:__consumer_offsets
主题分区:36
主题:__consumer_offsets
主题分区:47
主题:__consumer_offsets
主题分区:14
主题:__consumer_offsets
主题分区:43
主题:__consumer_offsets
主题分区:10
主题:__consumer_offsets
主题分区:22
主题:__consumer_offsets
主题分区:18
主题:__consumer_offsets
主题分区:31
主题:__consumer_offsets
主题分区:2
主题:topic_z
主题分区:27
主题:__consumer_offsets
主题分区:39
主题:__consumer_offsets
主题分区:6
主题:__consumer_offsets
主题分区:35
主题:__consumer_offsets
主题分区:2
主题:topic_x
主题分区:2
主题:__consumer_offsets

 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

posted on   yanqi_vip  阅读(31)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示