Search in sources :

Example 11 with AvroRuntimeException

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;
}
Also used : Field(org.apache.avro.Schema.Field) Schema(org.apache.avro.Schema) ArrayList(java.util.ArrayList) AvroRuntimeException(org.apache.avro.AvroRuntimeException) ToString(lombok.ToString) Field(org.apache.avro.Schema.Field)

Example 12 with AvroRuntimeException

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);
}
Also used : Field(java.lang.reflect.Field) AvroRuntimeException(org.apache.avro.AvroRuntimeException)

Example 13 with AvroRuntimeException

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;
}
Also used : AvroRuntimeException(org.apache.avro.AvroRuntimeException) GenericRecord(org.apache.avro.generic.GenericRecord)

Example 14 with AvroRuntimeException

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;
    }
}
Also used : GenericRecord(org.apache.avro.generic.GenericRecord) AvroRuntimeException(org.apache.avro.AvroRuntimeException) GenericRecord(org.apache.avro.generic.GenericRecord) IOException(java.io.IOException) AvroRuntimeException(org.apache.avro.AvroRuntimeException)

Example 15 with AvroRuntimeException

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;
}
Also used : AvroRuntimeException(org.apache.avro.AvroRuntimeException) AvroRuntimeException(org.apache.avro.AvroRuntimeException) SupportType(org.apache.apex.malhar.lib.util.FieldInfo.SupportType)

Aggregations

AvroRuntimeException (org.apache.avro.AvroRuntimeException)17 Schema (org.apache.avro.Schema)8 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 Field (org.apache.avro.Schema.Field)3 GenericRecord (org.apache.avro.generic.GenericRecord)3 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 ToString (lombok.ToString)2 CsvParseException (org.spf4j.io.csv.CsvParseException)2 BufferedInputStream (java.io.BufferedInputStream)1 BufferedReader (java.io.BufferedReader)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 DataInputStream (java.io.DataInputStream)1 File (java.io.File)1 InputStream (java.io.InputStream)1 Field (java.lang.reflect.Field)1 URI (java.net.URI)1 FileSystem (java.nio.file.FileSystem)1