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.KeyValue;
026import org.apache.hadoop.hbase.KeyValueUtil;
027import org.apache.hadoop.io.serializer.Deserializer;
028import org.apache.hadoop.io.serializer.Serialization;
029import org.apache.hadoop.io.serializer.Serializer;
030import org.apache.yetus.audience.InterfaceAudience;
031
032/**
033 * Use to specify the type of serialization for the mappers and reducers
034 * @deprecated Use {@link CellSerialization}. Will be removed from 3.0 onwards
035 */
036@Deprecated
037@InterfaceAudience.Public
038public class KeyValueSerialization implements Serialization<KeyValue> {
039  @Override
040  public boolean accept(Class<?> c) {
041    return KeyValue.class.isAssignableFrom(c);
042  }
043
044  @Override
045  public KeyValueDeserializer getDeserializer(Class<KeyValue> t) {
046    return new KeyValueDeserializer();
047  }
048
049  @Override
050  public KeyValueSerializer getSerializer(Class<KeyValue> c) {
051    return new KeyValueSerializer();
052  }
053
054  public static class KeyValueDeserializer implements Deserializer<KeyValue> {
055    private DataInputStream dis;
056
057    @Override
058    public void close() throws IOException {
059      this.dis.close();
060    }
061
062    @Override
063    public KeyValue deserialize(KeyValue ignore) throws IOException {
064      // I can't overwrite the passed in KV, not from a proto kv, not just yet. TODO
065      return KeyValueUtil.create(this.dis);
066    }
067
068    @Override
069    public void open(InputStream is) throws IOException {
070      this.dis = new DataInputStream(is);
071    }
072  }
073
074  public static class KeyValueSerializer implements Serializer<KeyValue> {
075    private DataOutputStream dos;
076
077    @Override
078    public void close() throws IOException {
079      this.dos.close();
080    }
081
082    @Override
083    public void open(OutputStream os) throws IOException {
084      this.dos = new DataOutputStream(os);
085    }
086
087    @Override
088    public void serialize(KeyValue kv) throws IOException {
089      KeyValueUtil.write(kv, this.dos);
090    }
091  }
092}