View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.mapreduce;
19  
20  import java.io.DataInputStream;
21  import java.io.DataOutputStream;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.io.OutputStream;
25  
26  import org.apache.hadoop.hbase.KeyValue;
27  import org.apache.hadoop.hbase.KeyValueUtil;
28  import org.apache.hadoop.hbase.classification.InterfaceAudience;
29  import org.apache.hadoop.hbase.classification.InterfaceStability;
30  import org.apache.hadoop.io.serializer.Deserializer;
31  import org.apache.hadoop.io.serializer.Serialization;
32  import org.apache.hadoop.io.serializer.Serializer;
33
34  @InterfaceAudience.Public
35  @InterfaceStability.Evolving
36  public class KeyValueSerialization implements Serialization<KeyValue> {
37    @Override
38    public boolean accept(Class<?> c) {
39      return KeyValue.class.isAssignableFrom(c);
40    }
41
42    @Override
43    public KeyValueDeserializer getDeserializer(Class<KeyValue> t) {
44      return new KeyValueDeserializer();
45    }
46
47    @Override
48    public KeyValueSerializer getSerializer(Class<KeyValue> c) {
49      return new KeyValueSerializer();
50    }
51
52    public static class KeyValueDeserializer implements Deserializer<KeyValue> {
53      private DataInputStream dis;
54
55      @Override
56      public void close() throws IOException {
57        this.dis.close();
58      }
59
60      @Override
61      public KeyValue deserialize(KeyValue ignore) throws IOException {
62        // I can't overwrite the passed in KV, not from a proto kv, not just yet.  TODO
63        return KeyValueUtil.create(this.dis);
64      }
65
66      @Override
67      public void open(InputStream is) throws IOException {
68        this.dis = new DataInputStream(is);
69      }
70    }
71
72    public static class KeyValueSerializer implements Serializer<KeyValue> {
73      private DataOutputStream dos;
74
75      @Override
76      public void close() throws IOException {
77        this.dos.close();
78      }
79
80      @Override
81      public void open(OutputStream os) throws IOException {
82        this.dos = new DataOutputStream(os);
83      }
84
85      @Override
86      public void serialize(KeyValue kv) throws IOException {
87        KeyValueUtil.write(kv, this.dos);
88      }
89    }
90  }