将这三个文件打成jar包,然后放在集群里用hadoop jar命令来运行hadoop jar wc.jar wc.LinuxS
hadoop jar命令会自动调取本地依赖和配置文件
jobcommit
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 LinuxS {
public static void main(String[] args) throws Exception {
BasicConfigurator.configure(); //自动快速地使用缺省Log4j环境。
System.setProperty("HADOOP_USER_NAME","root");//配置jvm变量
Configuration conf = new Configuration();
// 在hdfs上运行
conf.set("fs.defaultFS", "hdfs://master:9000");
conf.set("mapreduce.framework.name", "yarn");//默认linux单机运行local
conf.set("yarn.resourcemanager.hostname","master");
conf.set("mapreduce.app-submission.cross-platform","true");//兼容win
conf.set("yarn.app.mapreduce.am.resource.mb", "500");// 防止虚拟机内存报错
Job job = Job.getInstance(conf);
//mapper,reducer,submitter三个类
job.setJarByClass(LinuxS.class); //在hadoop集群里提交jar包,可以用这个方法,免去设置jar包
// job.setJar("d:/wc.jar"); //在idea里提交,需要指明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("/input"));
FileOutputFormat.setOutputPath(job, new Path("/output"));
job.setNumReduceTasks(2);
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));
}
}
}
reducer
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));
}
}
相关资料:
Linux提交hadoop任务