20,810
社区成员
发帖
与我相关
我的任务
分享
package test;
import java.io.IOException;
import java.util.StringTokenizer;
//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.mapred.JobConf;
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;
import org.apache.hadoop.util.GenericOptionsParser;
public class WordCount {
public static class TokenizerMapper
extends Mapper<Object, Text, Text, IntWritable>{
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
public void map(Object key, Text value, Context context
) throws IOException, InterruptedException {
StringTokenizer itr = new StringTokenizer(value.toString());
while (itr.hasMoreTokens()) {
word.set(itr.nextToken());
context.write(word, one);
}
}
}
public static class IntSumReducer
extends Reducer<Text,IntWritable,Text,IntWritable> {
private IntWritable result = new IntWritable();
public void reduce(Text key, Iterable<IntWritable> values,
Context context
) throws IOException, InterruptedException {
int sum = 0;
for (IntWritable val : values) {
sum += val.get();
}
result.set(sum);
context.write(key, result);
}
}
public static void main(String[] args) throws Exception {
//System.setProperty("HADOOP_USER_NAME", "hadoop");
JobConf conf = new JobConf();
String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
if (otherArgs.length != 2) {
System.err.println("Usage: wordcount <in> <out>");
System.exit(2);
}
//conf.set("hadoop.job.ugi", "hadoop");
//conf.set("hadoop.job.user", "hadoop");
// conf.set("mapred.job.tracker", hostIp + ":9001");
//conf.set("fs.defaultFS", "hdfs://n1:9000");
// conf.set("mapred.map.tasks", "2");
System.setProperty("HADOOP_USER_NAME", "hadoop");
//conf.set("mapreduce.job.queuename", "test");
//conf.set("fs.default.name", "hdfs://n1:8020");
conf.set("fs.defaultFS", "hdfs://n1:8020");
conf.set("hadoop.job.user","hadoop");
conf.set("mapreduce.framework.name","yarn");
conf.set("mapreduce.jobtracker.address","n1:9001"); //这个端口配置文件如果不设置默认是多少啊,官方文档写的默认是"local" 后面的英文说明也没看明白。端口实在太多了 好乱。
conf.set("yarn.resourcemanager.hostname", "n1");
conf.set("yarn.resourcemanager.scheduler.address", "n1:8030");
conf.set("yarn.resourcemanager.resource-tracker.address", "n1:8031");
conf.set("yarn.resourcemanager.address", "n1:8032"); //
conf.set("yarn.resourcemanager.admin.address", "n1:8033");
//conf.set("mapred.jar", "/Users/chenlong/Documents/hadoop_root/hadoop-2.7.1/myjar.jar");
//Job job = new Job(conf, "word count");
Job job = Job.getInstance(conf, "wordcount11");
job.setJarByClass(WordCount.class);
job.setMapperClass(TokenizerMapper.class);
//Uncomment this to
job.setCombinerClass(IntSumReducer.class);
job.setReducerClass(IntSumReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
String input = "hdfs://n1:8020/user/hadoop/input/wordcount.txt";
String output = "hdfs://n1:8020/user/hadoop/output/wordcount5";
FileInputFormat.addInputPath(job, new Path(input));
FileOutputFormat.setOutputPath(job, new Path(output));
//FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
//FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}