hadoop-serializations

一. Writable

note:part of codes are from other's blog!here is a integrated and optimized shards.

 

package test;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.DefaultStringifier;

public class serializerWritable {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		Configuration conf = new Configuration();
		conf.set(
				"io.serializations",
				//TestSerializer uses Java's Serialization.
                                //if Testcase is used by that,here must be uncomment.
//				"org.apache.hadoop.io.serializer.JavaSerialization," + 
				"org.apache.hadoop.io.serializer.WritableSerialization"
				);
		TestSerializerWritable ts = new TestSerializerWritable(1, "测试呀");
		DefaultStringifier<TestSerializerWritable> ds = new DefaultStringifier<TestSerializerWritable>(
				conf, TestSerializerWritable.class);
		String s = null;
		try {
			s = ds.toString(ts);	//invoke ts's serialization method(write) automatically
		} catch (IOException e) {
			e.printStackTrace();
		}
                //if u used java serialization ,u will see the result  is space-cost much  than this
		System.out.println(s);
		TestSerializerWritable tsxp = null;
		try {
			tsxp = ds.fromString(s); //invoke deserialization method(read)
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println(tsxp.getA() + ":" + tsxp.getB());
	}

}

package test;

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

import org.apache.hadoop.io.Writable;

public class TestSerializerWritable implements Writable{

	private int a;
	private String b;
	public TestSerializerWritable( ) {
		
	}

	public TestSerializerWritable(int a, String b) {
		super();
		this.a = a;
		this.b = b;
	}

	public int getA() {
		return a;
	}

	public void setA(int a) {
		this.a = a;
	}

	public String getB() {
		return b;
	}

	public void setB(String b) {
		this.b = b;
	}

	@Override
	public void write(DataOutput out) throws IOException {
		out.writeInt(a);
		out.writeUTF(b);
		
	}

	@Override
	public void readFields(DataInput in) throws IOException {
		a = in.readInt();
		b = in.readUTF();
//		byte[] bb = new byte[1];
//		in.readFully(bb);
//		b = new String(bb);
	}
} 

 

here is a tips to not to use java objects to serialize to SequenceFile

 

 

 

//TODO

 

 

References:

http://blog.sina.com.cn/s/blog_5cec1e1d0100oi8p.html

你可能感兴趣的:(hadoop)