mr案例

(1)创建maven项目

(2)在po,.xml添加下面代码

<dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.7.30</version></dependency><dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-common</artifactId><version>3.1.3</version></dependency><dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-client</artifactId><version>3.1.3</version></dependency><dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-hdfs</artifactId><version>3.1.3</version><scope>test</scope></dependency></dependencies>

(3)在项目的src/main/resources目录下,新建一个文 件,命名为“log4j.properties”,在文件中填入。

log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=target/spring.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n

编写程序

(1)编写Mapper类

package com.scy;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> {//实例化数据类型对象Text k = new Text();IntWritable v = new IntWritable(1);@Overrideprotected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {//1.获取一行数据String line =  value.toString();//2.切割字符串String[] words =  line.split(" ");//3.输出for(String word : words){k.set(word);context.write(k,v);}}
}

(2)编写Reducer类

package com.scy;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> {int sum;IntWritable v = new IntWritable();@Overrideprotected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {//1.累加求和sum = 0;for(IntWritable count: values){sum += count.get();}//2.输出v.set(sum);context.write(key,v);}
}

(3)编写Driver驱动类

package com.scy;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.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 {public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {// 1 获取配置信息以及封装任务Configuration configuration = new Configuration();Job job = Job.getInstance();// 2 设置jar加载路径job.setJarByClass(WordCountDriver.class);// 3 设置map和reduce类job.setMapperClass(WordCountMapper.class);job.setReducerClass(WordCountReducer.class);// 4 设置map输出job.setMapOutputKeyClass(Text.class);job.setMapOutputValueClass(IntWritable.class);// 5 设置最终输出kv类型job.setOutputKeyClass(Text.class);job.setMapOutputValueClass(IntWritable.class);// 6 设置输入和输出路径FileInputFormat.setInputPaths(job,new Path("D:\\input\\hello.txt"));FileOutputFormat.setOutputPath(job,new Path("D:\\output"));// 7 提交boolean result = job.waitForCompletion(true);System.out.println(result?0:1);}
}

(5)本地测试

(1)如果电脑系统是win7的就将win7的hadoop jar包
解压到非中文路径,并在Windows环境上配置
HADOOP_HOME环境变量。如果是电脑win10操作系
统,就解压win10的hadoop jar包,并配置
HADOOP_HOME环境变量。
注意:win8电脑和win10家庭版操作系统可能有问题,
需要重新编译源码或者更改操作系统。
(2)在Eclipse/Idea上运行程序

(6)集群上测试

将路径改成下方代码的路径

        // 6 设置输入和输出路径FileInputFormat.setInputPaths(job,new Path(args[0]));FileOutputFormat.setOutputPath(job,new Path(args[1]));

(0)用maven打jar包,需要添加的打包插件依赖
注意:标记红颜色的部分需要替换为自己工程主类

<build>
<plugins>
<plugin>
<artifactId>maven-compilerplugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assemblyplugin </artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jarwith-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.xyd.WordcountDriver</mainCla
ss>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>makeassembly</id>
<phase>package</phase><goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

注意:如果工程上显示红叉。在项目上右键->maven->update project即可。
(1)将程序打成jar包,然后拷贝到Hadoop集群中
(/opt/modle/hadoop-3.1.3)
步骤详情:右键->Run as->maven install。等待编译完
成就会在项目的target文件夹中生成jar包。如果看不
到。在项目上右键-》Refresh,即可看到。修改不带依
赖的jar包名称为mr.jar,并拷贝该jar包到Hadoop集
群。
(2)启动Hadoop集群(linux)
(3)执行WordCount程序 (linux)

[admin@hadoop1002 hadoop-3.1.3]$ hadoop jar
mr.jar com.xyd.WordCountDriver /input
/output

以上就是mr案例的所有的本地测试和集群测试,请大家参考。

hadoop之mr案例相关推荐

  1. MR案例:Reduce-Join

    问题描述:两种类型输入文件:address(地址)和company(公司)进行一对多的关联查询,得到地址名(例如:Beijing)与公司名(例如:Beijing JD.Beijing Red Star ...

  2. Hadoop之企业案例分析

    Hadoop之企业案例分析 目录 海量日志数据,提取出某日访问百度次数最多的那个IP 有一个1G大小的一个文件,里面每一行是一个词,词的大小不超过16字节,内存限制大小是1M.返回频数最高的100 个 ...

  3. Hadoop hdfs编程案例和java交互

    Hadoop hdfs编程案例 一. HDFS编程实践 二.利用Java API与HDFS进行交互 三.应用程序的部署 一. HDFS编程实践 启动hadoop 切换到hadoop安装目录 cd /u ...

  4. 图解hadoop的MR计算流程

    图解hadoop的MR计算流程: 1.左边部分是map阶段,右边部分是reduce阶段 1)我们可以看出左边的有四个map task,一般情况下一个map tasl处理一个split的数据,一个spl ...

  5. 【Hadoop】四、Hadoop生态综合案例 ——陌陌聊天数据分析

    文章目录 四.Hadoop生态综合案例 --陌陌聊天数据分析 1.陌陌聊天数据分析案例需求 1.1.背景介绍 1.2.目标需求 1.3.数据内容 2.基于Hive数仓实现需求开发 2.1.建库建表.加 ...

  6. MR案例:CombineFileInputFormat

    CombineFileInputFormat是一个抽象类.Hadoop提供了两个实现类CombineTextInputFormat和CombineSequenceFileInputFormat. 此案 ...

  7. 大数据讲课笔记5.7 MR案例—TopN

    文章目录 零.学习目标 一.导入新课 二.新课讲解 (一)案例分析 1.TopN分析法介绍 2.案例需求及分析 (二)案例实现 1.准备数据文件 (1)在虚拟机上创建文本文件 (2)上传文件到HDFS ...

  8. hadoop jar包_【大数据学习】Hadoop的MR分布式开发小实战

    前提:hadoop集群应部署完毕. 一.实战科目 做一个Map Reduce分布式开发,开发内容为统计文件中的单词出现次数. 二.战前准备 1.本人在本地创建了一个用于执行MR的的文件,文件中有209 ...

  9. 大数据学习笔记22:MR案例——双MR统计总利润并排序

    文章目录 一.提出任务 二.解题思路 1.第一个mr计算每个人总利润 2.第二个mr对总利润进行排序 三.准备工作 1.启动hadoop服务 2.上传数据文件到HDFS 3.创建Maven项目Doub ...

最新文章

  1. 合肥工业大学—SQL Server数据库实验十:用户及其权限管理
  2. mysql数据库可以升级吗_[数据库]MySQL升级
  3. JFreeChart 使用介绍
  4. 单张表超过30个字段_拉链表
  5. LeetCode MySQL 1132. 报告的记录 II
  6. php微信支付接口开发程序(概念篇)
  7. 构建meteor应用程序_我构建了一个渐进式Web应用程序并将其发布在3个应用程序商店中。 这是我学到的。...
  8. Linux之find常用命令汇总
  9. 浅析B/S架构数据库连接方式
  10. [Flex]浅析Mate flex framework在实际项目中的应用(二)
  11. 各 Delphi 历史版本下载合集
  12. 项目质量管理的几种常规方法
  13. 最全计算机网络期末考试试题及答案
  14. Android AES 文件加密解密
  15. Scrapy爬取豆瓣读书全站
  16. linux启动db2的命令窗口_linux下如何启动db2数据库命令窗口
  17. php中网页生成图片的方式,类似长微博图片生成器
  18. RNN及变体LSTM、GRU(在NILM中的应用)
  19. NVIDIA Jetson tx2 cuda和cudnn安装_刷机失败,手动离线安装
  20. 关于branch XYZ is published (but not merged) and is now N commits behind错误的一点分析

热门文章

  1. 2021一级计算机考证(全)
  2. 高通AR摄像机参数详解
  3. 夜神模拟器链接eclipse并导出模拟器中app的db文件,查看sqlite数据库过程
  4. 文件防泄密系统如何保障企业文档的安全性?
  5. ASP.NET Aries 入门开发教程6:列表数据表格的格式化处理及行内编辑
  6. linux boost filesystem程序链接,c-在Ubuntu 13.04上链接boost :: filesystem
  7. 【财富空间】AI淘金指南:不想成为最先死掉的那批?这是变身黄金矿工的秘籍
  8. 数字农业与智慧农业的区别和差异
  9. DynaSLAM2 2020论文翻译
  10. word 粘贴的表格超出了页面边界导致不能调整宽度