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 java.io.IOException;
21  import java.util.Arrays;
22  
23  import org.apache.hadoop.hbase.KeyValue;
24  import org.apache.hadoop.hbase.client.Put;
25  import org.apache.hadoop.hbase.mapreduce.ImportTsv.TsvParser.BadTsvLineException;
26  import org.apache.hadoop.hbase.mapreduce.ImportTsv.TsvParser.ParsedLine;
27  import org.apache.hadoop.hbase.util.Bytes;
28  
29  /**
30   * 
31   * Just shows a simple example of how the attributes can be extracted and added
32   * to the puts
33   */
34  public class TsvImporterCustomTestMapperForOprAttr extends TsvImporterMapper {
35    @Override
36    protected void populatePut(byte[] lineBytes, ParsedLine parsed, Put put, int i)
37        throws BadTsvLineException, IOException {
38      KeyValue kv;
39      kv = new KeyValue(lineBytes, parsed.getRowKeyOffset(), parsed.getRowKeyLength(),
40          parser.getFamily(i), 0, parser.getFamily(i).length, parser.getQualifier(i), 0,
41          parser.getQualifier(i).length, ts, KeyValue.Type.Put, lineBytes, parsed.getColumnOffset(i),
42          parsed.getColumnLength(i));
43      if (parsed.getIndividualAttributes() != null) {
44        String[] attributes = parsed.getIndividualAttributes();
45        for (String attr : attributes) {
46          String[] split = attr.split(ImportTsv.DEFAULT_ATTRIBUTES_SEPERATOR);
47          if (split == null || split.length <= 1) {
48            throw new BadTsvLineException(msg(attributes));
49          } else {
50            if (split[0].length() <= 0 || split[1].length() <= 0) {
51              throw new BadTsvLineException(msg(attributes));
52            }
53            put.setAttribute(split[0], Bytes.toBytes(split[1]));
54          }
55        }
56      }
57      put.add(kv);
58    }
59  
60    private String msg(Object[] attributes) {
61      return "Invalid attributes separator specified: " + Arrays.toString(attributes);
62    }
63  }