资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

hadoop中mapreducez如何自定义分区

这篇文章主要为大家展示了“hadoop中mapreducez如何自定义分区”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“hadoop中mapreducez如何自定义分区”这篇文章吧。

创新互联是一家集网站建设,南通企业网站建设,南通品牌网站建设,网站定制,南通网站建设报价,网络营销,网络优化,南通网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。

package hello_hadoop;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.DoubleWritable;
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;
public class AutoParitionner {
	public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
		if(args.length!=2) {
			System.err.println("Usage: hadoop jar xxx.jar  ");
			System.exit(1);
		}
		Configuration conf = new Configuration();
		Job job = Job.getInstance(conf, "avg of grades");
		job.setJarByClass(AutoParitionner.class);
		job.setMapperClass(PartitionInputClass.class);
		job.setReducerClass(PartitionOutputClass.class);
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(DoubleWritable.class);
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(DoubleWritable.class);
		//声明自定义分区的类,下面有类的声明
		job.setPartitionerClass(MyPartitioner.class);
		job.setNumReduceTasks(2);
		FileInputFormat.addInputPath(job, new Path(args[0]));
		FileOutputFormat.setOutputPath(job, new Path(args[1]));
		
		System.exit(job.waitForCompletion(true)?0:1);
		
	}
	}
class PartitionInputClass extends Mapper{
	@Override
	protected void map(LongWritable key, Text value, Mapper.Context context)
			throws IOException, InterruptedException {
		String line = value.toString();
		if(line.length()>0){
			String[] array = line.split("\t");
			if(array.length==2){
				String name=array[0];
				int grade = Integer.parseInt(array[1]);
				context.write(new Text(name), new DoubleWritable(grade));
			}
		}
		
		
	}
	
}
class PartitionOutputClass extends Reducer{
	@Override
	protected void reduce(Text text, Iterable iterable,
			Reducer.Context context) throws IOException, InterruptedException {
		int sum = 0;
		int cnt= 0 ;
		for(DoubleWritable iw : iterable) {
			sum+=iw.get();
			cnt++;
		}
		context.write(text, new DoubleWritable(sum/cnt));
	}
	
}
//自定义分区的类
//Partitioner Text,DoubleWirtable分别为map结果的key,value
class MyPartitioner extends Partitioner{
	@Override
	public int getPartition(Text text, DoubleWritable value, int numofreuceTask) {
		String name = text.toString();
		if(name.equals("wd")||name.equals("wzf")||name.equals("xzh")||name.equals("zz")) {
			return 0;		
		}else
			return 1;
	}
	}

以上是“hadoop中mapreducez如何自定义分区”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!


分享文章:hadoop中mapreducez如何自定义分区
转载源于:http://cdkjz.cn/article/pjijse.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220