001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.mapreduce;
019
020import java.io.DataInputStream;
021import java.io.DataOutputStream;
022import java.io.IOException;
023import java.io.InputStream;
024import java.io.OutputStream;
025import org.apache.hadoop.hbase.Cell;
026import org.apache.hadoop.hbase.KeyValue;
027import org.apache.hadoop.hbase.KeyValueUtil;
028import org.apache.hadoop.hbase.PrivateCellUtil;
029import org.apache.hadoop.hbase.util.Bytes;
030import org.apache.hadoop.io.serializer.Deserializer;
031import org.apache.hadoop.io.serializer.Serialization;
032import org.apache.hadoop.io.serializer.Serializer;
033import org.apache.yetus.audience.InterfaceAudience;
034
035/**
036 * Use to specify the type of serialization for the mappers and reducers
037 */
038@InterfaceAudience.Public
039public class CellSerialization implements Serialization<Cell> {
040  @Override
041  public boolean accept(Class<?> c) {
042    return Cell.class.isAssignableFrom(c);
043  }
044
045  @Override
046  public CellDeserializer getDeserializer(Class<Cell> t) {
047    return new CellDeserializer();
048  }
049
050  @Override
051  public CellSerializer getSerializer(Class<Cell> c) {
052    return new CellSerializer();
053  }
054
055  public static class CellDeserializer implements Deserializer<Cell> {
056    private DataInputStream dis;
057
058    @Override
059    public void close() throws IOException {
060      this.dis.close();
061    }
062
063    @Override
064    public KeyValue deserialize(Cell ignore) throws IOException {
065      // I can't overwrite the passed in KV, not from a proto kv, not just yet. TODO
066      return KeyValueUtil.create(this.dis);
067    }
068
069    @Override
070    public void open(InputStream is) throws IOException {
071      this.dis = new DataInputStream(is);
072    }
073  }
074
075  public static class CellSerializer implements Serializer<Cell> {
076    private DataOutputStream dos;
077
078    @Override
079    public void close() throws IOException {
080      this.dos.close();
081    }
082
083    @Override
084    public void open(OutputStream os) throws IOException {
085      this.dos = new DataOutputStream(os);
086    }
087
088    @Override
089    public void serialize(Cell kv) throws IOException {
090      dos.writeInt(PrivateCellUtil.estimatedSerializedSizeOf(kv) - Bytes.SIZEOF_INT);
091      PrivateCellUtil.writeCell(kv, dos, true);
092    }
093  }
094}