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 org.apache.hadoop.io.LongWritable;
21  import org.apache.hadoop.io.Text;
22  import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
23  import org.apache.hadoop.hbase.client.Put;
24  import org.apache.hadoop.hbase.client.Durability;
25  import org.apache.hadoop.hbase.util.Bytes;
26  import org.apache.hadoop.hbase.KeyValue;
27  
28  import java.io.IOException;
29  
30  /**
31   * Dummy mapper used for unit tests to verify that the mapper can be injected.
32   * This approach would be used if a custom transformation needed to be done after
33   * reading the input data before writing it to HFiles.
34   */
35  public class TsvImporterCustomTestMapper extends TsvImporterMapper {
36  
37    @Override
38    protected void setup(Context context) {
39      doSetup(context);
40    }
41  
42    /**
43     * Convert a line of TSV text into an HBase table row after transforming the
44     * values by multiplying them by 3.
45     */
46    @Override
47    public void map(LongWritable offset, Text value, Context context)
48          throws IOException {
49      byte[] family = Bytes.toBytes("FAM");
50      final byte[][] qualifiers = { Bytes.toBytes("A"), Bytes.toBytes("B") };
51  
52      // do some basic line parsing
53      byte[] lineBytes = value.getBytes();
54      String[] valueTokens = new String(lineBytes, "UTF-8").split("\u001b");
55  
56      // create the rowKey and Put
57      ImmutableBytesWritable rowKey =
58        new ImmutableBytesWritable(Bytes.toBytes(valueTokens[0]));
59      Put put = new Put(rowKey.copyBytes());
60      put.setDurability(Durability.SKIP_WAL);
61  
62      //The value should look like this: VALUE1 or VALUE2. Let's multiply
63      //the integer by 3
64      for(int i = 1; i < valueTokens.length; i++) {
65        String prefix = valueTokens[i].substring(0, "VALUE".length());
66        String suffix = valueTokens[i].substring("VALUE".length());
67        String newValue = prefix + Integer.parseInt(suffix) * 3;
68  
69        KeyValue kv = new KeyValue(rowKey.copyBytes(), family,
70            qualifiers[i-1], Bytes.toBytes(newValue));
71        put.add(kv);
72      }
73  
74      try {
75        context.write(rowKey, put);
76      } catch (InterruptedException e) {
77        e.printStackTrace();
78      }
79    }
80  }