001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.mapreduce;
019
020import java.io.IOException;
021
022import org.apache.hadoop.hbase.KeyValue;
023import org.apache.hadoop.hbase.client.Durability;
024import org.apache.hadoop.hbase.client.Put;
025import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
026import org.apache.hadoop.hbase.util.Bytes;
027import org.apache.hadoop.io.LongWritable;
028import org.apache.hadoop.io.Text;
029
030/**
031 * Dummy mapper used for unit tests to verify that the mapper can be injected.
032 * This approach would be used if a custom transformation needed to be done after
033 * reading the input data before writing it to HFiles.
034 */
035public class TsvImporterCustomTestMapper extends TsvImporterMapper {
036  @Override
037  protected void setup(Context context) {
038    doSetup(context);
039  }
040
041  /**
042   * Convert a line of TSV text into an HBase table row after transforming the
043   * values by multiplying them by 3.
044   */
045  @Override
046  public void map(LongWritable offset, Text value, Context context)
047        throws IOException {
048    byte[] family = Bytes.toBytes("FAM");
049    final byte[][] qualifiers = { Bytes.toBytes("A"), Bytes.toBytes("B") };
050
051    // do some basic line parsing
052    byte[] lineBytes = value.getBytes();
053    String[] valueTokens = new String(lineBytes, "UTF-8").split("\u001b");
054
055    // create the rowKey and Put
056    ImmutableBytesWritable rowKey =
057      new ImmutableBytesWritable(Bytes.toBytes(valueTokens[0]));
058    Put put = new Put(rowKey.copyBytes());
059    put.setDurability(Durability.SKIP_WAL);
060
061    //The value should look like this: VALUE1 or VALUE2. Let's multiply
062    //the integer by 3
063    for(int i = 1; i < valueTokens.length; i++) {
064      String prefix = valueTokens[i].substring(0, "VALUE".length());
065      String suffix = valueTokens[i].substring("VALUE".length());
066      String newValue = prefix + Integer.parseInt(suffix) * 3;
067
068      KeyValue kv = new KeyValue(rowKey.copyBytes(), family,
069          qualifiers[i-1], Bytes.toBytes(newValue));
070      put.add(kv);
071    }
072
073    try {
074      context.write(rowKey, put);
075    } catch (InterruptedException e) {
076      e.printStackTrace();
077    }
078  }
079}