MapReduce 编程实践

文章目录

    • 1. MapReduce 作业流程
    • 2. 实践
      • 2.1 启动 hadoop
      • 2.2 创建 java 项目
      • 2.3 MapReduce shell
      • 2.4 MapReduce Web UI
    • 3. MapReduce 编程实践:统计对象中的某些属性

参考书:《Hadoop大数据原理与应用》

1. MapReduce 作业流程


2. 实践

2.1 启动 hadoop

start-dfs.sh
start-yarn.sh
mr-jobhistory-daemon.sh start historyserver
# 第三条可以用下面的命令,上面的显示过期了,以后弃用
mapred --daemon start historyserver

2.2 创建 java 项目

  • WordCountMapper.java
package com.michael.mapreduce;import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable>{//self define map method 自定义 map 方法@Overrideprotected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException{String line = value.toString();String[] words = line.split(" ");for(String word : words)context.write(new Text(word), new IntWritable(1));// context.write() give to next stage: shuffle}
}
  • WordCountReducer.java
package com.michael.mapreduce;import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;public class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable>{// 自定义 reduce 方法@Overrideprotected void reduce(Text key, Iterable<IntWritable> values, Context context) throwsIOException, InterruptedException{int sum = 0;for(IntWritable value : values)sum += value.get();context.write(key, new IntWritable(sum));}
}
  • WordCountDriver.java,dirver 类设置本次 job
package com.michael.mapreduce;import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.compress.BZip2Codec;
import org.apache.hadoop.io.compress.CompressionCodec;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;import java.io.IOException;public class WordCountDriver {// args 参数 输入输出文件路径public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException{Configuration conf = new Configuration();// map compress, 开启 map 阶段的压缩conf.setBoolean("mapreduce.map.output.compress", true);// compress type,指定压缩类型conf.setClass("mapreduce.map.output.compress.codec", BZip2Codec.class, CompressionCodec.class);Job job = Job.getInstance(conf, "word count diy:");job.setJarByClass(WordCountDriver.class);job.setMapperClass(WordCountMapper.class);// 自定义 Combinejob.setCombinerClass(WordCountReducer.class);job.setReducerClass(WordCountReducer.class);// 指定 map 输出数据的类型job.setMapOutputKeyClass(Text.class);job.setMapOutputValueClass(IntWritable.class);// 指定 reduce 输出数据类型job.setOutputKeyClass(Text.class);job.setOutputValueClass(IntWritable.class);// 设置输入文件路径FileInputFormat.setInputPaths(job,  new Path(args[0]));// 设置输出文件路径FileOutputFormat.setOutputPath(job, new Path(args[1]));// 开启 reduce 阶段的解压缩FileOutputFormat.setCompressOutput(job, true);// 指定解压缩类型(跟上面压缩类型一致)FileOutputFormat.setOutputCompressorClass(job, BZip2Codec.class);boolean result = job.waitForCompletion(true);System.exit(result ? 0 : 1);}
}
  • 导出 wordcount_diy.jar
    在这里插入图片描述- 提交hadoop执行
hadoop jar /home/dnn/eclipse-workspace/HDFS_example/wordcount_diy.jar com.michael.mapreduce.WordCountDriver /InputDataTest /OutputDataTest1
  • 查看结果
hdfs dfs -cat /OutputDataTesdfs dfs -cat /OutputDataTest1/part-r-00000.bz2

显示乱码,需要下载然后解压,再查看

  • 下载
hdfs dfs -get /OutputDataTest1/part-r-00000.bz2 /home/dnn/eclipse-workspace/HDFS_example/part-r-00000.bz2
  • 查看
bzcat /home/dnn/eclipse-workspace/HDFS_example/part-r-00000.bz2

在这里插入图片描述

2.3 MapReduce shell

查看作业状态

mapred job -status job_1615849408082_0001
[dnn@master Desktop]$ mapred job -status job_1615849408082_0001
WARNING: HADOOP_MAPRED_PID_DIR has been replaced by HADOOP_PID_DIR. Using value of HADOOP_MAPRED_PID_DIR.
2021-03-26 04:25:14,881 INFO client.DefaultNoHARMFailoverProxyProvider: Connecting to ResourceManager at master/192.168.253.130:8032
2021-03-26 04:25:15,939 INFO mapred.ClientServiceDelegate: Application state is completed. FinalApplicationStatus=SUCCEEDED. Redirecting to job history serverJob: job_1615849408082_0001
Job File: hdfs://192.168.253.130:9000/tmp/hadoop-yarn/staging/history/done/2021/03/24/000000/job_1615849408082_0001_conf.xml
Job Tracking URL : http://master:19888/jobhistory/job/job_1615849408082_0001
Uber job : false
Number of maps: 3
Number of reduces: 1
map() completion: 1.0
reduce() completion: 1.0
Job state: SUCCEEDED
retired: false
reason for failure:  
Counters: 54File System CountersFILE: Number of bytes read=6640FILE: Number of bytes written=1072644FILE: Number of read operations=0FILE: Number of large read operations=0FILE: Number of write operations=0HDFS: Number of bytes read=25631HDFS: Number of bytes written=4967HDFS: Number of read operations=14HDFS: Number of large read operations=0HDFS: Number of write operations=2HDFS: Number of bytes read erasure-coded=0Job Counters Launched map tasks=3Launched reduce tasks=1Data-local map tasks=3Total time spent by all maps in occupied slots (ms)=43801Total time spent by all reduces in occupied slots (ms)=5037Total time spent by all map tasks (ms)=43801Total time spent by all reduce tasks (ms)=5037Total vcore-milliseconds taken by all map tasks=43801Total vcore-milliseconds taken by all reduce tasks=5037Total megabyte-milliseconds taken by all map tasks=44852224Total megabyte-milliseconds taken by all reduce tasks=5157888Map-Reduce FrameworkMap input records=667Map output records=3833Map output bytes=40605Map output materialized bytes=8455Input split bytes=358Combine input records=3833Combine output records=1264Reduce input groups=913Reduce shuffle bytes=8455Reduce input records=1264Reduce output records=913Spilled Records=2528Shuffled Maps =3Failed Shuffles=0Merged Map outputs=3GC time elapsed (ms)=818CPU time spent (ms)=3140Physical memory (bytes) snapshot=599461888Virtual memory (bytes) snapshot=10950950912Total committed heap usage (bytes)=385351680Peak Map Physical memory (bytes)=167784448Peak Map Virtual memory (bytes)=2735529984Peak Reduce Physical memory (bytes)=96972800Peak Reduce Virtual memory (bytes)=2744360960Shuffle ErrorsBAD_ID=0CONNECTION=0IO_ERROR=0WRONG_LENGTH=0WRONG_MAP=0WRONG_REDUCE=0File Input Format Counters Bytes Read=25273File Output Format Counters Bytes Written=4967

2.4 MapReduce Web UI

http://192.168.253.130:19888/jobhistory

在这里插入图片描述

3. MapReduce 编程实践:统计对象中的某些属性

MapReduce 编程实践:统计对象中的某些属性

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/472536.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

linux c代码出现段错误,Linux下段错误(C语言)

问题描述&#xff1a;在Linux下编程有时会出现段错误的提醒&#xff0c;出现这种错误有可能是因为以下几种原因1.数组越界&#xff1a;如果在初始化或者接收输入时内容超过了定义好的数组元素个数时会出现段错误&#xff0c;Linux的数组越界检查做的不是很好&#xff0c;在编译…

micropython webrepl_4-5 MicroPython WebREPL 命令行交互环境设置-2 接入点模式

在这一节教程里我们将一起学习如何为NodeMCU在接入点模式下设置MicroPython网络命令行交互环境(以下简称: WebREPL)。所谓接入点模式就是NodeMCU可以建立WIFI网络供其他设备接入。如下图所示。ESP8266-NodeMCU接入点(Access Point)工作模式在开始设置WebREPL以前请确认您已经完…

基于XMPP实现的Openfire的配置安装+Android客户端的实现

http://blog.csdn.net/sky_monkey/article/details/9495571转载于:https://www.cnblogs.com/eustoma/p/4217028.html

lammps软件_Lammps模型构建的方法之一:组合模型构建

对于Lammps初学者&#xff0c;建模的方法主要有以下几种&#xff1a;1、在Lammps中自行建模&#xff0c;适合金属等简单的模型&#xff0c;如果遇到聚合物就比较麻烦了&#xff1b;2、通过第三方软件建模&#xff0c;例如&#xff1a;Matlab、Python、VMD、Material Studio(MS)…

MapReduce 编程实践:统计对象中的某些属性

文章目录1. 生成数据2. 编写实体类3. Mapper类4. Reducer类5. Driver类6. 运行参考书&#xff1a;《Hadoop大数据原理与应用》 相关文章&#xff1a;MapReduce 编程实践 1. 生成数据 超市消费者 数据&#xff1a; id&#xff0c; 时间&#xff0c;消费金额&#xff0c;会员/…

linux共享数据,使用Linux共享数据对象

Linux共享数据对象类似于windows中的动态链接库&#xff0c;其后缀通常为so.* (*为版本号)&#xff0c;例如为我们所熟知的libpcap&#xff0c;它对应的文件为/usr/lib/libpcap.so。如果程序中使用了某共享数据对象文件&#xff0c;需要在链接时指定gcc的链接参数。如使用libpc…

pythonselenium提高爬虫效率_[编程经验] Python中使用selenium进行动态爬虫

Hello&#xff0c;大家好&#xff01;停更了这么久&#xff0c;中间发生了很多事情&#xff0c;我的心情也发生了很大的变化&#xff0c;看着每天在增长的粉丝&#xff0c;实在不想就这么放弃了&#xff0c;所以以后我会尽量保持在一周一篇的进度&#xff0c;与大家分享我的学习…

超级签名源码_企业签名和超级签名有哪些区别?

我们知道iOS系统对于非App Store中的应用是有安装限制的&#xff0c;而App Store严格的审核机制又将许多APP拒之门外&#xff0c;这令不少开发者们郁闷不已。所以很多开发者们会选择苹果签名的方式&#xff0c;让自己的iOS APP可以不经过App Store就安装在用户的苹果手机上&…

2015-01-14

1.鞋子到了 2.网络一天没有好 3. 又吸烟了,难受 4. 单双杠&#xff1a;60 5. 洗澡&#xff1a;no 6. 仰卧起坐&#xff1a;100 7. 洗脚/刷牙 8.曾的车 9.老梁关世界 总结&#xff1a;今天还好吧&#xff0c;但我还是很想znn&#xff01;&#xff01; 转载于:https://www.cnblo…

天池 在线编程 到达终点

文章目录1. 题目2. 解题1. 题目 描述 A robot is located in a pair of integer coordinates (x, y). It must be moved to a location with another set of coordinates. Though the bot can move any number of times, it can only make the following two types of moves:…

python os函数_python os模块主要函数

使用python提供的os模块&#xff0c;对文件和目录进行操作&#xff0c;重命名文件&#xff0c;添加&#xff0c;删除&#xff0c;复制目录以及文件等。一、文件目录常用函数在进行文件和目录操作时&#xff0c;一般会用到以下几种操作。1、获得当前&#xff1b;路径在python中可…

第十七节(is-a 、is-like-a 、has-a,包和 import )

is - a 类与类之间的继承关系&#xff1b;is - like - a 类与接口之间的关系&#xff1b;has - a 关联关系&#xff1b; public class Animal{public void method01();}// 类与类之间的关系class Dog extends Animal{ // Dog is a Animal} /// public interface I{public void…

quartz获取开始结束时间_Springboot集成quartz

Quartz 是一个完全由 Java 编写的开源作业调度框架,为在 Java 应用程序中进行作业调度提供了简单却强大的机制。本文描述在springboot 2.x环境下怎么集成quartz。一、添加quartz到项目中在pom.xml中加入 <dependency>特别注意application入口类的注解&#xff0c;这里一定…

linux 添加本地磁盘,XenServer如何添加本地存储

在一次测试中&#xff0c;发现本地有两块磁盘&#xff0c;但是只有一块磁盘在XenServer中显示出来&#xff0c;另外一块没有显示。本地只有一个Local storage。查询KB后&#xff0c;发现XenServer可以添加多块本地存储。详情&#xff0c;请见KB&#xff1a;CTX121313详细添加如…

流畅的Python 5. 函数

文章目录1. 函数对象2. 高阶函数3. 匿名函数4. 可调用函数5. 定位参数、仅限关键字参数6. 获取参数信息7. 函数注解8. 支持函数式编程的包1. 函数对象 def factorial(n):returns n! n的阶乘return 1 if n < 2 else n * factorial(n - 1)print(factorial(42)) print(factori…

python方向键键值_python字典键值对的添加和遍历方法

添加键值对 首先定义一个空字典 >>> dic{} 直接对字典中不存在的key进行赋值来添加 >>> dic[name]zhangsan >>> dic {name: zhangsan} 如果key或value都是变量也可以用这种方法 >>> keyage >>> value30 >>> dic[key]val…

无穷大正整数 python_python模块:数字处理

http://blog.csdn.net/pipisorry/article/details/37055183python数字处理简介数字类型python没有unsighed int&#xff1a;The Python int is an abstraction of an integer value, not a direct access to a fixed-byte-size integer.不过int还是当成sighed int处理的&#x…

linux主机基本情况,查看linux主机系统基本信息.pdf

查看linux 主机系统的基本信息一、 硬件信息1. CPUa. Cat /proc/cpuinfo例&#xff1a;[rootlinux victor]# cat /proc/cpuinfoprocessor : 0vendor_id : GenuineIntelcpu family : 6model : 13model name : Intel(R) Celeron(R) M processor 1.50GHzstepping : 8cpu MHz : 150…

LeetCode 1805. 字符串中不同整数的数目(哈希set)

文章目录1. 题目2. 解题1. 题目 给你一个字符串 word &#xff0c;该字符串由数字和小写英文字母组成。 请你用空格替换每个不是数字的字符。 例如&#xff0c;“a123bc34d8ef34” 将会变成 " 123 34 8 34" 。 注意&#xff0c;剩下的这些整数间至少要用一个空格隔…

Android线程之异步消息处理机制(二)——Message、Handler、MessageQueue和Looper

异步消息处理机制解析 Android中的异步消息处理主要有四个部分组成&#xff0c;Message、Handler、MessageQueue和Looper。 1、Message Message是在线程之间传递的消息&#xff0c;它可以在内部携带少量的信息&#xff0c;用于在不同线程之间交换数据。上个例子中就使用了Messa…