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; 021import java.nio.charset.StandardCharsets; 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. This approach would 032 * be used if a custom transformation needed to be done after reading the input data before writing 033 * 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 values by multiplying 043 * them by 3. 044 */ 045 @Override 046 public void map(LongWritable offset, Text value, Context context) throws IOException { 047 byte[] family = Bytes.toBytes("FAM"); 048 final byte[][] qualifiers = { Bytes.toBytes("A"), Bytes.toBytes("B") }; 049 050 // do some basic line parsing 051 byte[] lineBytes = value.getBytes(); 052 String[] valueTokens = new String(lineBytes, StandardCharsets.UTF_8).split("\u001b"); 053 054 // create the rowKey and Put 055 ImmutableBytesWritable rowKey = new ImmutableBytesWritable(Bytes.toBytes(valueTokens[0])); 056 Put put = new Put(rowKey.copyBytes()); 057 put.setDurability(Durability.SKIP_WAL); 058 059 // The value should look like this: VALUE1 or VALUE2. Let's multiply 060 // the integer by 3 061 for (int i = 1; i < valueTokens.length; i++) { 062 String prefix = valueTokens[i].substring(0, "VALUE".length()); 063 String suffix = valueTokens[i].substring("VALUE".length()); 064 String newValue = prefix + Integer.parseInt(suffix) * 3; 065 066 KeyValue kv = 067 new KeyValue(rowKey.copyBytes(), family, qualifiers[i - 1], Bytes.toBytes(newValue)); 068 put.add(kv); 069 } 070 071 try { 072 context.write(rowKey, put); 073 } catch (InterruptedException e) { 074 e.printStackTrace(); 075 } 076 } 077}