hadoop Metrics Example

来源:互联网 发布:excel数据透视 编辑:程序博客网 时间:2024/06/07 22:53

TestMetrics

import org.apache.hadoop.metrics2.annotation.Metric;import org.apache.hadoop.metrics2.annotation.Metrics;import org.apache.hadoop.metrics2.lib.MetricsRegistry;import org.apache.hadoop.metrics2.lib.MutableCounterLong;import org.apache.hadoop.metrics2.lib.MutableRate;@Metrics(name="metricsTest",about = "testMetricsAbout", context = "testMetricsContext")public class TestMetrics {    public TestMetrics() {    }    final MetricsRegistry registry = new MetricsRegistry("testmetrics");    @Metric    MutableCounterLong eventOcccurredTime;    @Metric    MutableRate rate;}
import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hdfs.HdfsConfiguration;import org.apache.hadoop.metrics2.MetricsSystem;import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem;import java.util.concurrent.TimeUnit;public class DefaultMetricsTest {    public static void main(String[] args) {        Configuration conf = new HdfsConfiguration();        DefaultMetricsSystem.initialize("testmetrics");        MetricsSystem ms = DefaultMetricsSystem.instance();        TestMetrics testMetrics = new TestMetrics();        ms.register("testmetrics",null, testMetrics);        long sleepMS = 100;        Thread t = new Thread(){            long i = 1;            public void run(){                while(true) {                    testMetrics.eventOcccurredTime.incr(1);                    testMetrics.rate.add(1, i++);                    try {                        TimeUnit.MILLISECONDS.sleep(sleepMS);                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                }            }        };        t.setDaemon(true);        t.start();        try {            TimeUnit.SECONDS.sleep(100);        } catch (InterruptedException e) {            e.printStackTrace();        }    }}

hadoop-metrics2.properties add the following contents.

testmetrics.sink.file.class=org.apache.hadoop.metrics2.sink.FileSinktestmetrics.sink.file.filename=testmetrics-metrics.out
原创粉丝点击