Java Code Examples for org.apache.avro.reflect.ReflectData#newInstance()

The following examples show how to use org.apache.avro.reflect.ReflectData#newInstance() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RecordBuilder.java    From parquet-mr with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private E newRecordInstance() {
  if (recordClass != GenericData.Record.class && !recordClass.isInterface()) {
    E record = (E) ReflectData.newInstance(recordClass, schema);
    if (record != null) {
      return record;
    }
  }
  return (E) new GenericData.Record(schema);
}
 
Example 2
Source File: AvroRecordConverter.java    From parquet-mr with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private Collection<Object> newContainer() {
  if (containerClass == null) {
    return new GenericData.Array<Object>(0, avroSchema);
  } else if (containerClass.isAssignableFrom(ArrayList.class)) {
    return new ArrayList<Object>();
  } else {
    // not need to use the data model to instantiate because it resolved
    // the class, which used the correct ClassLoader
    return (Collection<Object>) ReflectData.newInstance(containerClass, avroSchema);
  }
}
 
Example 3
Source File: AvroRecordConverter.java    From parquet-mr with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private Map<K, V> newMap() {
  if (mapClass == null || mapClass.isAssignableFrom(HashMap.class)) {
    return new HashMap<K, V>();
  } else {
    return (Map<K, V>) ReflectData.newInstance(mapClass, schema);
  }
}
 
Example 4
Source File: CSVRecordBuilder.java    From kite with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private E newRecordInstance() {
  if (recordClass != GenericData.Record.class && !recordClass.isInterface()) {
    E record = (E) ReflectData.newInstance(recordClass, schema);
    if (record != null) {
      return record;
    }
  }
  return (E) new GenericData.Record(schema);
}
 
Example 5
Source File: DataModelUtil.java    From kite with Apache License 2.0 3 votes vote down vote up
/**
 * If E implements GenericRecord, but does not implement SpecificRecord, then
 * create a new instance of E using reflection so that GenericDataumReader
 * will use the expected type.
 *
 * Implementations of GenericRecord that require a {@link Schema} parameter
 * in the constructor should implement SpecificData.SchemaConstructable.
 * Otherwise, your implementation must have a no-args constructor.
 *
 * @param <E> The entity type
 * @param type The Java class of the entity type
 * @param schema The reader schema
 * @return An instance of E, or null if the data model is specific or reflect
 */
@SuppressWarnings("unchecked")
public static <E> E createRecord(Class<E> type, Schema schema) {
  // Don't instantiate SpecificRecords or interfaces.
  if (isGeneric(type) && !type.isInterface()) {
    if (GenericData.Record.class.equals(type)) {
      return (E) GenericData.get().newRecord(null, schema);
    }
    return (E) ReflectData.newInstance(type, schema);
  }

  return null;
}