org.apache.hadoop.hbase.mapreduce.CompositeKeyTsvImporterMapper.java Source code

Java tutorial

Introduction

Here is the source code for org.apache.hadoop.hbase.mapreduce.CompositeKeyTsvImporterMapper.java

Source

/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.hadoop.hbase.mapreduce;

import java.io.IOException;
import java.util.Collection;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.CompositeKeyImportTsv.TsvParser.BadTsvLineException;
import org.apache.hadoop.hbase.util.Base64;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Counter;
import org.apache.hadoop.mapreduce.Mapper;

/**
 * Write table content out to files in hdfs.
 */
public class CompositeKeyTsvImporterMapper extends Mapper<LongWritable, Text, ImmutableBytesWritable, Put> {

    protected static final Log LOG = LogFactory.getLog(CompositeKeyTsvImporterMapper.class);

    /** Timestamp for all inserted rows */
    private long ts;

    /** Column separator */
    private String separator;

    /** Should skip bad lines */
    private boolean skipBadLines;
    private Counter badLineCount;

    private CompositeKeyImportTsv.TsvParser parser;

    public long getTs() {
        return ts;
    }

    public boolean getSkipBadLines() {
        return skipBadLines;
    }

    public Counter getBadLineCount() {
        return badLineCount;
    }

    public void incrementBadLineCount(int count) {
        this.badLineCount.increment(count);
    }

    /**
     * Handles initializing this class with objects specific to it (i.e., the parser).
     * Common initialization that might be leveraged by a subsclass is done in
     * <code>doSetup</code>. Hence a subclass may choose to override this method
     * and call <code>doSetup</code> as well before handling it's own custom params.
     *
     * @param context
     */
    @Override
    protected void setup(Context context) {
        doSetup(context);

        Configuration conf = context.getConfiguration();

        try {
            parser = new CompositeKeyImportTsv.TsvParser(conf.get(CompositeKeyImportTsv.COLUMNS_CONF_KEY),
                    separator);
        } catch (BadTsvLineException e) {
            throw new RuntimeException("Invalid Row Key spec.");
        }
        if (parser.getRowKeyColumnsIndex().size() == 0) {
            throw new RuntimeException("No row key column specified");
        }
    }

    /**
     * Handles common parameter initialization that a subclass might want to leverage.
     * @param context
     */
    protected void doSetup(Context context) {
        Configuration conf = context.getConfiguration();

        // If a custom separator has been used,
        // decode it back from Base64 encoding.
        separator = conf.get(CompositeKeyImportTsv.SEPARATOR_CONF_KEY);
        if (separator == null) {
            separator = CompositeKeyImportTsv.DEFAULT_SEPARATOR;
        } else {
            separator = new String(Base64.decode(separator));
        }

        // Should never get 0 as we are setting this to a valid value in job
        // configuration.
        ts = conf.getLong(CompositeKeyImportTsv.TIMESTAMP_CONF_KEY, 0);

        skipBadLines = context.getConfiguration().getBoolean(CompositeKeyImportTsv.SKIP_LINES_CONF_KEY, true);
        badLineCount = context.getCounter("ImportTsv", "Bad Lines");
    }

    /**
     * Convert a line of TSV text into an HBase table row.
     */
    @Override
    public void map(LongWritable offset, Text value, Context context) throws IOException {
        byte[] lineBytes = value.getBytes();

        try {
            CompositeKeyImportTsv.TsvParser.ParsedLine parsed = parser.parse(lineBytes, value.getLength());
            ImmutableBytesWritable rowKey = parsed.getRowKey();
            // Retrieve timestamp if exists
            ts = parsed.getTimestamp(ts);
            Collection<Integer> rowkeyValues = parser.getRowKeyColumnsIndex().values();
            Put put = new Put(rowKey.copyBytes());
            for (int i = 0; i < parsed.getColumnCount(); i++) {
                if (rowkeyValues.contains(i) || i == parser.getTimestampKeyColumnIndex()) {
                    continue;
                }
                KeyValue kv = new KeyValue(rowKey.copyBytes(), 0, rowKey.copyBytes().length, parser.getFamily(i), 0,
                        parser.getFamily(i).length, parser.getQualifier(i), 0, parser.getQualifier(i).length, ts,
                        KeyValue.Type.Put, lineBytes, parsed.getColumnOffset(i), parsed.getColumnLength(i));
                put.add(kv);
            }
            context.write(rowKey, put);
        } catch (CompositeKeyImportTsv.TsvParser.BadTsvLineException badLine) {
            if (skipBadLines) {
                LOG.error("Bad line at offset: " + offset.get() + ":\n" + badLine.getMessage());
                //badLine.printStackTrace();
                incrementBadLineCount(1);
                return;
            } else {
                throw new IOException(badLine);
            }
        } catch (IllegalArgumentException e) {
            if (skipBadLines) {
                LOG.error("Bad line at offset: " + offset.get() + ":\n" + e.getMessage());
                incrementBadLineCount(1);
                return;
            } else {
                throw new IOException(e);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}