kafka监控获取指定topic的消息总量示例


Posted in Python onDecember 23, 2019

我就废话不多说了,直接 上代码吧!

import kafka.api.PartitionOffsetRequestInfo;
import kafka.common.TopicAndPartition;
import kafka.javaapi.OffsetResponse;
import kafka.javaapi.PartitionMetadata;
import kafka.javaapi.TopicMetadata;
import kafka.javaapi.TopicMetadataRequest;
import kafka.javaapi.consumer.SimpleConsumer;
 
import java.util.*;
import java.util.Map.Entry;
 
public class KafkaOffsetTools {
public final static String KAFKA_TOPIC_NAME_ADAPTER = "sample";
public final static String KAFKA_TOPIC_NAME_EXCEPTION = "exception";
public final static String KAFKA_TOPIC_NAME_AUDIT = "audit";
private static final String rawTopicTotal = "rawTopicTotalRecordCounter";
private static final String avroTopicTotal = "avroTopicTotalRecordCounter";
private static final String exceptionTopicTotal = "exceptionTopicTotalRecordCounter";
 
public KafkaOffsetTools() {
}
 
public static long getLastOffset(SimpleConsumer consumer, String topic,
int partition, long whichTime, String clientName) {
TopicAndPartition topicAndPartition = new TopicAndPartition(topic,
partition);
Map, PartitionOffsetRequestInfo> requestInfo = new HashMap, PartitionOffsetRequestInfo>();
requestInfo.put(topicAndPartition, new PartitionOffsetRequestInfo(
whichTime, 1));
kafka.javaapi.OffsetRequest request = new kafka.javaapi.OffsetRequest(
requestInfo, kafka.api.OffsetRequest.CurrentVersion(),
clientName);
OffsetResponse response = consumer.getOffsetsBefore(request);
 
if (response.hasError()) {
System.err.println("Error fetching data Offset Data the Broker. Reason: " + response.errorCode(topic, partition));
return 0;
}
long[] offsets = response.offsets(topic, partition);
return offsets[0];
}
 
private TreeMap, PartitionMetadata> findLeader(List a_seedBrokers, String a_topic) {
TreeMap, PartitionMetadata> map = new TreeMap, PartitionMetadata>();
loop:
for (String seed : a_seedBrokers) {
SimpleConsumer consumer = null;
try {
String[] hostAndPort;
hostAndPort = seed.split(":");
consumer = new SimpleConsumer(hostAndPort[0], Integer.valueOf(hostAndPort[1]), 100000, 64 * 1024,
"leaderLookup" + new Date().getTime());
List topics = Collections.singletonList(a_topic);
TopicMetadataRequest req = new TopicMetadataRequest(topics);
kafka.javaapi.TopicMetadataResponse resp = consumer.send(req);
 
List metaData = resp.topicsMetadata();
for (TopicMetadata item : metaData) {
for (PartitionMetadata part : item.partitionsMetadata()) {
map.put(part.partitionId(), part);
}
}
} catch (Exception e) {
System.out.println("Error communicating with Broker [" + seed
+ "] to find Leader for [" + a_topic + ", ] Reason: " + e);
} finally {
if (consumer != null)
consumer.close();
}
}
return map;
}
 
public static void main(String[] args) {
String kafkaBrokerList = System.getenv("metadata.broker.list");
if(kafkaBrokerList == null || kafkaBrokerList.length() == 0){
System.err.println("No config kafka metadata.broker.list,it is null .");
//for test
kafkaBrokerList = "localhost:9092,localhost:9093";
System.err.println("Use this broker list for test,metadata.broker.list="+kafkaBrokerList);
}
//init topic,logSize = 0
Map,Integer> topics = new HashMap,Integer>();
topics.put(KAFKA_TOPIC_NAME_ADAPTER,0);
topics.put(KAFKA_TOPIC_NAME_EXCEPTION,0);
topics.put(KAFKA_TOPIC_NAME_AUDIT,0);
//init kafka broker list
String[] kafkaHosts;
kafkaHosts = kafkaBrokerList.split(",");
if (kafkaHosts == null || kafkaHosts.length == 0) {
System.err.println("No config kafka metadata.broker.list,it is null .");
System.exit(1);
}
List seeds = new ArrayList();
for (int i = 0; i < kafkaHosts.length; i++) {
seeds.add(kafkaHosts[i]);
}
 
KafkaOffsetTools kot = new KafkaOffsetTools();
 
for(String topicName : topics.keySet()){
TreeMap, PartitionMetadata> metadatas = kot.findLeader(seeds, topicName);
int logSize = 0;
for (Entry, PartitionMetadata> entry : metadatas.entrySet()) {
int partition = entry.getKey();
String leadBroker = entry.getValue().leader().host();
String clientName = "Client_" + topicName + "_" + partition;
SimpleConsumer consumer = new SimpleConsumer(leadBroker, entry.getValue().leader().port(), 100000,
64 * 1024, clientName);
long readOffset = getLastOffset(consumer, topicName, partition,
kafka.api.OffsetRequest.LatestTime(), clientName);
logSize += readOffset;
if (consumer != null) consumer.close();
}
topics.put(topicName,logSize);
}
System.out.println(topics.toString());
System.out.println(rawTopicTotal+"="+topics.get(KAFKA_TOPIC_NAME_ADAPTER)+" "+System.currentTimeMillis());
System.out.println(avroTopicTotal+"="+topics.get(KAFKA_TOPIC_NAME_AUDIT)+" "+System.currentTimeMillis());
System.out.println(exceptionTopicTotal+"="+topics.get(KAFKA_TOPIC_NAME_EXCEPTION)+" "+System.currentTimeMillis());
}
}

以上这篇kafka监控获取指定topic的消息总量示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python 获取本机ip地址的两个方法
Feb 25 Python
python3读取MySQL-Front的MYSQL密码
May 03 Python
Python守护线程用法实例
Jun 23 Python
在python中使用正则表达式查找可嵌套字符串组
Oct 24 Python
Python-OpenCV基本操作方法详解
Apr 02 Python
python 2.7.14安装图文教程
Apr 08 Python
对numpy的array和python中自带的list之间相互转化详解
Apr 13 Python
Python3实现的字典、列表和json对象互转功能示例
May 22 Python
Python元组知识点总结
Feb 18 Python
基于python SMTP实现自动发送邮件教程解析
Jun 02 Python
Python如何执行系统命令
Sep 23 Python
使用Python制作一盏 3D 花灯喜迎元宵佳节
Feb 26 Python
kafka-python 获取topic lag值方式
Dec 23 #Python
python3连接kafka模块pykafka生产者简单封装代码
Dec 23 #Python
python pyenv多版本管理工具的使用
Dec 23 #Python
Python测试Kafka集群(pykafka)实例
Dec 23 #Python
Python with语句和过程抽取思想
Dec 23 #Python
python每5分钟从kafka中提取数据的例子
Dec 23 #Python
Python彻底删除文件夹及其子文件方式
Dec 23 #Python
You might like
兼容PHP5的PHP目录管理函数库
2008/07/10 PHP
PHP面向对象程序设计之类常量用法实例
2014/08/20 PHP
利用PHP抓取百度阅读的方法示例
2016/12/18 PHP
php压缩文件夹最新版
2018/07/18 PHP
Laravel+Intervention实现上传图片功能示例
2019/07/09 PHP
php array 转json及java 转换 json数据格式操作示例
2019/11/13 PHP
php文件上传原理与实现方法详解
2019/12/20 PHP
JavaScript 面向对象编程(2) 定义类
2010/05/18 Javascript
js实现全屏漂浮广告移入光标停止移动
2013/12/02 Javascript
js通过更改按钮的显示样式实现按钮的滑动效果
2014/04/23 Javascript
深入分析js的冒泡事件
2014/12/05 Javascript
JavaScript让Textarea支持tab按键的方法
2015/06/26 Javascript
js中对函数设置默认参数值的3种方法
2015/10/23 Javascript
JavaScript实现的MD5算法完整实例
2016/02/02 Javascript
基于Three.js插件制作360度全景图
2016/11/29 Javascript
浅谈js script标签中的预解析
2016/12/30 Javascript
angular4模块中给标签添加背景图的实现方法
2017/09/15 Javascript
Node.js进阶之核心模块https入门
2018/05/23 Javascript
Vue父子组件之间的通信实例详解
2018/09/28 Javascript
JS实现电商商品展示放大镜特效
2020/01/07 Javascript
解决Python selenium get页面很慢时的问题
2019/01/30 Python
python+webdriver自动化环境搭建步骤详解
2019/06/03 Python
python logging模块书写日志以及日志分割详解
2019/07/22 Python
Pytorch之view及view_as使用详解
2019/12/31 Python
关于TensorFlow新旧版本函数接口变化详解
2020/02/10 Python
Python @property原理解析和用法实例
2020/02/11 Python
tensorflow实现从.ckpt文件中读取任意变量
2020/05/26 Python
HTML5实现经典坦克大战坦克乱走还能发出一个子弹
2013/09/02 HTML / CSS
美津浓巴西官方网站:Mizuno巴西
2019/07/24 全球购物
啤酒节策划方案
2014/05/28 职场文书
学校标语大全
2014/06/19 职场文书
开展创先争优活动总结
2014/08/28 职场文书
骨干教师个人总结
2015/02/11 职场文书
财政局个人年终总结
2015/03/03 职场文书
团支部组织委员竞选稿
2015/11/21 职场文书
安装Windows Server 2012 R2企业版操作系统并设置好相关参数
2022/04/29 Servers