统计Metric
package com.example.mail; import org.apache.storm.Config;
import org.apache.storm.LocalCluster;
import org.apache.storm.StormSubmitter;
import org.apache.storm.metric.LoggingMetricsConsumer;
import com.example.mail.TestWordSpout;
import org.apache.storm.topology.TopologyBuilder;
import org.apache.storm.utils.Utils; public class ExclamationTopology {
public static void main(String[] args) throws Exception {
TopologyBuilder builder = new TopologyBuilder();
builder.setSpout("word", new TestWordSpout());
builder.setBolt("exclaim1", new ExclamationBolt()).shuffleGrouping("word");
//builder.setBolt("exclaim2", new ExclamationBolt()).shuffleGrouping("exclaim1");
Config conf = new Config();
conf.setDebug(true);
//输出统计指标值到日志文件中
conf.registerMetricsConsumer(LoggingMetricsConsumer.class);
if (args != null && args.length > 0) {
conf.setNumWorkers(3);
StormSubmitter.submitTopology(args[0], conf, builder.createTopology());
}
else {
LocalCluster cluster = new LocalCluster();
cluster.submitTopology("test", conf, builder.createTopology());
Utils.sleep(100000);
cluster.killTopology("test");
cluster.shutdown();
}
}
}
package com.example.mail; import org.apache.storm.Config;
import org.apache.storm.topology.OutputFieldsDeclarer;
import java.util.Map;
import org.apache.storm.spout.SpoutOutputCollector;
import org.apache.storm.task.TopologyContext;
import org.apache.storm.topology.base.BaseRichSpout;
import org.apache.storm.tuple.Fields;
import org.apache.storm.tuple.Values;
import org.apache.storm.utils.Utils;
import java.util.HashMap;
import java.util.Random;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; public class TestWordSpout extends BaseRichSpout {
public static Logger LOG = LoggerFactory.getLogger(TestWordSpout.class);
boolean _isDistributed;
SpoutOutputCollector _collector; public TestWordSpout() {
this(true);
} public TestWordSpout(boolean isDistributed) {
_isDistributed = isDistributed;
} public void open(Map conf, TopologyContext context, SpoutOutputCollector collector) {
_collector = collector;
} public void close() { } public void nextTuple() {
Utils.sleep(1000);
final String[] words = new String[] {"a", "b", "c", "d", "e"};
final Random rand = new Random();
final String word = words[rand.nextInt(words.length)];
_collector.emit(new Values(word));
} public void ack(Object msgId) { } public void fail(Object msgId) { } public void declareOutputFields(OutputFieldsDeclarer declarer) {
declarer.declare(new Fields("word"));
} /* @Override
public Map<String, Object> getComponentConfiguration() {
if(!_isDistributed) {
Map<String, Object> ret = new HashMap<String, Object>();
ret.put(Config.TOPOLOGY_MAX_TASK_PARALLELISM, 1);
return ret;
} else {
return null;
}
} */
}
package com.example.mail; import java.util.Map; import org.apache.storm.metric.api.CountMetric;
import org.apache.storm.metric.api.MeanReducer;
import org.apache.storm.metric.api.MultiCountMetric;
import org.apache.storm.metric.api.ReducedMetric;
import org.apache.storm.task.OutputCollector;
import org.apache.storm.task.TopologyContext;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.topology.base.BaseRichBolt;
import org.apache.storm.tuple.Fields;
import org.apache.storm.tuple.Tuple;
import org.apache.storm.tuple.Values; public class ExclamationBolt extends BaseRichBolt {
OutputCollector _collector;
//定义指标统计对象
transient CountMetric _countMetric;
transient MultiCountMetric _wordCountMetric;
//transient ReducedMetric _wordLengthMeanMetric;
@Override
public void prepare(Map conf, TopologyContext context, OutputCollector collector) {
_collector = collector;
initMetrics(context);
}
@Override
public void execute(Tuple tuple) {
_collector.emit(tuple, new Values(tuple.getString(0) + "!!!"));
_collector.ack(tuple);
updateMetrics(tuple.getString(0));
} @Override
public void declareOutputFields(OutputFieldsDeclarer declarer) {
declarer.declare(new Fields("word"));
}
//初始化计数器
void initMetrics(TopologyContext context)
{
_countMetric = new CountMetric();
_wordCountMetric = new MultiCountMetric();
//_wordLengthMeanMetric = new ReducedMetric(new MeanReducer());
context.registerMetric("execute_count", _countMetric, 5);
context.registerMetric("word_count", _wordCountMetric, 10);
//context.registerMetric("word_length", _wordLengthMeanMetric, 60);
} //更新计数器
void updateMetrics(String word)
{
_countMetric.incr();
_wordCountMetric.scope(word).incr();
//_wordLengthMeanMetric.update(word.length());
}
}
统计Metric的更多相关文章
- 33.bucket与metric核心概念讲解
主要知识点: bucket与metric核心慨念 一.核心慨念 1.bucket:一个数据分组 比如有下面几条数据: city name 北京 小李 北京 小王 上海 小张 上海 小丽 上海 小陈 ...
- 【分享】thanos receiver的grafana报表配置
具体的部署是:用prometheus抓取thanos receiver的10909端口,然后grafana再请求这个prometheus 具体的JSON配置请见最后 本文的一些监控配置,参考了这篇文章 ...
- OstrichNet 简易统计信息收集工具
Ostrich 是twitter用于监控服务器性能的一个scala库,项目地址https://github.com/twitter/ostrich, 主要功能是收集.展示统计信息, 同时也提供了关闭服 ...
- Elasticsearch聚合初探——metric篇
Elasticsearch是一款提供检索以及相关度排序的开源框架,同时,也支持对存储的文档进行复杂的统计--聚合. 前言 ES中的聚合被分为两大类:Metric度量和bucket桶(原谅我英语差,找不 ...
- netstat--查看服务器[有效]连接数--统计端口并发数--access.log分析
简介 Netstat 命令用于显示各种网络相关信息,如网络连接,路由表,接口状态 (Interface Statistics),masquerade 连接,多播成员 (Multicast Member ...
- Linux网络统计工具/命令
我在Linux(基于CentOS 或者 Debian 的发行版)中该如何查看当前网络端口吞吐量的统计信息?在Linux操作系统中如何查看当前内核snmp计数器以及网络端口的统计信息? 你可以使用以下任 ...
- logstash 统计告警
在实际的项目中需要对线上日志做实时分析跟统计,这一套方案可以用现有的ELK(ElasticSearch, Logstash, Kibana)方案既可以满足,关于这个方案的具体的步骤可以参考网上的解决方 ...
- 统计网卡TX(发送)RX(接受)流量脚本
显示网卡流量的方法蛮多,一般我们可以通过dstat来查看,但dstat不一定所有的机器都有安装.而我们知道,通过ifconfig可以看到某一网卡发送与接收的字节数,所以我们可以写一个脚本来统计一下. ...
- 【转载】netstat--查看服务器[有效]连接数--统计端口并发数--access.log分析
简介 Netstat 命令用于显示各种网络相关信息,如网络连接,路由表,接口状态 (Interface Statistics),masquerade 连接,多播成员 (Multicast Member ...
随机推荐
- ZF 语法
Zend Framework Command Line Console Tool v1.11.11 Details for action "" and provider " ...
- UIScrollView 实现比例缩放
#import "RootViewController.h" @interface RootViewController ()<UIScrollViewDelegate> ...
- VMware内部错误解决办法
虚拟机内部错误,不要担心不是致命错误,往往是由于你的配置被禁用了或者VMware运行权限不够导致 检查你的VMware虚拟网卡是否被禁用 检查你的VMware的运行权限,直接管理员运行就够够的了
- Entity Framework Tutorial Basics(23):Add Single Entity
Add New Entity using DBContext in Disconnected Scenario: In this chapter you will learn how to add n ...
- sequoiadb的c++应用开发1
使用sequoiadb开发c++应用时需要使用BSON对象,本篇主要讲下BSON的操作方面的东西 1:构建一个_id的BSON对象 BSON的c++驱动给我提供了一个宏BSON,使用该对象可以很方便的 ...
- 使用Spring加载properties配置文件.md
背景 类似于datasource.properties之类的配置文件,最初通过Java的Properties类进行处理.这种方式有许多弊端,如每次都需要读取配置文件:若将Properties作为成员变 ...
- Linux Wifi模块灰色解决办法
https://www.cnblogs.com/mcy0808/p/5415689.html -sudo gedit /etc/rc.local 连上了CUG但还是打不开网页,是不是需要编辑Conne ...
- widget自定义控件【android.view.InflateException: Binary XML file line #2: Error inflating class...】
此错误比较难定位,场景是这样的:在一个widget中使用了自定义控件,始终会报 android.view.InflateException: Binary XML file line #2: Erro ...
- Bat 多个执行操作选择
Bat在日常编程中使用到会帮我们省去很多力气. @echo off Title DataBase Color 0A :caozuo echo. echo ═══════════════════════ ...
- UWP&WP8.1 基础控件——Border
border 是边框控件 border是UWP和WP8.1最常用的控件之一. border字面意义是用来添加边框的. 基础用法 <border BorderThickness="1&q ...