submit
package wc;
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 org.apache.log4j.BasicConfigurator;
public class IDES {
public static void main(String[] args) throws Exception {
BasicConfigurator.configure(); //自动快速地使用缺省Log4j环境。
System.setProperty("HADOOP_USER_NAME","cla");//配置jvm变量
Configuration conf = new Configuration();//
//默认在本地运行,这里会直接读取win里里面的hadoop配置,所以可以把conf.set省略
//conf.set("fs.defaultFS", "file:///");
//conf.set("mapreduce.framework.name", "local");
// 在hdfs上运行
conf.set("fs.defaultFS", "hdfs://192.168.43.241:9000");
conf.set("mapreduce.framework.name", "yarn");
conf.set("yarn.resourcemanager.hostname","192.168.43.241");
conf.set("mapreduce.app-submission.cross-platform","true");//兼容win
Job job = Job.getInstance(conf);
//mapper,reducer,submitter三个类
// job.setJarByClass(JobSubmitter.class);
job.setJar("d:/wc.jar");
job.setMapperClass(CountMapper.class);
job.setReducerClass(CountReducer.class);
//mapper,reducer输出key和value的类型
job.setMapOutputKeyClass(Text.class);//mapper
job.setMapOutputValueClass(IntWritable.class);
job.setOutputKeyClass(Text.class);//reducer
job.setOutputValueClass(IntWritable.class);
FileInputFormat.setInputPaths(job, new Path("/wordcount/input"));
FileOutputFormat.setOutputPath(job, new Path("/wordcount/output"));
job.setNumReduceTasks(3);
boolean res = job.waitForCompletion(true);
System.exit(res?0:1);
}
}
mapper
package wc;
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;
/**
* 但是,在mapreduce中,map产生的数据需要传输给reduce,需要进行序列化和反序列化,java原生的数据类型会很慢,
* 所以hadoop为jdk中的常用基本类型Long String Integer Float等数据类型封住了自己的实现了hadoop序列化接口的类型:
* LongWritable,Text,IntWritable,FloatWritable
*
*/
public class CountMapper extends Mapper<LongWritable, Text, Text, IntWritable>{
//KEYIN :是map task读取到的一行的起始偏移量Long,hadoop中新建了LongWritable类型来替代
//VALUEIN:是map task读取到的该行的内容,类型是String,hadoop中用Text来表示
//KEYOUT:返回的key,用Text,既字母
//VALUEOUT:返回的value,用Integer,既字母的个数
@Override
protected void map(LongWritable key, Text value, Context context)//Context既输出的key-value的容器,可以看成是一个大的字典
throws IOException, InterruptedException {
String line = value.toString(); //将一行Text数据转换为String
String[] words = line.split(" ");//分割单词
for(String word:words){//将分割的每一个单词写入到字典
context.write(new Text(word), new IntWritable(1));
}
}
}
reduce
package wc;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
import java.util.Iterator;
public class CountReducer extends Reducer<Text, IntWritable, Text, IntWritable>{
@Override
protected void reduce(Text key, Iterable<IntWritable> values,Context context) throws IOException, InterruptedException {
int count = 0;
Iterator<IntWritable> iterator = values.iterator();
while(iterator.hasNext()){
IntWritable value = iterator.next();
count += value.get();
}
context.write(key, new IntWritable(count));
}
}
相关资源
IDE直接和hadoop集群连接