MapReduce API

ProvincPar

ProvincPar
package a.b.c;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
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.Partitioner;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;


class ProParMapper extends Mapper<LongWritable, Text, Text, IntWritable>{
	 @Override
	protected void map(LongWritable key, Text value,Context context)
			throws IOException, InterruptedException {
		 String [] provString=value.toString().trim().split(",");
		 int provCode=Integer.parseInt(provString[0]);
		 String provName=provString[1];
		 
		 context.write(new Text(provName), new IntWritable(provCode));
	}
		
}

class ProParPartition extends Partitioner<Text, IntWritable>{
	
	private Map<String, Integer> getProMap(){
		String[] provinceString={"湖南省","湖北省","北京市","上海市"};
		Map<String,Integer> map = new HashMap<String,Integer>();
		for (int i=0;i<(provinceString.length);i++){
			map.put(provinceString[i],i);
		}
		return map;
	}
	//reduce分组数
	@Override
	public int getPartition(Text key, IntWritable values, int numPartitions) {
		Map<String, Integer> map = getProMap();
		if (map.containsKey(key.toString().trim())) {
			System.out.println(map.get(key.toString()));
			System.out.println(map.size());
			return map.get(key.toString().trim());
		} else {
			return map.size() + 1;
		}
	}
}


class ProParReducer extends Reducer<Text, IntWritable, Text, IntWritable>{
	@Override
	protected void reduce(Text key, Iterable<IntWritable> value,Context context)
			throws IOException, InterruptedException {
		int sum=0;
		for (IntWritable each:value){
			sum+=each.get();
		}
		context.write(key,new IntWritable(sum));
	}
	
	
}


public class ProvincPar {
	public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
		Configuration configuration=new Configuration();
		Job job=Job.getInstance(configuration);
		job.setJarByClass(ProvincPar.class);
		
		job.setMapperClass(ProParMapper.class);
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(IntWritable.class);
		
		job.setPartitionerClass(ProParPartition.class);
		
		job.setReducerClass(ProParReducer.class);
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(IntWritable.class);
		job.setNumReduceTasks(6);
		
		Path path = new Path(args [1]);
		FileSystem fileSystem = path.getFileSystem(configuration);
		if (fileSystem.exists(path)) {
			fileSystem.delete((path),true);
		}

		FileInputFormat.addInputPath(job, new Path(args[0]));
		FileOutputFormat.setOutputPath(job, new Path(args[1]));
		
		
		System.exit(job.waitForCompletion(true) ? 0 : 1);
		
		
	}
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值