zl程序教程

您现在的位置是:首页 >  大数据

当前栏目

Hadoop大数据——mapreduce中的Combiner/序列化/排序初步

hadoop排序数据 序列化 MapReduce 初步
2023-09-14 09:02:04 时间
  • mapreduce中的Combiner

(1)combiner是MR程序中Mapper和Reducer之外的一种组件
(2)combiner组件的父类就是Reducer
(3)Combiner和reducer的区别在于运行的位置:
Combiner是在每一个maptask所在的节点运行
Reducer是接收全局所有Mapper的输出结果;

  • mapreduce中的序列化

(1)Java的序列化是一个重量级序列化框架(Serializable),一个对象被序列化后,会附带很多额外的信息(各种校验信息,header,继承体系。。。。),所以很臃肿,不便于在网络中高效传输;
所以,hadoop自己开发了一套序列化机制(Writable),精简,高效
简单代码验证两种序列化机制的差别:

public class TestSeri {
	public static void main(String[] args) throws Exception {
		//定义两个ByteArrayOutputStream,用来接收不同序列化机制的序列化结果
		ByteArrayOutputStream ba = new ByteArrayOutputStream();
		ByteArrayOutputStream ba2 = new ByteArrayOutputStream();

		//定义两个DataOutputStream,用于将普通对象进行jdk标准序列化
		DataOutputStream dout = new DataOutputStream(ba);
		DataOutputStream dout2 = new DataOutputStream(ba2);
		ObjectOutputStream obout = new ObjectOutputStream(dout2);
		//定义两个bean,作为序列化的源对象
		ItemBeanSer itemBeanSer = new ItemBeanSer(1000L, 89.9f);
		ItemBean itemBean = new ItemBean(1000L, 89.9f);

		//用于比较String类型和Text类型的序列化差别
		Text atext = new Text("a");
		// atext.write(dout);
		itemBean.write(dout);

		byte[] byteArray = ba.toByteArray();

		//比较序列化结果
		System.out.println(byteArray.length);
		for (byte b : byteArray) {

			System.out.print(b);
			System.out.print(":");
		}

		System.out.println("-----------------------");

		String astr = "a";
		// dout2.writeUTF(astr);
		obout.writeObject(itemBeanSer);

		byte[] byteArray2 = ba2.toByteArray();
		System.out.println(byteArray2.length);
		for (byte b : byteArray2) {
			System.out.print(b);
			System.out.print(":");
		}
	}
}
  • mapreduce的排序初步

MR程序在处理数据的过程中会对数据排序,排序的依据是mapper输出的key