Search in sources :

Example 1 with PigException

use of org.apache.pig.PigException in project hive by apache.

the class AbstractHCatStorerTest method testNoAlias.

@Test
public void testNoAlias() throws IOException, CommandNeedRetryException {
    driver.run("drop table junit_parted");
    String createTable = "create table junit_parted(a int, b string) partitioned by (ds string) stored as " + storageFormat;
    int retCode = driver.run(createTable).getResponseCode();
    if (retCode != 0) {
        throw new IOException("Failed to create table.");
    }
    PigServer server = new PigServer(ExecType.LOCAL);
    boolean errCaught = false;
    try {
        server.setBatchOn();
        server.registerQuery("A = load '" + INPUT_FILE_NAME + "' as (a:int, b:chararray);");
        server.registerQuery("B = foreach A generate a+10, b;");
        server.registerQuery("store B into 'junit_parted' using " + HCatStorer.class.getName() + "('ds=20100101');");
        server.executeBatch();
    } catch (PigException fe) {
        PigException pe = LogUtils.getPigException(fe);
        assertTrue(pe instanceof FrontendException);
        assertEquals(PigHCatUtil.PIG_EXCEPTION_CODE, pe.getErrorCode());
        assertTrue(pe.getMessage().contains("Column name for a field is not specified. Please provide the full schema as an argument to HCatStorer."));
        errCaught = true;
    }
    assertTrue(errCaught);
    errCaught = false;
    try {
        server.setBatchOn();
        server.registerQuery("A = load '" + INPUT_FILE_NAME + "' as (a:int, B:chararray);");
        server.registerQuery("B = foreach A generate a, B;");
        server.registerQuery("store B into 'junit_parted' using " + HCatStorer.class.getName() + "('ds=20100101');");
        server.executeBatch();
    } catch (PigException fe) {
        PigException pe = LogUtils.getPigException(fe);
        assertTrue(pe instanceof FrontendException);
        assertEquals(PigHCatUtil.PIG_EXCEPTION_CODE, pe.getErrorCode());
        assertTrue(pe.getMessage().contains("Column names should all be in lowercase. Invalid name found: B"));
        errCaught = true;
    }
    driver.run("drop table junit_parted");
    assertTrue(errCaught);
}
Also used : PigServer(org.apache.pig.PigServer) IOException(java.io.IOException) PigException(org.apache.pig.PigException) FrontendException(org.apache.pig.impl.logicalLayer.FrontendException) Test(org.junit.Test) HCatBaseTest(org.apache.hive.hcatalog.mapreduce.HCatBaseTest)

Example 2 with PigException

use of org.apache.pig.PigException in project phoenix by apache.

the class TypeUtil method transformToTuple.

/**
     * Transforms the PhoenixRecord to Pig {@link Tuple}.
     * 
     * @param record
     * @param projectedColumns
     * @return
     * @throws IOException
     */
public static Tuple transformToTuple(final PhoenixRecordWritable record, final ResourceFieldSchema[] projectedColumns) throws IOException {
    Map<String, Object> columnValues = record.getResultMap();
    if (columnValues == null || columnValues.size() == 0 || projectedColumns == null || projectedColumns.length != columnValues.size()) {
        return null;
    }
    int numColumns = columnValues.size();
    Tuple tuple = TUPLE_FACTORY.newTuple(numColumns);
    try {
        int i = 0;
        for (Map.Entry<String, Object> entry : columnValues.entrySet()) {
            final ResourceFieldSchema fieldSchema = projectedColumns[i];
            Object object = entry.getValue();
            if (object == null) {
                tuple.set(i++, null);
                continue;
            }
            switch(fieldSchema.getType()) {
                case DataType.BYTEARRAY:
                    byte[] bytes = PDataType.fromTypeId(PBinary.INSTANCE.getSqlType()).toBytes(object);
                    tuple.set(i, new DataByteArray(bytes, 0, bytes.length));
                    break;
                case DataType.CHARARRAY:
                    tuple.set(i, DataType.toString(object));
                    break;
                case DataType.DOUBLE:
                    tuple.set(i, DataType.toDouble(object));
                    break;
                case DataType.FLOAT:
                    tuple.set(i, DataType.toFloat(object));
                    break;
                case DataType.INTEGER:
                    tuple.set(i, DataType.toInteger(object));
                    break;
                case DataType.LONG:
                    tuple.set(i, DataType.toLong(object));
                    break;
                case DataType.BOOLEAN:
                    tuple.set(i, DataType.toBoolean(object));
                    break;
                case DataType.DATETIME:
                    if (object instanceof java.sql.Timestamp)
                        tuple.set(i, new DateTime(((java.sql.Timestamp) object).getTime()));
                    else
                        tuple.set(i, new DateTime(object));
                    break;
                case DataType.BIGDECIMAL:
                    tuple.set(i, DataType.toBigDecimal(object));
                    break;
                case DataType.BIGINTEGER:
                    tuple.set(i, DataType.toBigInteger(object));
                    break;
                case DataType.TUPLE:
                    {
                        PhoenixArray array = (PhoenixArray) object;
                        Tuple t = TUPLE_FACTORY.newTuple(array.getDimensions());
                        ;
                        for (int j = 0; j < array.getDimensions(); j++) {
                            t.set(j, array.getElement(j));
                        }
                        tuple.set(i, t);
                        break;
                    }
                default:
                    throw new RuntimeException(String.format(" Not supported [%s] pig type", fieldSchema));
            }
            i++;
        }
    } catch (Exception ex) {
        final String errorMsg = String.format(" Error transforming PhoenixRecord to Tuple [%s] ", ex.getMessage());
        LOG.error(errorMsg);
        throw new PigException(errorMsg);
    }
    return tuple;
}
Also used : PhoenixArray(org.apache.phoenix.schema.types.PhoenixArray) PUnsignedTimestamp(org.apache.phoenix.schema.types.PUnsignedTimestamp) Timestamp(java.sql.Timestamp) PTimestamp(org.apache.phoenix.schema.types.PTimestamp) PUnsignedSmallint(org.apache.phoenix.schema.types.PUnsignedSmallint) PUnsignedTinyint(org.apache.phoenix.schema.types.PUnsignedTinyint) PTinyint(org.apache.phoenix.schema.types.PTinyint) PSmallint(org.apache.phoenix.schema.types.PSmallint) DateTime(org.joda.time.DateTime) PigException(org.apache.pig.PigException) SQLException(java.sql.SQLException) IOException(java.io.IOException) ResourceFieldSchema(org.apache.pig.ResourceSchema.ResourceFieldSchema) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) DataByteArray(org.apache.pig.data.DataByteArray) Tuple(org.apache.pig.data.Tuple) PigException(org.apache.pig.PigException)

Example 3 with PigException

use of org.apache.pig.PigException in project hive by apache.

the class HCatLoader method getSchema.

@Override
public ResourceSchema getSchema(String location, Job job) throws IOException {
    HCatContext.INSTANCE.setConf(job.getConfiguration()).getConf().get().setBoolean(HCatConstants.HCAT_DATA_TINY_SMALL_INT_PROMOTION, true);
    Table table = phutil.getTable(location, hcatServerUri != null ? hcatServerUri : PigHCatUtil.getHCatServerUri(job), PigHCatUtil.getHCatServerPrincipal(job), // (hive.metastore.uris = "").
    job);
    HCatSchema hcatTableSchema = HCatUtil.getTableSchemaWithPtnCols(table);
    try {
        PigHCatUtil.validateHCatTableSchemaFollowsPigRules(hcatTableSchema);
    } catch (IOException e) {
        throw new PigException("Table schema incompatible for reading through HCatLoader :" + e.getMessage() + ";[Table schema was " + hcatTableSchema.toString() + "]", PigHCatUtil.PIG_EXCEPTION_CODE, e);
    }
    storeInUDFContext(signature, HCatConstants.HCAT_TABLE_SCHEMA, hcatTableSchema);
    outputSchema = hcatTableSchema;
    return PigHCatUtil.getResourceSchema(hcatTableSchema);
}
Also used : Table(org.apache.hadoop.hive.ql.metadata.Table) HCatSchema(org.apache.hive.hcatalog.data.schema.HCatSchema) IOException(java.io.IOException) PigException(org.apache.pig.PigException)

Example 4 with PigException

use of org.apache.pig.PigException in project hive by apache.

the class HCatStorer method setStoreLocation.

/**
   * @param location databaseName.tableName
   */
@Override
public void setStoreLocation(String location, Job job) throws IOException {
    Configuration config = job.getConfiguration();
    config.set(INNER_SIGNATURE, INNER_SIGNATURE_PREFIX + "_" + sign);
    Properties udfProps = UDFContext.getUDFContext().getUDFProperties(this.getClass(), new String[] { sign });
    String[] userStr = location.split("\\.");
    if (udfProps.containsKey(HCatConstants.HCAT_PIG_STORER_LOCATION_SET)) {
        for (Enumeration<Object> emr = udfProps.keys(); emr.hasMoreElements(); ) {
            PigHCatUtil.getConfigFromUDFProperties(udfProps, config, emr.nextElement().toString());
        }
        Credentials crd = jobCredentials.get(INNER_SIGNATURE_PREFIX + "_" + sign);
        if (crd != null) {
            job.getCredentials().addAll(crd);
        }
    } else {
        Job clone = new Job(job.getConfiguration());
        OutputJobInfo outputJobInfo;
        if (userStr.length == 2) {
            outputJobInfo = OutputJobInfo.create(userStr[0], userStr[1], partitions);
        } else if (userStr.length == 1) {
            outputJobInfo = OutputJobInfo.create(null, userStr[0], partitions);
        } else {
            throw new FrontendException("location " + location + " is invalid. It must be of the form [db.]table", PigHCatUtil.PIG_EXCEPTION_CODE);
        }
        Schema schema = (Schema) ObjectSerializer.deserialize(udfProps.getProperty(PIG_SCHEMA));
        if (schema != null) {
            pigSchema = schema;
        }
        if (pigSchema == null) {
            throw new FrontendException("Schema for data cannot be determined.", PigHCatUtil.PIG_EXCEPTION_CODE);
        }
        String externalLocation = (String) udfProps.getProperty(HCatConstants.HCAT_PIG_STORER_EXTERNAL_LOCATION);
        if (externalLocation != null) {
            outputJobInfo.setLocation(externalLocation);
        }
        try {
            HCatOutputFormat.setOutput(job, outputJobInfo);
        } catch (HCatException he) {
            // information passed to HCatOutputFormat was not right
            throw new PigException(he.getMessage(), PigHCatUtil.PIG_EXCEPTION_CODE, he);
        }
        HCatSchema hcatTblSchema = HCatOutputFormat.getTableSchema(job.getConfiguration());
        try {
            doSchemaValidations(pigSchema, hcatTblSchema);
        } catch (HCatException he) {
            throw new FrontendException(he.getMessage(), PigHCatUtil.PIG_EXCEPTION_CODE, he);
        }
        computedSchema = convertPigSchemaToHCatSchema(pigSchema, hcatTblSchema);
        HCatOutputFormat.setSchema(job, computedSchema);
        udfProps.setProperty(COMPUTED_OUTPUT_SCHEMA, ObjectSerializer.serialize(computedSchema));
        // methods need not be called many times.
        for (Entry<String, String> keyValue : job.getConfiguration()) {
            String oldValue = clone.getConfiguration().getRaw(keyValue.getKey());
            if ((oldValue == null) || (keyValue.getValue().equals(oldValue) == false)) {
                udfProps.put(keyValue.getKey(), keyValue.getValue());
            }
        }
        //Store credentials in a private hash map and not the udf context to
        // make sure they are not public.
        jobCredentials.put(INNER_SIGNATURE_PREFIX + "_" + sign, job.getCredentials());
        udfProps.put(HCatConstants.HCAT_PIG_STORER_LOCATION_SET, true);
    }
}
Also used : Configuration(org.apache.hadoop.conf.Configuration) Schema(org.apache.pig.impl.logicalLayer.schema.Schema) ResourceSchema(org.apache.pig.ResourceSchema) HCatSchema(org.apache.hive.hcatalog.data.schema.HCatSchema) HCatException(org.apache.hive.hcatalog.common.HCatException) Properties(java.util.Properties) HCatSchema(org.apache.hive.hcatalog.data.schema.HCatSchema) OutputJobInfo(org.apache.hive.hcatalog.mapreduce.OutputJobInfo) Job(org.apache.hadoop.mapreduce.Job) Credentials(org.apache.hadoop.security.Credentials) FrontendException(org.apache.pig.impl.logicalLayer.FrontendException) PigException(org.apache.pig.PigException)

Example 5 with PigException

use of org.apache.pig.PigException in project hive by apache.

the class PigHCatUtil method getTable.

/*
  * The job argument is passed so that configuration overrides can be used to initialize
  * the metastore configuration in the special case of an embedded metastore
  * (hive.metastore.uris = "").
  */
public Table getTable(String location, String hcatServerUri, String hcatServerPrincipal, Job job) throws IOException {
    Pair<String, String> loc_server = new Pair<String, String>(location, hcatServerUri);
    Table hcatTable = hcatTableCache.get(loc_server);
    if (hcatTable != null) {
        return hcatTable;
    }
    Pair<String, String> dbTablePair = PigHCatUtil.getDBTableNames(location);
    String dbName = dbTablePair.first;
    String tableName = dbTablePair.second;
    Table table = null;
    IMetaStoreClient client = null;
    try {
        client = getHiveMetaClient(hcatServerUri, hcatServerPrincipal, PigHCatUtil.class, job);
        table = HCatUtil.getTable(client, dbName, tableName);
    } catch (NoSuchObjectException nsoe) {
        // prettier error messages to frontend
        throw new PigException("Table not found : " + nsoe.getMessage(), PIG_EXCEPTION_CODE);
    } catch (Exception e) {
        throw new IOException(e);
    } finally {
        HCatUtil.closeHiveClientQuietly(client);
    }
    hcatTableCache.put(loc_server, table);
    return table;
}
Also used : Table(org.apache.hadoop.hive.ql.metadata.Table) NoSuchObjectException(org.apache.hadoop.hive.metastore.api.NoSuchObjectException) IOException(java.io.IOException) IMetaStoreClient(org.apache.hadoop.hive.metastore.IMetaStoreClient) PigException(org.apache.pig.PigException) HCatException(org.apache.hive.hcatalog.common.HCatException) PigException(org.apache.pig.PigException) IOException(java.io.IOException) NoSuchObjectException(org.apache.hadoop.hive.metastore.api.NoSuchObjectException) Pair(org.apache.hive.hcatalog.data.Pair)

Aggregations

PigException (org.apache.pig.PigException)5 IOException (java.io.IOException)4 Table (org.apache.hadoop.hive.ql.metadata.Table)2 HCatException (org.apache.hive.hcatalog.common.HCatException)2 HCatSchema (org.apache.hive.hcatalog.data.schema.HCatSchema)2 FrontendException (org.apache.pig.impl.logicalLayer.FrontendException)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 SQLException (java.sql.SQLException)1 Timestamp (java.sql.Timestamp)1 Map (java.util.Map)1 Properties (java.util.Properties)1 Configuration (org.apache.hadoop.conf.Configuration)1 IMetaStoreClient (org.apache.hadoop.hive.metastore.IMetaStoreClient)1 NoSuchObjectException (org.apache.hadoop.hive.metastore.api.NoSuchObjectException)1 Job (org.apache.hadoop.mapreduce.Job)1 Credentials (org.apache.hadoop.security.Credentials)1 Pair (org.apache.hive.hcatalog.data.Pair)1 HCatBaseTest (org.apache.hive.hcatalog.mapreduce.HCatBaseTest)1 OutputJobInfo (org.apache.hive.hcatalog.mapreduce.OutputJobInfo)1 PSmallint (org.apache.phoenix.schema.types.PSmallint)1