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