一、MapReduce程序的打包运行过程:
1> 选中待打包项目,右键选择菜单export,导出项目
2> 点击Next进行下一步操作,选择需要打包的类,输入导出jar包的名称和路径。(可以报lib包去掉,集群上包含的有mr的依赖包)
3> 继续点击Next,在如下画面选择执行文件的主函数类,点击Finish完成导出
4> 把导出的jar包通过wincsp等ftp工具上传到集群服务器上。
5> 通过hadoop命令在集群上执行wordcount.jar包
命令:hadoop jar wordcount.jar
6> 执行结果如下:
二、MR程序在Yarn环境下运行过程:
1> 案例:计算单词出现的词频(本地运行模式)
文件E:\\word.txt,内容:
郑州,开封,洛阳,南阳,信阳,驻马店,安阳,
郑州,开封,洛阳,南阳,信阳,驻马店,安阳,
郑州,开封,洛阳,南阳,信阳,驻马店,安阳,
郑州,开封,洛阳,南阳,信阳,驻马店,安阳,
郑州,开封,洛阳,南阳,信阳,驻马店,安阳,
郑州,开封,洛阳,南阳,信阳,驻马店,安阳,
周口,周口,郑州,开封,洛阳,周口,开封
2> 词频统计代码实现:
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;public class WordCountLocalToYarn {
/**
* mapper类
*
* @author Administrator
*
*/
public static class WordCountMapper extends Mapper<LongWritable, Text, Text, LongWritable> {@Override
protected void map(LongWritable key, Text line, Mapper<LongWritable, Text, Text, LongWritable>.Context context)
throws IOException, InterruptedException {
String[] words = line.toString().split(",");
System.out.println(words.length);
for (String w : words) {
System.out.println(w);
context.write(new Text(w), new LongWritable(1));
}
}
}/**
* reducer类
*
* @author Administrator
*
*/
public static class WordCountReducer extends Reducer<Text, LongWritable, Text, LongWritable> {@Override
protected void reduce(Text text, Iterable<LongWritable> counts,
Reducer<Text, LongWritable, Text, LongWritable>.Context context)
throws IOException, InterruptedException {
long sum = 0;
for (LongWritable i : counts) {
System.out.println(i);
sum += 1;
}
context.write(text, new LongWritable(sum));
}}
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
Configuration conf = new Configuration();
// 为什么显示设置?因为默认读取本地项目中的默认配置文件,里面是local
// 运行在yarn上
conf.set("mapreduce.framework.name", "yarn");
// 告诉程序所运行的resourcemanager服务器
conf.set("yarn.resourcemanager.hostname", "192.168.248.100");
conf.set("fs.defaultFS", "hdfs://192.168.248.100:9000");// 告诉hadoop要兼容windows向Linux直接提交程序的兼容性
conf.set("mapreduce.app-submission.cross-platform", "true");
// 本地运行前,先手动导出包,如下,这样当直接在myEclipse环境下运行程序时,才会将这个jar自动提交到远端Yarn集群上运行,
// 通过开启JobHistoryServer通过web界面可以看到提交上来的任务
conf.set("mapred.jar", "E://wordcount.jar");Job job = Job.getInstance(conf, "word");
job.setJarByClass(WordCountLocalToYarn.class);job.setMapperClass(WordCountMapper.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(LongWritable.class);job.setReducerClass(WordCountReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(LongWritable.class);
FileInputFormat.setInputPaths(job, new Path("/input"));
FileOutputFormat.setOutputPath(job, new Path("/output"));boolean res = job.waitForCompletion(true);
if (res) {
System.out.println("success");
} else {
System.out.println("failse");
}}
}
3> 安装上述步骤进行打包生成指定位置的wordcount.jar包
4> 运行主函数,直接将jar包上传到服务器,并进行词频统计处理。