import java.io.IOException;
import java.io.PrintStream;
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.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reporter;
import org.apache.hadoop.mapred.lib.MultipleOutputs;
import org.apache.hadoop.mapreduce.InputSplit;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.Reducer.Context;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;
public class BET {
public static class TokenizerMapper
extends Mapper<Object, Text, Text, Text>{
private final static IntWritable one = new IntWritable(1);
private final static Text creditBalance = new Text("creditBalance");
private final static Text betString = new Text("betString");
private Text word = new Text();
public void map(Object key, Text value, Context context
) throws IOException, InterruptedException {
// value =new Text("hello zhrg bey zhrg") ;
StringTokenizer itr = new StringTokenizer(value.toString());
while (itr.hasMoreTokens()) {
word.set(itr.nextToken());
// context.write(word, one);
}
FileSplit fs = (FileSplit) context.getInputSplit();
Path grandpa = fs.getPath().getParent().getParent();
String date = grandpa.getName();
if(date == null)
throw new RuntimeException("The input path is not right. No date field can be extracted!");
word.set("creditBalance1creditBalance2:"+key);
context.write(creditBalance,word);
word.set("betString1betString2:"+key);
context.write(betString,word);
}
}
//对每一个数字进行汇总求和
public static class SumCombiner extends
Reducer<Text,Text,Text,Text> {
private LongWritable result = new LongWritable();
private Text k = new Text("midsum");
private Text text = new Text();
public void reduce(Text key, Iterable<Text> values,
Context context
) throws IOException, InterruptedException {StringBuffer sb = new StringBuffer();
for(Text tx:values){
sb.append(tx.toString());
}
text.set(sb.toString());
context.write(key, text);
}
}
public static class IntSumReducer
extends Reducer<Text,Text,Text,Text> {
private IntWritable result = new IntWritable();
private Text text = new Text();
public void reduce(Text key, Iterable<Text> values,
Context context
) throws IOException, InterruptedException {
int sum = 0;
/* for (IntWritable val : values) {
sum += val.get();
}*/
// result.set(sum);
StringBuffer sb = new StringBuffer();
for(Text tx:values){
sb.append(tx.toString());
}
text.set(sb.toString());
context.write(key, text);
}
}
public static void main(String[] args) throws Exception {
// Path path = new Path("/hadoop/file01");
Configuration conf = new Configuration();
String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
if (otherArgs.length != 2) {
System.err.println("Usage: wordcount <in> <out>");
System.exit(2);
}
HDFS_File fu = new HDFS_File();
String src = "/install/sumnum.txt";
// HadoopFileUtil hu = new HadoopFileUtil();
// String src = "/hadoop/files02";
/*String dst = "hdfs://s21:9000/user/sumnum";
if(!fu.CheckFileExist(conf, dst))
fu.PutFile(conf, src, dst);*/
// fu.putFileFormLocal(conf,"/install/sumnum.txt", otherArgs[0] );
/* String str = otherArgs[1]+"/sum_its4";
if(fu.CheckFileExist(conf, str))
fu.DelFile(conf, str, true);
str = otherArgs[1]+"/_SUCCESS";
if(fu.CheckFileExist(conf, str));
fu.DelFile(conf, str, true);
str = otherArgs[1];
if(fu.CheckFileExist(conf, str));
fu.DelFile(conf, str, true);*/
Job job = new Job(conf, "word count");
job.setJarByClass(BET.class);
job.setMapperClass(TokenizerMapper.class);
job.setCombinerClass(SumCombiner.class);
job.setReducerClass(IntSumReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
// job.setOutputFormatClass(coAuOutputFormat.class);
FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
System.out.println("ok");
}
}
mapreduce Bet
猜你喜欢
转载自zhrglchp.iteye.com/blog/1483347
今日推荐
周排行