Writable、WritableComparable和comparators
hadoop的序列化格式
- package org.apache.hadoop.io;
- public interface Writable {
- void write(java.io.DataOutput p1) throws java.io.IOException;
- void readFields(java.io.DataInput p1) throws java.io.IOException;
- }
- package com.sweetop.styhadoop;
- import junit.framework.Assert;
- import org.apache.hadoop.io.IntWritable;
- import org.apache.hadoop.io.Writable;
- import org.apache.hadoop.util.StringUtils;
- import org.junit.Before;
- import org.junit.Test;
- import java.io.*;
- /**
- * Created with IntelliJ IDEA.
- * User: lastsweetop
- * Date: 13-7-4
- * Time: 下午10:25
- * To change this template use File | Settings | File Templates.
- */
- public class TestWritable {
- byte[] bytes=null;
- /**
- * 初始化一个IntWritable实例,并且调用系列化方法
- * @throws IOException
- */
- @Before
- public void init() throws IOException {
- IntWritable writable = new IntWritable(163);
- bytes = serialize(writable);
- }
- /**
- * 一个IntWritable序列号后的四个字节的字节流
- * 并且使用big-endian的队列排列
- * @throws IOException
- */
- @Test
- public void testSerialize() throws IOException {
- Assert.assertEquals(bytes.length,4);
- Assert.assertEquals(StringUtils.byteToHexString(bytes),"000000a3");
- }
- /**
- * 创建一个没有值的IntWritable对象,并且通过调用反序列化方法将bytes的数据读入到它里面
- * 通过调用它的get方法,获得原始的值,163
- */
- @Test
- public void testDeserialize() throws IOException {
- IntWritable newWritable = new IntWritable();
- deserialize(newWritable,bytes);
- Assert.assertEquals(newWritable.get(),163);
- }
- /**
- * 将一个实现了Writable接口的对象序列化成字节流
- * @param writable
- * @return
- * @throws IOException
- */
- public static byte[] serialize(Writable writable) throws IOException {
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- DataOutputStream dataOut = new DataOutputStream(out);
- writable.write(dataOut);
- dataOut.close();
- return out.toByteArray();
- }
- /**
- * 将字节流转化为实现了Writable接口的对象
- * @param writable
- * @param bytes
- * @return
- * @throws IOException
- */
- public static byte[] deserialize(Writable writable,byte[] bytes) throws IOException {
- ByteArrayInputStream in=new ByteArrayInputStream(bytes);
- DataInputStream dataIn = new DataInputStream(in);
- writable.readFields(dataIn);
- dataIn.close();
- return bytes;
- }
- }
WritableComparable和comparators
- package org.apache.hadoop.io;
- public interface WritableComparable <T> extends org.apache.hadoop.io.Writable, java.lang.Comparable<T> {
- }
MapReduce在排序部分要根据key值的大小进行排序,因此类型的比较相当重要,RawComparator是Comparator的增强版
- package org.apache.hadoop.io;
- public interface RawComparator <T> extends java.util.Comparator<T> {
- int compare(byte[] bytes, int i, int i1, byte[] bytes1, int i2, int i3);
- }
它可以做到,不先反序列化就可以直接比较二进制字节流的大小:
- package com.sweetop.styhadoop;
- import org.apache.hadoop.io.IntWritable;
- import org.apache.hadoop.io.RawComparator;
- import org.apache.hadoop.io.Writable;
- import org.apache.hadoop.io.WritableComparator;
- import org.eclipse.jdt.internal.core.Assert;
- import org.junit.Before;
- import org.junit.Test;
- import java.io.ByteArrayOutputStream;
- import java.io.DataOutputStream;
- import java.io.IOException;
- /**
- * Created with IntelliJ IDEA.
- * User: lastsweetop
- * Date: 13-7-5
- * Time: 上午1:26
- * To change this template use File | Settings | File Templates.
- */
- public class TestComparator {
- RawComparator<IntWritable> comparator;
- IntWritable w1;
- IntWritable w2;
- /**
- * 获得IntWritable的comparator,并初始化两个IntWritable
- */
- @Before
- public void init() {
- comparator = WritableComparator.get(IntWritable.class);
- w1 = new IntWritable(163);
- w2 = new IntWritable(76);
- }
- /**
- * 比较两个对象大小
- */
- @Test
- public void testComparator() {
- Assert.isTrue(comparator.compare(w1, w2) > 0);
- }
- /**
- * 序列号后进行直接比较
- * @throws IOException
- */
- @Test
- public void testcompare() throws IOException {
- byte[] b1 = serialize(w1);
- byte[] b2 = serialize(w2);
- Assert.isTrue(comparator.compare(b1, 0, b1.length, b2, 0, b2.length) > 0);
- }
- /**
- * 将一个实现了Writable接口的对象序列化成字节流
- *
- * @param writable
- * @return
- * @throws java.io.IOException
- */
- public static byte[] serialize(Writable writable) throws IOException {
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- DataOutputStream dataOut = new DataOutputStream(out);
- writable.write(dataOut);
- dataOut.close();
- return out.toByteArray();
- }
- }
Writable、WritableComparable和comparators的更多相关文章
- hadoop中的序列化与Writable接口
本文地址:http://www.cnblogs.com/archimedes/p/hadoop-writable-interface.html,转载请注明源地址. 简介 序列化和反序列化就是结构化对象 ...
- Hadoop开发相关问题
总结自己在Hadoop开发中遇到的问题,主要在mapreduce代码执行方面.大部分来自日常代码执行错误的解决方法,还有一些是对Java.Hadoop剖析.对于问题,通过查询stackoverflow ...
- 分别使用Hadoop和Spark实现二次排序
零.序(注意本部分与标题无太大关系,可直接调至第一部分) 既然没用为啥会有序?原因不想再开一篇文章,来抒发点什么感想或者计划了,就在这里写点好了: 前些日子买了几本书,打算学习和研究大数据方面的知识, ...
- 02Hadoop二次排序2
案例: 数据: 邮编 | 日期 |金额 ILMN,2013-12-05,97.65GOOD,2013-12-09,1078.14IBM,2013-12-09,177.46ILMN, ...
- 01Hadoop二次排序
我的目的: 示例: 2012,01,01,352011,12,23,-42012,01,01,432012,01,01,232011,12,23,52011,4,1,22011,4,1,56 结果: ...
- 解读:MultipleOutputs类
//MultipleOutputs类用于简化多文件输出The MultipleOutputs class simplifies writing output data to multiple outp ...
- 详细讲解MapReduce二次排序过程
我在15年处理大数据的时候还都是使用MapReduce, 随着时间的推移, 计算工具的发展, 内存越来越便宜, 计算方式也有了极大的改变. 到现在再做大数据开发的好多同学都是直接使用spark, hi ...
- 二次排序问题(分别使用Hadoop和Spark实现)
不多说,直接上干货! 这篇博客里的算法部分的内容来自<数据算法:Hadoop/Spark大数据处理技巧>一书,不过书中的代码虽然思路正确,但是代码不完整,并且只有java部分的编程,我在它 ...
- 自定义Writable、RawComparatorWritable、comparators(转)
自定义Writable hadoop虽然已经实现了一些非常有用的Writable,而且你可以使用他们的组合做很多事情,但是如果你想构造一些更加复杂的结果,你可以自定义Writable来达到你的目的,我 ...
随机推荐
- Is Anchor magento
如何在magento分类页的Layered Navigation中可以用magento后台已有的attributes进行筛选. 首先,进入后台 Catalog > Manage Categori ...
- [LeetCode]题解(python):083 - Remove Duplicates from Sorted List
题目来源 https://leetcode.com/problems/remove-duplicates-from-sorted-list/ Given a sorted linked list, d ...
- LightOj1074 - Extended Traffic(SPFA最短路)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1074 题意:有n个城市,每个城市有一个拥堵值a[i],m条单向路u到v,从u到v所需时 ...
- ul和ol的一些知识
ul和ol的一些知识 div#div0 ul{ border:1px solid #ccc; list-style:none; } div#div0 ul li{ border:1px solid g ...
- Android 开发之如何保证Service不被杀掉(broadcast+system/app)
序言 最近项目要实现这样一个效果:运行后,要有一个service始终保持在后台运行,不管用户作出什么操作,都要保证service不被kill,这可真是一个难题.参考了现今各种定制版的系统和安全厂商牛虻 ...
- RouteOS软路由HotSpot热点认证网关
实现要求: 实现局域网有线无线需在网页输入用户名和密码登录,不同用户登录有不同的访问内外网权限. 环境要求: 一台PC机安装三张网卡,第一张网卡连接外网,第二张网卡配置局域网,第三张网卡做配置连接使用 ...
- windows下统计代码量
windows 工具 1.exe程序 http://blog.csdn.net/hui1502/article/details/51191678 https://sourceforge.net/pro ...
- J2EE sitemesh使用
maven包含sitemesh: <dependency> <groupId>opensymphony</groupId> <artifactId>si ...
- <s:iterator> 对list操作的一种方法
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA6IAAAH3CAIAAAAuRnW9AAAgAElEQVR4nOzdf4gk1333+wLDDffhPp
- poj1703 Find them, Catch them 并查集
poj(1703) Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26992 ...