本文记录用java实现map-reduce,并在服务器上运行的代码。
package com.demo.hadoop.test;
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.Arrays;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.FileUtil;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.Mapper.Context;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.mapreduce.v2.app.job.TaskAttempt;
import org.apache.hadoop.util.GenericOptionsParser;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.demo.hadoop.test.AvgScore.MyMap;
import com.demo.hadoop.test.AvgScore.MyReduce;
import com.sun.media.sound.SoftLowFrequencyOscillator;
public class NbRoute implements Tool {
public static final Logger log = LoggerFactory.getLogger(NbRoute.class);
Configuration configuration;
public static class MyMap extends Mapper {
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String[] citation = value.toString().split(",");
context.write(new Text(citation[0]), new Text(citation[1]));
}
}
public static class MyReduce extends Reducer {
public void reduce(Text key, Iterable values, Context context) throws IOException, InterruptedException {
String csv = "";
for (Text val : values) {
if (csv.length() > 0) {
csv += ",";
}
if (!csv.contains(val.toString()))
csv += val.toString();
}
String[] tmp = csv.split(",");
Arrays.sort(tmp);
String scenic = "";
if (tmp.length > 2) {
for (int i = 0; i < tmp.length; i++){
scenic += tmp[i];
scenic += ",";
}
String sctmp = scenic.substring(0,scenic.length()-1);
context.write(key, new Text(sctmp));
}
}
}
@Override
public int run(String[] args) throws Exception {
// TODO Auto-generated method stub
Configuration conf = new Configuration();
Job job = new Job(conf, "NbRoute");
job.setJarByClass(NbRoute.class);
job.setJobName("NbRoute");
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
job.setMapperClass(MyMap.class);
job.setCombinerClass(MyReduce.class);
job.setReducerClass(MyReduce.class);
job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);
FileInputFormat.setInputPaths(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
boolean success = job.waitForCompletion(true);
System.exit(job.waitForCompletion(true) ? 0 : 1);
return success ? 0 : 1;
}
@Override
public Configuration getConf() {
return configuration;
}
@Override
public void setConf(Configuration conf) {
conf = new Configuration();
configuration = conf;
}
public static void main(String[] args) throws Exception {
int ret = ToolRunner.run(new NbRoute(), args);
System.exit(ret);
}
} 从eclipse中导成jar包放到服务器上运行,代码运行命令:hadoop jar avgscore.jar com.demo.hadoop.test.NbRoute /APPLICATION/YIWUXSP/oidd/test/201703 /APPLICATION/YIWUXSP/oidd/test/out1