一、writable接口
二、使用
1、常规不讲
例如:BooleanWritable、IntWritable、Text等,可通过构造器或者set方法创建,get方法获得。
2、IntWritable、LongWritable、VintWritable和VlongWritable
其中变长类型能节省储存空间,尤其是在-127到127之间的数值;
而对于数值在整个值域空间分布均匀地情况下定长更好,但一般这种情况比较少;
VintWritable和VlongWritable其实是一样的,不用特地转化。
3、ObjectWritable(基本已被废弃)
` //简单使用
ObjectWritable object = new ObjectWritable();
object.set(new byte[]{1,2,3});
Object o = object.get();`
缺点:构造浪费(需要多次判断才能判断出类型),占用空间大。
4、GenericWritable(ObjectWritable的替代方法)
如果已知类型且类型数量比较少,最好用GenericWritable
`/**
* 使用方法:
* 1、创建新类继承GenericWritable
* 2、实现getTypes方法
* 3、private static Class[] myclass=new Class[]{Text.class, IntWritable.class, FloatWritable.class};
*/
public class MyGenericWritable extends GenericWritable {
private static Class[] myclass=new Class[]{Text.class, IntWritable.class, FloatWritable.class};
protected Class<? extends Writable>[] getTypes() {
return myclass;
}
}`
5、ArrayWritable
`ArrayWritable arrayWritable = new ArrayWritable(Text.class);
arrayWritable.set(new IntWritable[6]);`
6、MapWritable和SortedMapWritable
`MapWritable mapWritable = new MapWritable();
mapWritable.put(new Text("first"),new IntWritable(1));`
SortedMapWritable用于排序