Github user prashantkommireddi commented on a diff in the pull request:

    https://github.com/apache/incubator-phoenix/pull/22#discussion_r10812397
  
    --- Diff: 
phoenix-pig/src/main/java/org/apache/phoenix/pig/PhoenixHBaseLoader.java ---
    @@ -0,0 +1,190 @@
    +/*
    + * Copyright 2010 The Apache Software Foundation
    + *
    + * 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 maynot 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 applicablelaw 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.phoenix.pig;
    +
    +import java.io.IOException;
    +import java.sql.Connection;
    +import java.sql.SQLException;
    +import java.sql.Statement;
    +
    +import org.apache.commons.lang.StringUtils;
    +import org.apache.commons.logging.Log;
    +import org.apache.commons.logging.LogFactory;
    +import org.apache.hadoop.conf.Configuration;
    +import org.apache.hadoop.fs.Path;
    +import org.apache.hadoop.io.NullWritable;
    +import org.apache.hadoop.mapreduce.InputFormat;
    +import org.apache.hadoop.mapreduce.Job;
    +import org.apache.hadoop.mapreduce.RecordReader;
    +import org.apache.phoenix.compile.QueryPlan;
    +import org.apache.phoenix.jdbc.PhoenixStatement;
    +import org.apache.phoenix.pig.hadoop.PhoenixInputFormat;
    +import org.apache.phoenix.pig.hadoop.PhoenixRecord;
    +import org.apache.pig.LoadFunc;
    +import org.apache.pig.PigException;
    +import org.apache.pig.backend.executionengine.ExecException;
    +import 
org.apache.pig.backend.hadoop.executionengine.mapReduceLayer.PigSplit;
    +import org.apache.pig.data.Tuple;
    +
    +import com.google.common.base.Preconditions;
    +import com.google.common.base.Throwables;
    +
    +/**
    + * LoadFunc to load data from HBase using Phoenix .
    + * 
    + * Example usage: 
    + * a)   A = load 'hbase://table/HIRES'  using
    + * org.apache.phoenix.pig.PhoenixHBaseLoader('localhost');
    + *               
    + *       The above loads the data from a table 'HIRES'
    + * 
    + * b)   B = load 'hbase://query/SELECT fname,lname FROM HIRES' using
    + *             org.apache.phoenix.pig.PhoenixHBaseLoader('localhost');
    + *       
    + *        The above loads fname and lname columns from 'HIRES' table.
    + * 
    + */
    +public final class PhoenixHBaseLoader extends LoadFunc{
    +
    +    private static final Log LOG = 
LogFactory.getLog(PhoenixHBaseLoader.class);
    +    private static final String PHOENIX_TABLE_NAME_TEMPLATE = 
"hbase://table/";
    +    private static final String PHOENIX_QUERY_TEMPLATE      = 
"hbase://query/";
    +    
    +    private PhoenixPigConfiguration config;
    +    private String tableName;
    +    private String selectQuery;
    +    private String zkQuorum ;
    +    private PhoenixInputFormat inputFormat;
    +    private RecordReader<NullWritable, PhoenixRecord> reader;
    + 
    +    
    +    /**
    +     * @param zkQuorum
    +     */
    +    public PhoenixHBaseLoader(String zkQuorum) {
    +        super();
    +        this.zkQuorum = zkQuorum;
    +    }
    +    
    +    @Override
    +    public void setLocation(String location, Job job) throws IOException {
    +        
    +        final Configuration configuration = job.getConfiguration();
    +        if (location.startsWith(PHOENIX_TABLE_NAME_TEMPLATE)) {
    +            tableName = 
location.substring(PHOENIX_TABLE_NAME_TEMPLATE.length());
    +            configuration.set(PhoenixPigConfiguration.TABLE_NAME, 
tableName);
    +        } else if (location.startsWith(PHOENIX_QUERY_TEMPLATE)) {
    +            selectQuery = 
location.substring(PHOENIX_QUERY_TEMPLATE.length());
    +            configuration.set(PhoenixPigConfiguration.SELECT_STATEMENT, 
selectQuery);
    +        }
    +         if(StringUtils.isEmpty(this.tableName) && 
StringUtils.isEmpty(this.selectQuery)) {
    +            printUsage(location);
    +        }
    +        configuration.set(PhoenixPigConfiguration.SERVER_NAME, 
this.zkQuorum);
    +        this.config = new PhoenixPigConfiguration(configuration);
    +        // when sql query is given.
    +        if(StringUtils.isEmpty(this.tableName)) {
    +            try {
    +                this.tableName = 
getTableNameFromSelectQuery(config.getConnection(false), this.selectQuery);
    +                
config.getConfiguration().set(PhoenixPigConfiguration.TABLE_NAME, 
this.tableName);
    +            } catch (SQLException e) {
    +                
LOG.error(org.apache.hadoop.util.StringUtils.stringifyException(e));
    +                Throwables.propagate(e);
    +            }
    +        }
    +    }
    +
    +    @Override
    +    public String relativeToAbsolutePath(String location, Path curDir) 
throws IOException {
    +        return location;
    +    }
    +
    +    @Override
    +    public InputFormat getInputFormat() throws IOException {
    +        if(inputFormat == null) {
    +            inputFormat = new PhoenixInputFormat();
    +        }
    +        return inputFormat;
    +    }
    +
    +    @SuppressWarnings("unchecked")
    +    @Override
    +    public void prepareToRead(RecordReader reader, PigSplit split) throws 
IOException {
    +        this.reader = reader;
    +    }
    +
    +    @Override
    +    public Tuple getNext() throws IOException {
    +        try {
    +            if(!reader.nextKeyValue()) {
    +               return null; 
    +            }
    +            final PhoenixRecord value = reader.getCurrentValue();
    +            if(value == null) {
    +                return null;
    +            }
    +            final Tuple tuple = 
TypeUtil.transformToTuple(value,value.getProjectedColumns());
    +            return tuple;
    +       } catch (InterruptedException e) {
    +           int errCode = 6018;
    +           final String errMsg = "Error while reading input";
    +           throw new ExecException(errMsg, 
errCode,PigException.REMOTE_ENVIRONMENT, e);
    --- End diff --
    
    Indentation


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to