经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 大数据/云/AI » Hadoop » 查看文章
大数据学习(二)-------- MapReduce
来源:cnblogs  作者:独孤风  时间:2019/4/16 8:48:12  对本文有异议

前提已经安装好hadoop的hdfs集群,可以查看

**https://www.cnblogs.com/tree1123/p/10683570.html

Mapreduce是hadoop的运算框架,可以对hdfs中的数据分开进行计算,先执行很多maptask,在执行reducetask,这个过程中任务的执行需要一个任务调度的平台,就是yarn。

一、安装YARN集群

yarn集群中有两个角色:

主节点:Resource Manager ?1台

从节点:Node Manager ??N台

?

Resource Manager一般安装在一台专门的机器上

Node Manager应该与HDFS中的data node重叠在一起

修改配置文件:yarn-site.xml

  1. <property>
  2. <name>yarn.resourcemanager.hostname</name>
  3. <value>主机名</value>
  4. </property>
  5. <property>
  6. <name>yarn.nodemanager.aux-services</name>
  7. <value>mapreduce_shuffle</value>
  8. </property>
  9. <property>
  10. <name>yarn.nodemanager.resource.memory-mb</name>
  11. <value>2048</value>
  12. </property>
  13. <property>
  14. <name>yarn.nodemanager.resource.cpu-vcores</name>
  15. <value>2</value>
  16. </property>

然后scp到所有机器,修改主节点hadoop的slaves文件,列入要启动nodemanager的机器,配好免密

然后,就可以用脚本启动yarn集群:

sbin/start-yarn.sh

停止:

sbin/stop-yarn.sh

页面:http://主节点:8088 看看node manager节点是否识别

开发一个提交job到yarn的客户端类,mapreduce所有jar和自定义类,打成jar包上传到hadoop集群中的任意一台机器上,运行jar包中的(YARN客户端类

hadoop jar ......JobSubmitter

二、开发mapreduce程序

主要需要开发:

map阶段的进、出数据,

reduce阶段的进、出数据,

类型都应该是实现了HADOOP序列化框架的类型,如:

String对应Text

Integer对应IntWritable

Long对应LongWritable

例子wordcount代码:

WordcountMapper

  1. public class WordcountMapper extends Mapper<LongWritable, Text, Text, IntWritable>{
  2. @Override
  3. protected void map(LongWritable key, Text value, Context context)
  4. throws IOException, InterruptedException {
  5. // 切单词
  6. String line = value.toString();
  7. String[] words = line.split(" ");
  8. for(String word:words){
  9. context.write(new Text(word), new IntWritable(1));
  10. }
  11. }
  12. }

WordcountReducer

  1. public class WordcountReducer extends Reducer<Text, IntWritable, Text, IntWritable>{
  2. @Override
  3. protected void reduce(Text key, Iterable<IntWritable> values,Context context) throws IOException, InterruptedException {
  4. int count = 0;
  5. Iterator<IntWritable> iterator = values.iterator();
  6. while(iterator.hasNext()){
  7. IntWritable value = iterator.next();
  8. count += value.get();
  9. }
  10. context.write(key, new IntWritable(count));
  11. }
  12. }
  13. public class JobSubmitter {
  14. public static void main(String[] args) throws Exception {
  15. // 在代码中设置JVM系统参数,用于给job对象来获取访问HDFS的用户身份
  16. System.setProperty("HADOOP_USER_NAME", "root");
  17. Configuration conf = new Configuration();
  18. // 1、设置job运行时要访问的默认文件系统
  19. conf.set("fs.defaultFS", "hdfs://hdp-01:9000");
  20. // 2、设置job提交到哪去运行
  21. conf.set("mapreduce.framework.name", "yarn");
  22. conf.set("yarn.resourcemanager.hostname", "hdp-01");
  23. // 3、如果要从windows系统上运行这个job提交客户端程序,则需要加这个跨平台提交的参数
  24. conf.set("mapreduce.app-submission.cross-platform","true");
  25. Job job = Job.getInstance(conf);
  26. // 1、封装参数:jar包所在的位置
  27. job.setJar("d:/wc.jar");
  28. //job.setJarByClass(JobSubmitter.class);
  29. // 2、封装参数: 本次job所要调用的Mapper实现类、Reducer实现类
  30. job.setMapperClass(WordcountMapper.class);
  31. job.setReducerClass(WordcountReducer.class);
  32. // 3、封装参数:本次job的Mapper实现类、Reducer实现类产生的结果数据的key、value类型
  33. job.setMapOutputKeyClass(Text.class);
  34. job.setMapOutputValueClass(IntWritable.class);
  35. job.setOutputKeyClass(Text.class);
  36. job.setOutputValueClass(IntWritable.class);
  37. Path output = new Path("/wordcount/output");
  38. FileSystem fs = FileSystem.get(new URI("hdfs://hdp-01:9000"),conf,"root");
  39. if(fs.exists(output)){
  40. fs.delete(output, true);
  41. }
  42. // 4、封装参数:本次job要处理的输入数据集所在路径、最终结果的输出路径
  43. FileInputFormat.setInputPaths(job, new Path("/wordcount/input"));
  44. FileOutputFormat.setOutputPath(job, output); // 注意:输出路径必须不存在
  45. // 5、封装参数:想要启动的reduce task的数量
  46. job.setNumReduceTasks(2);
  47. // 6、提交job给yarn
  48. boolean res = job.waitForCompletion(true);
  49. System.exit(res?0:-1);
  50. }
  51. }

MR还有一些高级的用法:自定义类型,自定义Partitioner,Combiner,排序,倒排索引,自定义GroupingComparator

三、mapreduce与yarn的核心机制

yarn是一个分布式程序的运行调度平台

yarn中有两大核心角色:

1、Resource Manager

接受用户提交的分布式计算程序,并为其划分资源

管理、监控各个Node Manager上的资源情况,以便于均衡负载

?

2、Node Manager

管理它所在机器的运算资源(cpu + 内存)

负责接受Resource Manager分配的任务,创建容器、回收资源

Mapreduce工作机制:

划分输入切片——》 环形缓冲区 ——》 分区排序 ——》Combiner 局部聚合——》shuffle ——》GroupingComparator——》输出

原文链接:http://www.cnblogs.com/tree1123/p/10711141.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号