这是我参与11月更文挑战的第14天,活动详情查看:2021最后一次更文挑战」
配置
简单化的配置方式就是把hadoop中的配置文件添加到工程的resouces目录下
大数据中的HelloWord → WordCount (如何使用MapReduce进行wordCount)
依赖文件
在POM文件中加入下列的依赖
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.6.5</version>
</dependency>
- ExampleMapper(也就是MapReduce中的map)
/**
* @author jacques huang
* @date 2021年11月10日
*/
public class ExampleMapper extends Mapper<Object, Text, Text, IntWritable> {
private final IntWritable COUNT = new IntWritable(1);
private final Text KEY = new Text();
@Override
protected void map(Object key, Text value, Context context) throws IOException, InterruptedException {
/**
* 数据格式:
* hello hadoop 1
* hello hadoop 2
* 。
* 。
* 。
* key 是每一行字符串自己第一个字节面向源文件的偏移量
* value 就是每一行的数据-> 也就是例如 进来的数据 可能为 hello hadoop 1
*/
/*
StringTokenizer 通过空白切割符号对字符串进行切割
hello hadoop 1 被 切割 为 [hello,hadoop,1]
*/
StringTokenizer tokenizer = new StringTokenizer(value.toString());
while (tokenizer.hasMoreTokens()) {
/**
* 对切割出来的每一个字符进行计数
* COUNT 1
*/
KEY.set(tokenizer.nextToken());
/**
* context.write中已经对传入的KEY与VALUE进行序列化操作
* 所以使用成员变量的话既可以节省内存空间,还可以防止内存抖动
*/
context.write(KEY, COUNT);
}
}
}
- ExampleReduce(也就是MapReduce中的Reduce)
/**
* @author jacques huang
* @date 2021年11月10日
*/
public class ExampleReduce extends Reducer<Text, IntWritable,Text,IntWritable> {
private final IntWritable COUNT =new IntWritable(0);
@Override
protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
/**
* 对每一个进来的key的value进行累加
*/
int count=0;
for (IntWritable value : values) {
count += value.get();
}
COUNT.set(count);
/**
* 输出
*/
context.write(key,COUNT);
}
}
- ExampleMapReduceApplication(启动提交任务类)
/**
* @author jacques huang
* @date 2021/10/19 18:08
*/
public class ExampleMapReduceApplication {
public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
System.setProperty("hadoop.home.dir","E:\\winutils-master\\hadoop-2.6.0\\");
Configuration conf = new Configuration(true);
/**
* 工具类帮我们把-D 等等的属性直接set到conf,会留下commandOptions
*/
GenericOptionsParser parser = new GenericOptionsParser(conf, args);
/**
* 获取args 中非 -D 属性的配置信息 一般为 输入 输出
*/
String[] othargs = parser.getRemainingArgs();
/**
* 让框架知道是windows异构平台运行 在windows 平台需要
*/
conf.set("mapreduce.app-submission.cross-platform","true");
Job job = Job.getInstance(conf);
/**
* 指定程序的jar包路径
*/
job.setJar("F:\\sourceCode\\hadoop\\mapreduce-examples-demo\\target\\mapreduce-examples-demo-1.0.jar");
/**
* 指定程序的启动类
*/
job.setJarByClass(ExampleMapReduceApplication.class);
/**
* 指定任务名称
*/
job.setJobName("example-wordcount");
/**
* 输入数据
*/
Path infile = new Path(othargs[0]);
TextInputFormat.addInputPath(job, infile);
/**
* 输出数据
*/
Path outfile = new Path(othargs[1]);
if (outfile.getFileSystem(conf).exists(outfile)){
outfile.getFileSystem(conf).delete(outfile, true);
}
TextOutputFormat.setOutputPath(job, outfile);
/**
* mapper配置
*/
job.setMapperClass(ExampleMapper.class);
/**
*类型配置
*/
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
/**
*Reduce配置
*/
job.setReducerClass(ExampleReduce.class);
/**
*任务提交
*/
job.waitForCompletion(true);
}
}