1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
31
32
33
34 public class TsvImporterCustomTestMapper extends TsvImporterMapper {
35
36 @Override
37 protected void setup(Context context) {
38 doSetup(context);
39 }
40
41
42
43
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
52 byte[] lineBytes = value.getBytes();
53 String[] valueTokens = new String(lineBytes, "UTF-8").split("\u001b");
54
55
56 ImmutableBytesWritable rowKey =
57 new ImmutableBytesWritable(Bytes.toBytes(valueTokens[0]));
58 Put put = new Put(rowKey.copyBytes());
59 put.setWriteToWAL(false);
60
61
62
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 }