use of org.apache.pig.impl.logicalLayer.FrontendException in project sketches-pig by DataSketches.
the class ReservoirSampling method outputSchema.
@Override
public Schema outputSchema(final Schema input) {
if (input != null && input.size() > 0) {
try {
Schema source = input;
// if we have a bag, grab one level down to get a tuple
if (source.size() == 1 && source.getField(0).type == DataType.BAG) {
source = source.getField(0).schema;
}
final Schema recordSchema = new Schema();
recordSchema.add(new Schema.FieldSchema(N_ALIAS, DataType.LONG));
recordSchema.add(new Schema.FieldSchema(K_ALIAS, DataType.INTEGER));
// this should add a bag to the output
recordSchema.add(new Schema.FieldSchema(SAMPLES_ALIAS, source, DataType.BAG));
return new Schema(new Schema.FieldSchema(getSchemaName(this.getClass().getName().toLowerCase(), source), recordSchema, DataType.TUPLE));
} catch (final FrontendException e) {
throw new RuntimeException(e);
}
}
return null;
}
use of org.apache.pig.impl.logicalLayer.FrontendException in project parquet-mr by apache.
the class TupleReadSupport method getPigSchemaFromMultipleFiles.
/**
* @param fileSchema the parquet schema from the file
* @param keyValueMetaData the extra meta data from the files
* @return the pig schema according to the file
*/
static Schema getPigSchemaFromMultipleFiles(MessageType fileSchema, Map<String, Set<String>> keyValueMetaData) {
Set<String> pigSchemas = PigMetaData.getPigSchemas(keyValueMetaData);
if (pigSchemas == null) {
return pigSchemaConverter.convert(fileSchema);
}
Schema mergedPigSchema = null;
for (String pigSchemaString : pigSchemas) {
try {
mergedPigSchema = union(mergedPigSchema, parsePigSchema(pigSchemaString));
} catch (FrontendException e) {
throw new ParquetDecodingException("can not merge " + pigSchemaString + " into " + mergedPigSchema, e);
}
}
return mergedPigSchema;
}
use of org.apache.pig.impl.logicalLayer.FrontendException in project parquet-mr by apache.
the class PigSchemaConverter method convertMap.
/**
* @param alias
* @param fieldSchema
* @return an optional group containing one repeated group field (key, value)
* @throws FrontendException
*/
private GroupType convertMap(String alias, FieldSchema fieldSchema) {
Schema innerSchema = fieldSchema.schema;
if (innerSchema == null || innerSchema.size() != 1) {
throw new SchemaConversionException("Invalid map Schema, schema should contain exactly one field: " + fieldSchema);
}
FieldSchema innerField = null;
try {
innerField = innerSchema.getField(0);
} catch (FrontendException fe) {
throw new SchemaConversionException("Invalid map schema, cannot infer innerschema: ", fe);
}
Type convertedValue = convertWithName(innerField, "value");
return ConversionPatterns.stringKeyMapType(Repetition.OPTIONAL, alias, name(innerField.alias, "map"), convertedValue);
}
use of org.apache.pig.impl.logicalLayer.FrontendException in project parquet-mr by apache.
the class ParquetLoader method getSchemaFromRequiredFieldList.
private Schema getSchemaFromRequiredFieldList(Schema schema, List<RequiredField> fieldList) throws FrontendException {
Schema s = new Schema();
for (RequiredField rf : fieldList) {
FieldSchema f;
try {
f = schema.getField(rf.getAlias()).clone();
} catch (CloneNotSupportedException e) {
throw new FrontendException("Clone not supported for the fieldschema", e);
}
if (rf.getSubFields() == null) {
s.add(f);
} else {
Schema innerSchema = getSchemaFromRequiredFieldList(f.schema, rf.getSubFields());
if (innerSchema == null) {
return null;
} else {
f.schema = innerSchema;
s.add(f);
}
}
}
return s;
}
use of org.apache.pig.impl.logicalLayer.FrontendException in project zeppelin by apache.
the class PigQueryInterpreter method interpret.
@Override
public InterpreterResult interpret(String st, InterpreterContext context) {
// '-' is invalid for pig alias
String alias = "paragraph_" + context.getParagraphId().replace("-", "_");
String[] lines = st.split("\n");
List<String> queries = new ArrayList<>();
for (int i = 0; i < lines.length; ++i) {
if (i == lines.length - 1) {
lines[i] = alias + " = " + lines[i];
}
queries.add(lines[i]);
}
StringBuilder resultBuilder = new StringBuilder("%table ");
try {
pigServer.setJobName(createJobName(st, context));
File tmpScriptFile = PigUtils.createTempPigScript(queries);
// each thread should its own ScriptState & PigStats
ScriptState.start(pigServer.getPigContext().getExecutionEngine().instantiateScriptState());
// reset PigStats, otherwise you may get the PigStats of last job in the same thread
// because PigStats is ThreadLocal variable
PigStats.start(pigServer.getPigContext().getExecutionEngine().instantiatePigStats());
PigScriptListener scriptListener = new PigScriptListener();
ScriptState.get().registerListener(scriptListener);
listenerMap.put(context.getParagraphId(), scriptListener);
pigServer.registerScript(tmpScriptFile.getAbsolutePath());
Schema schema = pigServer.dumpSchema(alias);
boolean schemaKnown = (schema != null);
if (schemaKnown) {
for (int i = 0; i < schema.size(); ++i) {
Schema.FieldSchema field = schema.getField(i);
resultBuilder.append(field.alias != null ? field.alias : "col_" + i);
if (i != schema.size() - 1) {
resultBuilder.append("\t");
}
}
resultBuilder.append("\n");
}
Iterator<Tuple> iter = pigServer.openIterator(alias);
boolean firstRow = true;
int index = 0;
while (iter.hasNext() && index < maxResult) {
index++;
Tuple tuple = iter.next();
if (firstRow && !schemaKnown) {
for (int i = 0; i < tuple.size(); ++i) {
resultBuilder.append("c_" + i + "\t");
}
resultBuilder.append("\n");
firstRow = false;
}
resultBuilder.append(StringUtils.join(tuple.iterator(), "\t"));
resultBuilder.append("\n");
}
if (index >= maxResult && iter.hasNext()) {
resultBuilder.append("\n");
resultBuilder.append(ResultMessages.getExceedsLimitRowsMessage(maxResult, MAX_RESULTS));
}
} catch (IOException e) {
// 4. Other errors.
if (e instanceof FrontendException) {
FrontendException fe = (FrontendException) e;
if (!fe.getMessage().contains("Backend error :")) {
LOGGER.error("Fail to run pig query.", e);
return new InterpreterResult(Code.ERROR, ExceptionUtils.getStackTrace(e));
}
}
if (e.getCause() instanceof ParseException) {
return new InterpreterResult(Code.ERROR, e.getMessage());
}
PigStats stats = PigStats.get();
if (stats != null) {
String errorMsg = stats.getDisplayString();
if (errorMsg != null) {
return new InterpreterResult(Code.ERROR, errorMsg);
}
}
LOGGER.error("Fail to run pig query.", e);
return new InterpreterResult(Code.ERROR, ExceptionUtils.getStackTrace(e));
} finally {
listenerMap.remove(context.getParagraphId());
}
return new InterpreterResult(Code.SUCCESS, resultBuilder.toString());
}
Aggregations