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.ExtendedCell;
027import org.apache.hadoop.hbase.KeyValue;
028import org.apache.hadoop.hbase.KeyValueUtil;
029import org.apache.hadoop.hbase.PrivateCellUtil;
030import org.apache.hadoop.hbase.util.Bytes;
031import org.apache.hadoop.io.serializer.Deserializer;
032import org.apache.hadoop.io.serializer.Serialization;
033import org.apache.hadoop.io.serializer.Serializer;
034import org.apache.yetus.audience.InterfaceAudience;
035
036@InterfaceAudience.Public
037public class CellSerialization implements Serialization<Cell> {
038  @Override
039  public boolean accept(Class<?> c) {
040    return Cell.class.isAssignableFrom(c);
041  }
042
043  @Override
044  public CellDeserializer getDeserializer(Class<Cell> t) {
045    return new CellDeserializer();
046  }
047
048  @Override
049  public CellSerializer getSerializer(Class<Cell> c) {
050    return new CellSerializer();
051  }
052
053  public static class CellDeserializer implements Deserializer<Cell> {
054    private DataInputStream dis;
055
056    @Override
057    public void close() throws IOException {
058      this.dis.close();
059    }
060
061    @Override
062    public KeyValue deserialize(Cell ignore) throws IOException {
063      // I can't overwrite the passed in KV, not from a proto kv, not just yet. TODO
064      return KeyValueUtil.create(this.dis);
065    }
066
067    @Override
068    public void open(InputStream is) throws IOException {
069      this.dis = new DataInputStream(is);
070    }
071  }
072
073  public static class CellSerializer implements Serializer<Cell> {
074    private DataOutputStream dos;
075
076    @Override
077    public void close() throws IOException {
078      this.dos.close();
079    }
080
081    @Override
082    public void open(OutputStream os) throws IOException {
083      this.dos = new DataOutputStream(os);
084    }
085
086    @Override
087    public void serialize(Cell kv) throws IOException {
088      dos.writeInt(PrivateCellUtil.estimatedSerializedSizeOf(kv) - Bytes.SIZEOF_INT);
089      if (kv instanceof ExtendedCell) {
090        ((ExtendedCell) kv).write(dos, true);
091      } else {
092        throw new UnsupportedOperationException(
093          "Unsupported cell type: " + kv.getClass().getName());
094      }
095    }
096  }
097}