package one;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
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.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class TestYear {
public static void main(String[] args) throws IOException,
ClassNotFoundException,
InterruptedException {
Configuration config = new Configuration();
config.set("fs.defaultFS", "hdfs://192.168.0.100:9000");
config.set("yarn.resourcemanager.hostname", "192.168.0.100");
FileSystem fs = FileSystem.get(config);
Job job = Job.getInstance(config);
job.setJarByClass(TestYear.class);
//设置所用到的map类
job.setMapperClass(myMapper.class);
job.setMapOutputKeyClass(NullWritable.class);
job.setMapOutputValueClass(Text.class);
//设置用到的reducer类
job.setReducerClass(myReducer.class);
job.setOutputKeyClass(NullWritable.class);
job.setOutputValueClass(Text.class);
//设置输出地址
FileInputFormat.addInputPath(job, new Path("/zhoukao3/"));
Path path = new Path("/output1/");
if (fs.exists(path)) {
fs.delete(path, true);
}
//指定文件的输出地址
FileOutputFormat.setOutputPath(job, path);
//启动处理任务job
boolean completion = job.waitForCompletion(true);
if (completion) {
System.out.println("Job Success!");
}
}
public static class myMapper extends Mapper < LongWritable,
Text,
NullWritable,
Text > {
@Override protected void map(LongWritable key, Text value, Context context) throws IOException,
InterruptedException {
String values = value.toString();
String words[] = values.split("[-]| "); //2012,3,1,a
String s = words[0] + "年" + words[1] + "月" + words[2] + "日" + " " + words[3];
context.write(NullWritable.get(), new Text(s));
}
}
public static class myReducer extends Reducer < NullWritable,
Text,
NullWritable,
Text > {
@Override protected void reduce(NullWritable key, Iterable < Text > values, Context context) throws IOException,
InterruptedException {
for (Text value: values) {
context.write(key, value);
}
}
}
}
来源: http://www.bubuko.com/infodetail-2228912.html