Mapreduce编程
mapreduce编程
跑一个mapreduce程序 需要yarn
执行命令:
hadoop jar jar包文件路径(绝对的、相对的) 主函数 参数
1 | 上传文件到 hadoop |
出现错误
1 | [2019-11-05 20:54:32.193]Container exited with a non-zero exit code 127. Error file: prelaunch.err. |
wordcount运行日志
1 | [root@hadoop01 mapreduce]# hadoop jar hadoop-mapreduce-examples-3.2.1.jar wordcount /in /wcout |
WordCount 程序官方
下载 jar包
1 | <dependency> |
/maven-repo/org/apache/hadoop/hadoop-mapreduce-examples/3.2.1/hadoop-mapreduce-examples-3.2.1.jar!/org/apache/hadoop/examples/WordCount.class
观察依赖
ExampleDriver
这里就是 hadoop jar XXX.jar 主函数
主函数位置写的是 wordcount的原因
1 | package org.apache.hadoop.examples; |
WordCount
1 | // |
编程结构
1 |
|
自己写 MapReduce
Driver.java :组装 map 和 reduce
1 | package pers.sfl.mapreduce; |
WordCountMapper.java
1 | package pers.sfl.mapreduce; |
WordCountReduce.java
1 | package pers.sfl.mapreduce; |
jar上传运行
将编写好的程序打成jar包,上传到服务器任意一个节点
运行
运行 指定main函数,程序中指定路径, 没有运行参数的:
hadoop jar wc01.jar
1 | 报错:Error: java.io.IOException: Initialization of all the collectors failed. Error in last collector was :class com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$Text |
没有指定main函数:
1 | 主函数的类一定是 全限定名 |
没有主函数 又需要参数的
1 | hadoop jar wc03_nomain_pa.jar pers.sfl.mapreduce.Driver /wcin /wc_out02 |
说明
文件输入(加载的类):
FileInputFormat
,默认的实现类是:TextInputFormat
文件读取: 默认的文件读取器:
RecordReader
,默认实现类:LineRecordReader
LineRecordReader的核心方法
1 | //判断当前的文件是否还有下一个 k-v 要读取 |
过程
- 通过文件加载器和读取器,将文件内容发送到Map 端
- Map 端实现map方法(LongWritable key, Text value, Context context)
- 将内容进行切割得到(helllo,1),(helllo,1),(helllo,1),(world,1)
- shuffle 过程
- 分区:partitioner
- 排序: sort按照 key进行排序(字典顺序) writablecomparable
- 分组:grop相同的 key 分为一组 writablecomparator
- combiner 组件:优化
- reduce接收到的是key相同的一组数据
- FileInputFormat ->
TextOutputFormat
文件输出类 - 使用文件写出器
RecordWriter
(输出流) ->LineRecordWriter
写到 hdfs
mapreduce的编程套路
- reduce: 计算统计
- 一般map端:只做数据的字段的摘取
3个文件中最大值 最小值 平均值
maptask:
每次只能取到一行的内容
拿到每一个数值 -> 发送
map:输出
如何判断key:根据reduce端如何接受
map输出的key:分组
想按找哪一个字段进行分组 map的key就是那一个字段
reduce一组会调用一次
key:“数据” Text
value:数据的值
reducetask:一组
接受的数据 所有的数据分到一组
一次性访问所有的数据'
将所有的数据取出--values 求最大值 最小值 平均值
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 凉月の博客!
评论