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来达到你的目的,我 ...
随机推荐
- 获取设备唯一标识 uuid(采用第三方库SSKeychain)
SSKeyChain 下载链接: http://pan.baidu.com/s/1booV3VD 密码: ivdi /** * 获取设备唯一标识 uuid */ +(NSString*) uuid ...
- iOS 隐藏Status Bar
要隐藏,有3个地方要做: 1.在info.Plist里,将该属性的hidden,设置为YES,这样,在启动时,就不会显示了: 2.在application-didFinish里面写,这样,可以隐藏io ...
- JS-005-常见下拉列表 Select 和 datalist
下拉列表在我们日常的网页浏览的过程中,随处可见,是 web 编程过程中大家非常熟悉的一个页面元素,随着 HTML 语言的日益强大,其在广大攻城狮的手中可谓是千变万化,有了很多不同的实现方式.本文主要以 ...
- java web filter 之一 基础实现
本文主要对filter的基本使用进行了讲解,其中涉及到了 filter是什么 一个filter处理一个jsp 多个filter处理一个jsp filter是什么 Filter 是java下的一种过滤器 ...
- github上所有大于800 star OC框架
https://github.com/XCGit/awesome-objc-frameworks#awesome-objc-frameworks awesome-objc-frameworks ID ...
- LeetCode Flip Game
原题链接在这里:https://leetcode.com/problems/flip-game/ 题目: You are playing the following Flip Game with yo ...
- LeetCode Word Break II
原题链接在这里:https://leetcode.com/problems/word-break-ii/ 题目: Given a string s and a dictionary of words ...
- 股票中带有ST和*ST的股票是什么意思啊?一图了解新三板与主板、中小板、创业板制度差异!
st表示已经亏损超过1年以上的 *st表示连续亏损3年以上,并且有退市风险的,随时可能会“退市”到时候你的钱可能都拿不会来 主板股票代码上海60开头,深圳000开头,通常指大中型企业:中小板002开头 ...
- Android Studio IDE 主题设置
1.界面主题设置,如下图: 2.代码字体设置,如下图:
- C# Main函数的 args参数
网上参考 博客,使用如下代码: using System; using System.Collections.Generic; using System.Linq; using System.Text ...