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