Java Code Examples for org.apache.avro.Schema.Type#MAP

The following examples show how to use org.apache.avro.Schema.Type#MAP . 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: AvroUtils.java    From Cubert with Apache License 2.0 6 votes vote down vote up
private static Type convertToAvroType(DataType colType)
{
    final Type subType;
    if (colType == DataType.TUPLE)
    {
        /* Pig converts RECORD to TUPLE. Converting it back. */
        subType = Type.RECORD;
    }
    else if (colType == DataType.BAG)
    {
        subType = Type.ARRAY;
    }
    else if (colType == DataType.MAP)
    {
      subType = Type.MAP;
    }
    else
    {
        subType = Type.valueOf(colType.toString().toUpperCase());
    }
    return subType;
}
 
Example 2
Source File: AvroUtils.java    From Cubert with Apache License 2.0 5 votes vote down vote up
private static Field[] createFields(BlockSchema schema){
  Field[] fields = new Field[schema.getNumColumns()];
  for (int idx = 0; idx < fields.length; idx++)
  {
    final ColumnType col = schema.getColumnType(idx);
    final DataType colType = col.getType();
    final Type subType = convertToAvroType(colType);
    
    final Schema colSchema;
    if (col.getColumnSchema() != null ||
        subType == Type.ARRAY || subType == Type.MAP)
    {
      colSchema =
        convertFromBlockSchema(col.getName(),
                               subType,
                               col.getColumnSchema(), false);
      
    }
    else
    {
      List<Schema> unionSchema = new ArrayList<Schema>();
      unionSchema.add(Schema.create(Type.NULL));
      unionSchema.add(Schema.create(subType));
      
      colSchema = Schema.createUnion(unionSchema);
    }
    fields[idx] = new Field(col.getName(), colSchema, null, null);
  }
  return fields;
}
 
Example 3
Source File: CSVUtils.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
/**
 *
 */
private static void updateRecord(Field field, Type type, String providedValue, Record avroRecord) {
    if (Type.NULL != type) {
        Object value;
        if (Type.INT == type) {
            value = null == providedValue ? possiblyGetDefaultValue(field, IntNode.class).getIntValue()
                    : Integer.parseInt(providedValue);
            avroRecord.put(field.name(), value);
        } else if (Type.BOOLEAN == type) {
            value = null == providedValue
                    ? possiblyGetDefaultValue(field, BooleanNode.class).getBooleanValue()
                    : Boolean.parseBoolean(providedValue);
            avroRecord.put(field.name(), value);
        } else if (Type.DOUBLE == type) {
            value = null == providedValue ? possiblyGetDefaultValue(field, DoubleNode.class).getDoubleValue()
                    : Double.parseDouble(providedValue);
            avroRecord.put(field.name(), value);
        } else if (Type.FLOAT == type) {
            value = null == providedValue ? possiblyGetDefaultValue(field, DoubleNode.class).getDoubleValue()
                    : Float.parseFloat(providedValue);
            avroRecord.put(field.name(), value);
        } else if (Type.LONG == type) {
            value = null == providedValue ? possiblyGetDefaultValue(field, LongNode.class).getLongValue()
                    : Long.parseLong(providedValue);
            avroRecord.put(field.name(), value);
        } else if (Type.STRING == type) {
            value = null == providedValue ? possiblyGetDefaultValue(field, TextNode.class).getTextValue()
                    : providedValue;
            avroRecord.put(field.name(), value);
        } else if (Type.BYTES == type) {
            value = encodeLogicalType(field, providedValue);
            avroRecord.put(field.name(), value);
        } else if (Type.UNION == type) {
            field.schema().getTypes()
                    .forEach(schema -> updateRecord(field, schema.getType(), providedValue, avroRecord));
        } else if (Type.ARRAY == type || Type.ENUM == type || Type.FIXED == type || Type.MAP == type
                || Type.NULL == type || Type.RECORD == type) {
            throw new IllegalArgumentException("The field type '" + type + "' is not supported at the moment");
        } else {
            avroRecord.put(field.name(), providedValue);
        }
    }
}