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.IOException;
21  import java.io.InputStream;
22  import java.io.OutputStream;
23  
24  import org.apache.hadoop.hbase.client.Result;
25  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
26  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos;
27  import org.apache.hadoop.io.serializer.Deserializer;
28  import org.apache.hadoop.io.serializer.Serialization;
29  import org.apache.hadoop.io.serializer.Serializer;
30  
31  public class ResultSerialization implements Serialization<Result> {
32    @Override
33    public boolean accept(Class<?> c) {
34      return Result.class.isAssignableFrom(c);
35    }
36  
37    @Override
38    public Deserializer<Result> getDeserializer(Class<Result> c) {
39      return new ResultDeserializer();
40    }
41  
42    @Override
43    public Serializer<Result> getSerializer(Class<Result> c) {
44      return new ResultSerializer();
45    }
46  
47    private static class ResultDeserializer implements Deserializer<Result> {
48      private InputStream in;
49  
50      @Override
51      public void close() throws IOException {
52        in.close();
53      }
54  
55      @Override
56      public Result deserialize(Result mutation) throws IOException {
57        ClientProtos.Result proto =
58            ClientProtos.Result.parseDelimitedFrom(in);
59        return ProtobufUtil.toResult(proto);
60      }
61  
62      @Override
63      public void open(InputStream in) throws IOException {
64        this.in = in;
65      }
66      
67    }
68    private static class ResultSerializer implements Serializer<Result> {
69      private OutputStream out;
70  
71      @Override
72      public void close() throws IOException {
73        out.close();
74      }
75  
76      @Override
77      public void open(OutputStream out) throws IOException {
78        this.out = out;
79      }
80  
81      @Override
82      public void serialize(Result result) throws IOException {
83        ProtobufUtil.toResult(result).writeDelimitedTo(out);
84      }
85    }
86  }