一、加载IDEA,加载POM配置
1.打开IDEA软件
2.新建工程
3.创建工程执行结果
4.添加依赖插件
5.加载打包插件
加载主类的全类名时:
①可以先填写cn. bigdata.mapperReducer.JobMain主类名,再根据你填写的内容去依次创建主类名
②或者先创建主类名,再到POM文件中填写cn.bigdata.mapperReducer.JobMain主类名
二、编辑 Mapper 文件
1.新建包
2.新建MapReduce包名
包名:cn.bigdata.mapperReducer
3.新建类
4.新建 ClickRateMapper
5.继承Mapper
在导包时,Mapper类一定要选择hadoop.mappreduce包
6.添加泛型
<LongWritable, Text,Text,LongWritable>分别对应《k1的类型,V1的类型,k2的类型,v2的类型》
7.复写map方法
Ctl+O
8.复写成功,删除super
9.拆分文件,组装k2,v2
Text text = new Text();
LongWritable longWritable = new LongWritable();
//1.将文本进行拆分
String[] split = value.toString().split(",");
//2.遍历数组,组装 k2,v2
for (String word : split) {
//3.将k2,v2写入上下文
text.set(word);
longWritable.set(1);
context.write(text,longWritable);
}
三、编辑Reduce 文件
1.新建ClickReducer类
2.继承Reducer
Reducer类一定要选择mappreduce包的文件
extends Reducer
3.添加泛型
<Text, LongWritable,Text,LongWritale>分别对应《k2的类型,v2的类型,k3的类型,v3的类型》
<Text, LongWritable,Text,LongWritable>
4.复写reduce方法,删除super
5.编写reduce方法
@Override
protected void reduce(Text key, Iterable<LongWritable> values, Reducer<Text, LongWritable, Text, LongWritable>.Context context) throws IOException, InterruptedException {
long count = 0;
for (LongWritable value : values) {
count += value.get();
}
context.write(key,new LongWritable(count));
}
四、编辑JobMain文件
1.新建JobMain类
2.继承Configured类
Configured
3.实现Tool类
implements Tool
4.复写run方法
5.获取配置文件
//获取配置文件参数
Job job = Job.getInstance(super.getConf(),JobMain.class.getSimpleName());
//打包到集群上面运行时,必须要添加以下配置,指定程序的main函数
job.setJarByClass(JobMain.class);
//第一步:读取输入文件的key,value对
job.setInputFormatClass(TextInputFormat.class);
//添加HDFS上文件的路径
TextInputFormat.addInputPath(job,new Path(
"hdfs://192.168.229.111:8020/flume/events1/23-11-16/commodity-.1700125329822"
));
6.设置map输出类型
//第二步,设置我们的mapper类
job.setMapperClass(ClickRateMapper.class);
//设置我们map阶段完成之后的输出类型
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(LongWritable.class);
7.设置reduce输出类型
//第三步:设置的reduce类
job.setReducerClass(ClickRateReducer.class);
//设置reduce阶段完成之后的输出类型
job.setMapOutputKeyClass(Text.class);
job.setOutputValueClass(LongWritable.class);
//第四步:设置输出类型以及输出路径
job.setOutputFormatClass(TextOutputFormat.class);
TextOutputFormat.setOutputPath(job,new Path(
"hdfs://192.168.229.111:8020/flume/count/wordcount_out1"));
8.判断任务是否正常
boolean b = job.waitForCompletion(true);
return b?0:1;
9.程序主类入口
/**
* 程序main函数的入口类
* @param args
* @throws Exception
*/
public static void main(String[] args)throws Exception {
Configuration configuration = new Configuration();
Tool tool = new JobMain();
int run = ToolRunner.run(configuration,tool,args);
System.exit(run);
}
五、打包保存
1.打包
①clean:翻译:打扫清理;在进行真工的悔)之前进行,些清理工作,移除所有上一次构建
生成的文件。
②validate:翻译:验证;验证工程正确性所果更完整否
③compile:翻译:编译;会在你的项目餐下生成一个arget目录,在该目录中包含一个
④classes文件夹,里面全是生成的class文件及字节码文件
⑤test:翻译:测试;单元测试。
⑥package;翻译:打包:将工程文件打包为指定的格式,M如JAR,WAR等(看你项目的pom
文件,里面的packaging标签就是来指定打包类型的)
⑦verify:翻译:核实;主要是对package检查是否有效,科台你修install:翻译:安装:将包安装至本地仓库,以让其它项目球赖site;翻译:站点;生成项目的站点文档。
⑧depioy:翻译:配置部署;复制到远程仓库。
2.打包执行结果
①clickRate-1.0-SNAPSHOT.jar包为不带依赖的包,在执行时需要具备基础的执行环境。②clickRate-1.0-SNAPSHOT-jar-with-dependencies.jar包为带依赖包,在执行时不需要具备基础的执行环境。
3.保存到桌面
谢谢观看