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);
}
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;
}
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);
}
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);
}
}
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;
}
Aggregations