MapReduce(全局排序)

本文介绍了一种使用两次MapReduce作业进行页面访问计数排序的方法。首次MapReduce用于汇总各页面的访问次数,二次MapReduce则利用内部排序机制,对结果进行排序。最后的ReduceTask设置为1,确保了全局排序的正确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

主要分类两次MapReduce, 最后一次MapReduce 的ReduceTask需要设置为1个

1. 自定义序列化数据类型

package com.gerry.bigdata.mapreduce.pagecountsort;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

import org.apache.hadoop.io.WritableComparable;;

public class PageCount implements WritableComparable<PageCount> { // 自定义数据类型,序列化

	private String page;
	private int count;

	public PageCount() {
		// TODO Auto-generated constructor stub
	}

	public void set(String page, int count) {
		this.page = page;
		this.count = count;
	}

	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	@Override
	public int compareTo(PageCount o) {
		// TODO Auto-generated method stub
		return o.getCount() - this.getCount() == 0 ? this.page.compareTo(o.getPage()) : o.getCount() - this.getCount();
	}

	@Override
	public void write(DataOutput out) throws IOException {
		// TODO Auto-generated method stub
		out.writeUTF(this.page);
		out.writeInt(this.count);

	}

	@Override
	public void readFields(DataInput in) throws IOException {
		// TODO Auto-generated method stub
		this.page = in.readUTF();
		this.count = in.readInt();

	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return this.page + "," + this.count;
	}

}

2. 第一次的MapReduce

package com.gerry.bigdata.mapreduce.pagecountsort;

import java.io.IOException;

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.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 PageCountStep1 {

	public static class PageCountStep1Mapper extends Mapper<LongWritable, Text, Text, IntWritable> {
		@Override
		protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
			String line = value.toString();
			String[] splits = line.split(" ");
			context.write(new Text(splits[1]), new IntWritable(1));

		}

	}

	public static class PageCountStep1Reducer extends Reducer<Text, IntWritable, Text, IntWritable> {

		@Override
		protected void reduce(Text Key, Iterable<IntWritable> values, Context context)
				throws IOException, InterruptedException {
			int count = 0;
			for (IntWritable value : values) {

				count += value.get();
			}

			context.write(Key, new IntWritable(count));

		}

	}

	public static void main(String[] args) throws Exception {
		Configuration conf = new Configuration();
		Job job = Job.getInstance(conf);

		job.setJarByClass(PageCountStep1.class);
//		job.setJar("/home/gerry/pyspark/Bigdata/jars/pagecount.jar"); //在集群上运行
		job.setMapperClass(PageCountStep1Mapper.class);
		job.setReducerClass(PageCountStep1Reducer.class);

		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(IntWritable.class);

		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(IntWritable.class);

		Path inputPath = new Path("/home/gerry/pyspark/Bigdata/data/requests/input");
		Path outPath = new Path("/home/gerry/pyspark/Bigdata/data/requests/countout");
//		FileSystem fs = FileSystem.get(new URI("hdfs://172.16.0.2:9000/"), conf, "root");
//		if (fs.exists(outPath)) {
//			fs.delete(outPath, true);
//		}

		FileInputFormat.setInputPaths(job, inputPath);
		FileOutputFormat.setOutputPath(job, outPath);

		job.setNumReduceTasks(3);
		boolean result = job.waitForCompletion(true);
		System.exit(result ? 0 : 1);

	}

}

3. 第二次的MapReduce

package com.gerry.bigdata.mapreduce.pagecountsort;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
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 PageCountStep2 {
	
	public static class PageCountStep2Mapper extends Mapper<LongWritable, Text, PageCount, NullWritable>{
		@Override
		protected void map(LongWritable key, Text value, Context context)
				throws IOException, InterruptedException {
			String line = value.toString();
			String[] splits = line.split("\t");
			PageCount pageCount = new PageCount();
			pageCount.set(splits[0], Integer.parseInt(splits[1]));
			context.write(pageCount, NullWritable.get());
		}
	}
	
	//借用mapreduce 的shuffle阶段的内部排序机制
	public static class PageCountStep2Reducer extends Reducer<PageCount, NullWritable, PageCount, NullWritable>{
		
		@Override
		protected void reduce(PageCount key, Iterable<NullWritable> values,Context context)
				throws IOException, InterruptedException {
			context.write(key, NullWritable.get());
		}
		
	}
	
	
	public static void main(String[] args) throws Exception {
		Configuration conf = new Configuration();
		Job job = Job.getInstance(conf);

		job.setJarByClass(PageCountStep2.class);
//		job.setJar("/home/gerry/pyspark/Bigdata/jars/pagecount.jar"); //在集群上运行
		job.setMapperClass(PageCountStep2Mapper.class);
		job.setReducerClass(PageCountStep2Reducer.class);

		job.setMapOutputKeyClass(PageCount.class);
		job.setMapOutputValueClass(NullWritable.class);

		job.setOutputKeyClass(PageCount.class);
		job.setOutputValueClass(NullWritable.class);

		Path inputPath = new Path("/home/gerry/pyspark/Bigdata/data/requests/countout");
		Path outPath = new Path("/home/gerry/pyspark/Bigdata/data/requests/countoutput");
//		FileSystem fs = FileSystem.get(new URI("hdfs://172.16.0.2:9000/"), conf, "root");
//		if (fs.exists(outPath)) {
//			fs.delete(outPath, true);
//		}

		FileInputFormat.setInputPaths(job, inputPath);
		FileOutputFormat.setOutputPath(job, outPath);

		job.setNumReduceTasks(1);
		boolean result = job.waitForCompletion(true);
		System.exit(result ? 0 : 1);

	}
	

}

注意:本文涉及的代码都是在跨平台的Debug调试环境下,注意区分!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值