use of org.apache.avro.AvroRuntimeException in project incubator-gobblin by apache.
the class AvroUtils method switchNamespace.
/**
* Copies the input {@link org.apache.avro.Schema} but changes the schema namespace.
* @param schema {@link org.apache.avro.Schema} to copy.
* @param namespaceOverride namespace for the copied {@link org.apache.avro.Schema}.
* @return A {@link org.apache.avro.Schema} that is a copy of schema, but has the new namespace.
*/
public static Schema switchNamespace(Schema schema, Map<String, String> namespaceOverride) {
Schema newSchema;
String newNamespace = StringUtils.EMPTY;
// (Primitives are simply cloned)
switch(schema.getType()) {
case ENUM:
newNamespace = namespaceOverride.containsKey(schema.getNamespace()) ? namespaceOverride.get(schema.getNamespace()) : schema.getNamespace();
newSchema = Schema.createEnum(schema.getName(), schema.getDoc(), newNamespace, schema.getEnumSymbols());
break;
case FIXED:
newNamespace = namespaceOverride.containsKey(schema.getNamespace()) ? namespaceOverride.get(schema.getNamespace()) : schema.getNamespace();
newSchema = Schema.createFixed(schema.getName(), schema.getDoc(), newNamespace, schema.getFixedSize());
break;
case MAP:
newSchema = Schema.createMap(switchNamespace(schema.getValueType(), namespaceOverride));
break;
case RECORD:
newNamespace = namespaceOverride.containsKey(schema.getNamespace()) ? namespaceOverride.get(schema.getNamespace()) : schema.getNamespace();
List<Schema.Field> newFields = new ArrayList<>();
if (schema.getFields().size() > 0) {
for (Schema.Field oldField : schema.getFields()) {
Field newField = new Field(oldField.name(), switchNamespace(oldField.schema(), namespaceOverride), oldField.doc(), oldField.defaultValue(), oldField.order());
// Copy field level properties
copyFieldProperties(oldField, newField);
newFields.add(newField);
}
}
newSchema = Schema.createRecord(schema.getName(), schema.getDoc(), newNamespace, schema.isError());
newSchema.setFields(newFields);
break;
case UNION:
List<Schema> newUnionMembers = new ArrayList<>();
if (null != schema.getTypes() && schema.getTypes().size() > 0) {
for (Schema oldUnionMember : schema.getTypes()) {
newUnionMembers.add(switchNamespace(oldUnionMember, namespaceOverride));
}
}
newSchema = Schema.createUnion(newUnionMembers);
break;
case ARRAY:
newSchema = Schema.createArray(switchNamespace(schema.getElementType(), namespaceOverride));
break;
case BOOLEAN:
case BYTES:
case DOUBLE:
case FLOAT:
case INT:
case LONG:
case NULL:
case STRING:
newSchema = Schema.create(schema.getType());
break;
default:
String exceptionMessage = String.format("Schema namespace replacement failed for \"%s\" ", schema);
LOG.error(exceptionMessage);
throw new AvroRuntimeException(exceptionMessage);
}
// Copy schema metadata
copyProperties(schema, newSchema);
return newSchema;
}
use of org.apache.avro.AvroRuntimeException in project crunch by cloudera.
the class ScalaSafeReflectData method findField.
private static Field findField(Class original, String name) {
Class c = original;
do {
try {
Field f = c.getDeclaredField(dirty(name));
f.setAccessible(true);
return f;
} catch (NoSuchFieldException e) {
}
c = c.getSuperclass();
} while (c != null);
throw new AvroRuntimeException("No field named " + name + " in: " + original);
}
use of org.apache.avro.AvroRuntimeException in project apex-malhar by apache.
the class AvroFileInputOperator method readEntity.
/**
* Reads a GenericRecord from the given input stream<br>
* Emits the FileName,Offset,Exception on the error port if its connected
*
* @return GenericRecord
*/
@Override
protected GenericRecord readEntity() throws IOException {
GenericRecord record = null;
record = null;
try {
if (avroDataStream != null && avroDataStream.hasNext()) {
offset++;
record = avroDataStream.next();
recordCount++;
return record;
}
} catch (AvroRuntimeException are) {
LOG.error("Exception in parsing record for file - " + super.currentFile + " at offset - " + offset, are);
if (errorRecordsPort.isConnected()) {
errorRecordsPort.emit("FileName:" + super.currentFile + ", Offset:" + offset);
}
errorCount++;
throw new AvroRuntimeException(are);
}
return record;
}
use of org.apache.avro.AvroRuntimeException in project apex-malhar by apache.
the class PojoToAvro method getGenericRecord.
/**
* Returns a generic record mapping the POJO fields to provided schema
*
* @return Generic Record
*/
private GenericRecord getGenericRecord(Object tuple) throws Exception {
int writeErrorCount = 0;
GenericRecord rec = new GenericData.Record(getSchema());
for (int i = 0; i < columnNames.size(); i++) {
try {
rec.put(columnNames.get(i).name(), AvroRecordHelper.convertValueStringToAvroKeyType(getSchema(), columnNames.get(i).name(), keyMethodMap.get(i).get(tuple).toString()));
} catch (AvroRuntimeException e) {
LOG.error("Could not set Field [" + columnNames.get(i).name() + "] in the generic record", e);
fieldErrorCount++;
} catch (Exception e) {
LOG.error("Parse Exception", e);
fieldErrorCount++;
writeErrorCount++;
}
}
if (columnNames.size() == writeErrorCount) {
errorCount++;
return null;
} else {
return rec;
}
}
use of org.apache.avro.AvroRuntimeException in project apex-malhar by apache.
the class AvroToPojo method getPOJOFromGenericRecord.
/**
* Returns a POJO from a Generic Record
*
* @return Object
*/
@SuppressWarnings("unchecked")
private Object getPOJOFromGenericRecord(GenericRecord tuple) throws InstantiationException, IllegalAccessException {
Object newObj = getPojoClass().newInstance();
try {
for (int i = 0; i < columnFieldSetters.size(); i++) {
AvroToPojo.ActiveFieldInfo afi = columnFieldSetters.get(i);
SupportType st = afi.fieldInfo.getType();
Object val = null;
try {
val = tuple.get(afi.fieldInfo.getColumnName());
} catch (Exception e) {
LOG.error("Could not find field -" + afi.fieldInfo.getColumnName() + "- in the generic record", e);
val = null;
fieldErrorCount++;
}
if (val == null) {
continue;
}
try {
switch(st) {
case BOOLEAN:
((PojoUtils.SetterBoolean<Object>) afi.setterOrGetter).set(newObj, (boolean) tuple.get(afi.fieldInfo.getColumnName()));
break;
case DOUBLE:
((PojoUtils.SetterDouble<Object>) afi.setterOrGetter).set(newObj, (double) tuple.get(afi.fieldInfo.getColumnName()));
break;
case FLOAT:
((PojoUtils.SetterFloat<Object>) afi.setterOrGetter).set(newObj, (float) tuple.get(afi.fieldInfo.getColumnName()));
break;
case INTEGER:
((PojoUtils.SetterInt<Object>) afi.setterOrGetter).set(newObj, (int) tuple.get(afi.fieldInfo.getColumnName()));
break;
case STRING:
((PojoUtils.Setter<Object, String>) afi.setterOrGetter).set(newObj, new String(tuple.get(afi.fieldInfo.getColumnName()).toString()));
break;
case LONG:
((PojoUtils.SetterLong<Object>) afi.setterOrGetter).set(newObj, (long) tuple.get(afi.fieldInfo.getColumnName()));
break;
default:
throw new AvroRuntimeException("Invalid Support Type");
}
} catch (AvroRuntimeException e) {
LOG.error("Exception in setting value", e);
fieldErrorCount++;
}
}
} catch (Exception ex) {
LOG.error("Generic Exception in setting value" + ex.getMessage());
errorCount++;
newObj = null;
}
return newObj;
}
Aggregations