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.classification.InterfaceAudience;
27  import org.apache.hadoop.hbase.classification.InterfaceStability;
28  import org.apache.hadoop.hbase.KeyValue;
29  import org.apache.hadoop.io.serializer.Deserializer;
30  import org.apache.hadoop.io.serializer.Serialization;
31  import org.apache.hadoop.io.serializer.Serializer;
32  @InterfaceAudience.Public
33  @InterfaceStability.Evolving
34  public class KeyValueSerialization implements Serialization<KeyValue> {
35    @Override
36    public boolean accept(Class<?> c) {
37      return KeyValue.class.isAssignableFrom(c);
38    }
39  
40    @Override
41    public KeyValueDeserializer getDeserializer(Class<KeyValue> t) {
42      return new KeyValueDeserializer();
43    }
44  
45    @Override
46    public KeyValueSerializer getSerializer(Class<KeyValue> c) {
47      return new KeyValueSerializer();
48    }
49  
50    public static class KeyValueDeserializer implements Deserializer<KeyValue> {
51      private DataInputStream dis;
52  
53      @Override
54      public void close() throws IOException {
55        this.dis.close();
56      }
57  
58      @Override
59      public KeyValue deserialize(KeyValue ignore) throws IOException {
60        // I can't overwrite the passed in KV, not from a proto kv, not just yet.  TODO
61        return KeyValue.create(this.dis);
62      }
63  
64      @Override
65      public void open(InputStream is) throws IOException {
66        this.dis = new DataInputStream(is);
67      }
68    }
69  
70    public static class KeyValueSerializer implements Serializer<KeyValue> {
71      private DataOutputStream dos;
72  
73      @Override
74      public void close() throws IOException {
75        this.dos.close();
76      }
77  
78      @Override
79      public void open(OutputStream os) throws IOException {
80        this.dos = new DataOutputStream(os);
81      }
82  
83      @Override
84      public void serialize(KeyValue kv) throws IOException {
85        KeyValue.write(kv, this.dos);
86      }
87    }
88  }