学习mapreduce编程
分别写三个类
Mapper类
package com.j.mapreduce.wordcount2;
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;
/*
* KEYIN,map阶段输入的key的类型:Longwritable
*VALUEIN,map阶段输入的value的类型:TEXT
*KEYOUT,map阶段输出的key的类型:TEXT
* VALUEOUT,map阶段输出的value的类型:IntWritable
* */
public class wordcountMapper extends Mapper<LongWritable, Text,Text, IntWritable> {
private Text outkey =new Text();
private IntWritable outvalue=new IntWritable(1);
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
//获取一行
String line = value.toString();//alue.toString().var 用.var可以自动生产变量
//切割
String[] words = line.split(" ");//分割标志根据原文件来写
//循环写出
for (String word : words) {
//封装成outkey
outkey.set(word);
//写出
context.write(outkey, outvalue);
}
}
}
reducer类
package com.j.mapreduce.wordcount2;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
/*
* KEYIN,reduce阶段输入的key的类型:Text
*VALUEIN,reduce阶段输入的value的类型:Intwritable
*KEYOUT,reduce阶段输出的key的类型:TEXT
* VALUEOUT,reduce阶段输出的value的类型:IntWritable
* */
public class wordcountReducer extends Reducer<Text, IntWritable,Text,IntWritable> {
@Override
protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
int sum=0;
//Iterable<IntWritable> values 一个集合 是迭代器的祖宗 可以通过迭代器方式访问
//values.iterator().hasNext();
//累加
for (IntWritable value : values) {
sum+=value.get();
}
//转换成intwritable类型
IntWritable outvalue = new IntWritable();
outvalue.set(sum);
//写出
context.write(key,outvalue);
}
}
Driver类
package com.j.mapreduce.wordcount2;
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.获取job
Configuration conf = new Configuration();
Job job = Job.getInstance(conf);
//2.设置jar包路径
job.setJarByClass(wordcountDriver.class);
//3.关联mapper和reduce
job.setMapperClass(wordcountMapper.class);
job.setReducerClass(wordcountReducer.class);
//4.设置map输出的kv类型
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
//5.设置最终输出的kv类型
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
//6.设置输入路径和输出路径
FileInputFormat.setInputPaths(job,new Path(args[0]));
FileOutputFormat.setOutputPath(job,new Path(args[1]));
//提交obj
boolean result = job.waitForCompletion(true);
System.exit(result? 0 : 1);
}
}
将写好的程序通过maven打包成jar包,放到服务器进行运行
hadoop jar wc.jar com.j.mapreduce.wordcount2.wordcountDriver /sanguo /output 注意末尾的输出路径要不存在
注意如果xshell报错,我在查阅了一番资料之后,需要修改一个地方,
Container exited with a non-zero exit code 1. Error file: prelaunch.err.
Last 4096 bytes of prelaunch.err :
Last 4096 bytes of stderr :
Error: Could not find or load main class org.apache.hadoop.mapreduce.v2.app.MRAppMaster
Please check whether your etc/hadoop/mapred-site.xml contains the below configuration:
<property>
<name>yarn.app.mapreduce.am.env</name>
<value>HADOOP_MAPRED_HOME=${full path of your hadoop distribution directory}</value>
</property>
<property>
<name>mapreduce.map.env</name>
<value>HADOOP_MAPRED_HOME=${full path of your hadoop distribution directory}</value>
</property>
<property>
<name>mapreduce.reduce.env</name>
<value>HADOOP_MAPRED_HOME=${full path of your hadoop distribution directory}</value>
</property>
可以通过修改自己的mapred-site.xml
来修改
<property>
<name>mapreduce.application.classpath</name>
<value>
/opt/module/hadoop-3.1.3/etc/*,
/opt/module/hadoop-3.1.3/etc/hadoop/*,
/opt/module/hadoop-3.1.3/lib/*,
/opt/module/hadoop-3.1.3/share/hadoop/common/*,
/opt/module/hadoop-3.1.3/share/hadoop/common/lib/*,
/opt/module/hadoop-3.1.3/share/hadoop/mapreduce/*,
/opt/module/hadoop-3.1.3/share/hadoop/mapreduce/lib-examples/*,
/opt/module/hadoop-3.1.3/share/hadoop/hdfs/*,
/opt/module/hadoop-3.1.3/share/hadoop/hdfs/lib/*,
/opt/module/hadoop-3.1.3/share/hadoop/yarn/*,
/opt/module/hadoop-3.1.3/share/hadoop/yarn/lib/*,
</value>
</property>
注意里面的路劲与自己Linux的系统中hadoop路径相同
并分发给其他集群,xsync mapred-site.xml 即可
学习时间:12:19到15:54