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.util.Arrays;
022import org.apache.hadoop.hbase.KeyValue;
023import org.apache.hadoop.hbase.client.Put;
024import org.apache.hadoop.hbase.mapreduce.ImportTsv.TsvParser.BadTsvLineException;
025import org.apache.hadoop.hbase.mapreduce.ImportTsv.TsvParser.ParsedLine;
026import org.apache.hadoop.hbase.util.Bytes;
027
028/**
029 * Just shows a simple example of how the attributes can be extracted and added to the puts
030 */
031public class TsvImporterCustomTestMapperForOprAttr extends TsvImporterMapper {
032  @Override
033  protected void populatePut(byte[] lineBytes, ParsedLine parsed, Put put, int i)
034    throws BadTsvLineException, IOException {
035    KeyValue kv;
036    kv = new KeyValue(lineBytes, parsed.getRowKeyOffset(), parsed.getRowKeyLength(),
037      parser.getFamily(i), 0, parser.getFamily(i).length, parser.getQualifier(i), 0,
038      parser.getQualifier(i).length, ts, KeyValue.Type.Put, lineBytes, parsed.getColumnOffset(i),
039      parsed.getColumnLength(i));
040    if (parsed.getIndividualAttributes() != null) {
041      String[] attributes = parsed.getIndividualAttributes();
042      for (String attr : attributes) {
043        String[] split = attr.split(ImportTsv.DEFAULT_ATTRIBUTES_SEPERATOR);
044        if (split == null || split.length <= 1) {
045          throw new BadTsvLineException(msg(attributes));
046        } else {
047          if (split[0].length() <= 0 || split[1].length() <= 0) {
048            throw new BadTsvLineException(msg(attributes));
049          }
050          put.setAttribute(split[0], Bytes.toBytes(split[1]));
051        }
052      }
053    }
054    put.add(kv);
055  }
056
057  private String msg(Object[] attributes) {
058    return "Invalid attributes separator specified: " + Arrays.toString(attributes);
059  }
060}