package CartsDemo;
import java.io.IOException;
import java.util.HashMap;
import java.util.Set;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.IntWritable;
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;
import MRDemo.NewTime;
public class carNumber {
public static void main(String[] args) throws Exception {
if (args.length!=2) {
System.exit(0);
}
Job job = new Job (new Configuration(),"carNumber");
job.setJarByClass(carNumber.class);
FileInputFormat.setInputPaths(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
job.setMapperClass(numberMap.class);
job.setReducerClass(numberReduce.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(DoubleWritable.class);
job.waitForCompletion(true);
}
public static class numberMap extends Mapper<Object, Text, Text, IntWritable>{
public static boolean isNumeric(String str){
for (int i = 0; i < str.length(); i++){
//验证字符串中的字符是不是数字
if (!Character.isDigit(str.charAt(i))){
return false;
}
}
return true;
}
protected void map(Object key, Text value, org.apache.hadoop.mapreduce.Mapper<Object,Text,Text,IntWritable>.Context context) throws java.io.IOException ,InterruptedException {
String[] lines = value.toString().split("\t");
//Integer a = Integer.parseInt.(month);
String month = lines[1].trim();
//if(lines.length>1 && >=1 &&lines[1]<=12){
if(lines.length>1 &&isNumeric(lines[1])&&lines[1]!=null){
context.write(new Text(month),new IntWritable(1));
}
/*if (lines.length>1){
}*/
};
}
public static class numberReduce extends Reducer<Text, IntWritable, Text, DoubleWritable>{
Double total = 0.0;
HashMap<String, Integer> map = new HashMap<>();
protected void reduce(Text key, java.lang.Iterable<IntWritable> values, org.apache.hadoop.mapreduce.Reducer<Text,IntWritable,Text,DoubleWritable>.Context context) throws java.io.IOException ,InterruptedException {
int sum = 0;
for (IntWritable value : values) {
sum+=value.get();
}
//context.write(key, new IntWritable(sum));
// 把每种车的品牌与总数加入map中,
map.put(key.toString(),sum );
// 把所有车的总数相加
total+=sum;
};
protected void cleanup(org.apache.hadoop.mapreduce.Reducer<Text,IntWritable,Text,DoubleWritable>.Context context) throws IOException ,InterruptedException {
Set<String > set = map.keySet();
for (String key : set) {
// 通过map中的key获取value
int count = map.get(key);
// 求出每种车所占卖出总数的比例
double percent = count/total;
context.write(new Text(key), new DoubleWritable(percent) );
}
};
}
}
Mapreduce算法三、利用cleanup对reduce结果进行排序
最新推荐文章于 2024-05-08 19:58:34 发布