storm从0.9.0开始,增加了指标统计框架,用来收集应用程序的特定指标,并将其输出到外部系统。
本文中采用的监听类是LoggingMetricsConsumer,统计指标值将输出到metric.log日志文件中。
当然也可以自定义监听类,只需要实现
IMetricsConsumer接口即可,这些类可以在代码里注册(registerMetricsConsumer),也可以在 storm.yaml配置文件中注册:
topology.metrics.consumer.register:
- class: "backtype.storm.metrics.LoggingMetricsConsumer"
parallelism.hint: 1
- class: "org.mycompany.MyMetricsConsumer"
parallelism.hint: 1
argument:
- endpoint: "metrics-collector.mycompany.org"
|
下面简单修改storm-starter包中的例子,统计三个指标:
1、每5秒统计execute方法的调用次数
2、每1分钟统计每个单词的出现次数
3、每1分钟统计所有出现的单词的平均长度
public class ExclamationTopology {
public static 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);
|