com.ailk.oci.ocnosql.tools.load.single.SingleColumnImporterMapper.java Source code

Java tutorial

Introduction

Here is the source code for com.ailk.oci.ocnosql.tools.load.single.SingleColumnImporterMapper.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 com.ailk.oci.ocnosql.tools.load.single;

import java.io.IOException;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
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;

import com.ailk.oci.ocnosql.common.config.TableConfiguration;
import com.ailk.oci.ocnosql.common.rowkeygenerator.GenRKStep;
import com.ailk.oci.ocnosql.common.rowkeygenerator.TableRowKeyGenerator;
import com.ailk.oci.ocnosql.common.util.CommonConstants;
import com.ailk.oci.ocnosql.tools.load.single.SingleColumnImportTsv.TextArrayWritable;
import com.ailk.oci.ocnosql.tools.load.single.SingleColumnImportTsv.TsvParser;
import com.ailk.oci.ocnosql.tools.load.single.SingleColumnImportTsv.TsvParser.BadTsvLineException;

/**
 * Write table content out to files in hdfs.
 */
public class SingleColumnImporterMapper
        extends Mapper<LongWritable, Text, ImmutableBytesWritable, TextArrayWritable> {
    static Log LOG = LogFactory.getLog(SingleColumnImporterMapper.class);
    /** Timestamp for all inserted rows */
    private long ts;

    /** Column seperator */
    private String separator;

    /** Should skip bad lines */
    private boolean skipBadLines;
    private Counter badLineCount;
    private TableRowKeyGenerator rowkeyGenerator;
    private Text text;
    private TextArrayWritable writer;
    private String tableName;
    private SingleColumnImportTsv.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) {
        LOG.info("single set up");
        doSetup(context);

        Configuration conf = context.getConfiguration();

        parser = new SingleColumnImportTsv.TsvParser(conf.get(CommonConstants.COLUMNS), separator);
        //    if (parser.getRowKeyColumnIndex() == -1) {
        //      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.
        // ???BASE64?
        separator = conf.get(CommonConstants.SEPARATOR);
        if (separator == null) {
            separator = CommonConstants.DEFAULT_SEPARATOR;
        } else {
            separator = new String(Base64.decode(separator));
        }

        skipBadLines = context.getConfiguration().getBoolean(CommonConstants.SKIPBADLINE, true);

        //?badline
        badLineCount = context.getCounter("ImportTsv", "Bad Lines");

        //    String rowkeyGennerator = context.getConfiguration().get(CommonConstants.ROWKEY_GENERATOR);
        //    //???MD5
        //    if(RowKeyGeneratorHolder.TYPE.md5.name().equalsIgnoreCase(rowkeyGennerator)){
        //       rowkeyGenerator = new MD5RowKeyGenerator();
        //    }
        tableName = conf.get(CommonConstants.TABLE_NAME);
        List<GenRKStep> genRKStepList = TableConfiguration.getInstance().getTableGenRKSteps(tableName, conf);
        rowkeyGenerator = new TableRowKeyGenerator(conf, genRKStepList);
        writer = new TextArrayWritable();
    }

    /**
     * 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 {
            TsvParser.ParsedLine parsed = parser.parse(lineBytes, value.getLength());
            //
            Text[] texts = new Text[parsed.getColumnCount()];
            int index = 0;
            for (int i = 0; i < parsed.getColumnCount(); i++) {
                //            if (i == parser.getRowKeyColumnIndex()){
                //               continue;
                //            }
                text = new Text();
                //?
                text.append(lineBytes, parsed.getColumnOffset(i), parsed.getColumnLength(i));
                texts[index] = text;
                index++;
            }
            writer.set(texts);
            /*
            //rowkey
            String oriRowKey = new String(lineBytes, parsed.getRowKeyOffset(), parsed.getRowKeyLength());
                
            // hash rowkey
            String newRowKey = oriRowKey;
            if(rowkeyGenerator != null){
               newRowKey = (String)rowkeyGenerator.generate(oriRowKey);
            }
            */
            String newRowKey = rowkeyGenerator.generateByGenRKStep(value.toString(), false);//???rowkey
            //LOG.info("single column newRowKey = " + newRowKey);
            context.write(new ImmutableBytesWritable(newRowKey.getBytes()), writer);
        } catch (BadTsvLineException badLine) {
            if (skipBadLines) {
                LOG.error("Bad line at offset: " + offset.get() + ":\n" + badLine.getMessage());
                badLineCount.increment(1);
                return;
            } else {
                throw new IOException(badLine);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}