MapReduce 计数器

1. MapReduce 计数器

1.1 计数器是什么

计数器一般用来记录 job 执行进度和状态信息。

如果自己手动实现计数,需要考虑将多个线程的计算结果合并,编码过于麻烦,通常使用 MapReduce 的计数器。

实际应用中可以用来统计任务的某个环节的执行次数或者数据量,并作为优化前后的参数依据。

1.2 计数器分类

🌟 内置计数器:
内置计数器用来描述该作业的各项指标。

🌟 自定义计数器:

用户可以通过自己定义计数器,来实现特定的需求,可以通过枚举的方式定义计数器,也可以通过 context.getCounter 的方式自定义计数器。

2. MapReduce 内置计数器

内置计数器中分为若干个组,组内包含若干个统计项。

组别对应类描述
MapReduce 任务计数器mapreduce.TaskCounter统计任务的具体信息
文件系统计数器mapreduce.FileSystemCounter统计任务的读取或写入
FileInputFormat 计数器mapreduce.lib.input.FileInputFormatCounter统计读取的字节数
FileOutputFormat 计数器mapreduce.lib.input.FileOutputFormatCounter统计写出的字节数
作业计数器mapreduce.JobCounter统计任务的作业

2.1 MapReduce 任务计数器

名称描述
map input recordsmap 输入的记录数
map input bytesmap 输入的字节数
map skipped recordsmap 跳过的记录数
map output recordsmap 输出的记录数
map output bytesmap 输出的字节数
split raw bytes分片的原始字节数
map output materialized bytesmap 输出的物化字节数
combine input recordscombine 输入的记录数
combine output recordscombine 输出的记录数
reduce input groupsreduce 输入的组
reduce input recordsreduce 输入的记录数
reduce output recordsreduce 输出的记录数
reduce skipped groupsreduce 跳过的组数
reduce skipped recordsreduce 跳过的字节数
reduce shuffle bytesreduce 结果 shuffle 的字节数
spilled records溢出的记录数
cpu millisecondsCPU 毫秒
physical memory bytes物理内存字节数
virtual memory bytes虚拟内存字节数
committed heap bytes有效的堆字节数
gc time millisgc 运行时间
shuffled maps由 shuffle 传输的 map 输出数
failed shuffle失败的 shuffle 数
merged map outputs被合并的 map 输出数

2.2 文件系统计数器

名称描述
bytes read文件系统的读字节数
bytes waitten文件系统的写字节数

2.3 FileInputFormat 计数器

名称描述
bytes read读的字节数

2.4 FileOutputFormat 计数器

名称描述
bytes waitten写的字节数

2.5 作业计数器

名称描述
total launched maps启用的 map 任务数
total launched reduces启用的 reduce任务数
total launched ubertasks启用的 uber 任务数
num uber submapsuber 中的 map 任务数
num uber subreducesuber 中的reduce 任务数
num failed maps失败的 map任务数
num failed reduces失败的 reduce任务数
num failed ubertasks失败的 uber 任务数
data local maps数据本地化的 map 任务数
rack local maps机架本地化的 map 任务数
other local maps其他本地化的 map 任务数
slots millis mapsmap 任务的总运行时间
slots millis reducesreduce 任务的总运行时间
fallow slots millis maps在保留槽之后,map 任务等待的总时间
fallow slots millis reduces在保留槽之后,reduce 任务等待的总时间

3. 自定义计数器

MapReduce 提供了两种方式直接创建 MapReduce 程序全局计数器,并且使用 Counter.incriment() 进行累加操作。

3.1 窥见源码

  /**
   * 获取给定counterName的计数器
   * @param counterName 计数器名称
   * @return 给定counterName的计数器
   */
  public Counter getCounter(Enum<?> counterName);

  /**
   * 获取给定groupName和counterName的计数器。
   */
  public Counter getCounter(String groupName, String counterName);

3.2 枚举声明计数器

通过 getCounter 传入枚举类型,可以实现计数器的功能。

实现:统计IP数量、统计192开头的IP数量!

Counter counter1 = context.getCounter(IpCounterEnum.IP_Quantity_Statistics);
counter1.increment(1);
Counter counter2 = context.getCounter(IpCounterEnum.IP_Start_With_192);
if (key.toString().startsWith("192")){
    counter2.increment(1);
}

📤 查看输出:

CustomCounter.IpCounterEnum
		IP_Quantity_Statistics=1137
		IP_Start_With_192=32

3.3 自定义声明计数器

通过 getCounter 传入自定义组名及项名,可以实现计数器的功能。

实现:统计IP数量、统计192开头的IP数量!

Counter counter1 = context.getCounter("数量统计", "访问量");
counter1.increment(1);
Counter counter2 = context.getCounter("数量统计", "以192开头的IP");
if (key.toString().startsWith("192")) {
    counter2.increment(1);
}

📤 查看输出:

数量统计
		以192开头的IP=32
		访问量=1137

4. 写在最后

建议使用传入枚举的方式实现信息的统计!

 


❤️ END ❤️
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JOEL-T99

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值