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

The following examples show how to use org.apache.avro.Schema.Type#DOUBLE . 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: AvroSchemaGenerator.java    From aliyun-maxcompute-data-collectors with Apache License 2.0 6 votes vote down vote up
private Type toAvroType(String columnName, int sqlType) {
  Properties mapping = options.getMapColumnJava();

  if (mapping.containsKey(columnName)) {
    String type = mapping.getProperty(columnName);
    if (LOG.isDebugEnabled()) {
      LOG.info("Overriding type of column " + columnName + " to " + type);
    }

    if (type.equalsIgnoreCase("INTEGER")) { return Type.INT; }
    if (type.equalsIgnoreCase("LONG")) { return Type.LONG; }
    if (type.equalsIgnoreCase("BOOLEAN")) { return Type.BOOLEAN; }
    if (type.equalsIgnoreCase("FLOAT")) { return Type.FLOAT; }
    if (type.equalsIgnoreCase("DOUBLE")) { return Type.DOUBLE; }
    if (type.equalsIgnoreCase("STRING")) { return Type.STRING; }
    if (type.equalsIgnoreCase("BYTES")) { return Type.BYTES; }

    // Mapping was not found
    throw new IllegalArgumentException("Cannot convert to AVRO type " + type);
  }

  return connManager.toAvroType(tableName, columnName, sqlType);
}
 
Example 2
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);
        }
    }
}
 
Example 3
Source File: ConnManager.java    From aliyun-maxcompute-data-collectors with Apache License 2.0 4 votes vote down vote up
/**
 * Resolve a database-specific type to Avro data type.
 * @param sqlType     sql type
 * @return            avro type
 */
public Type toAvroType(int sqlType) {
  switch (sqlType) {
  case Types.TINYINT:
  case Types.SMALLINT:
  case Types.INTEGER:
    return Type.INT;
  case Types.BIGINT:
    return Type.LONG;
  case Types.BIT:
  case Types.BOOLEAN:
    return Type.BOOLEAN;
  case Types.REAL:
    return Type.FLOAT;
  case Types.FLOAT:
  case Types.DOUBLE:
    return Type.DOUBLE;
  case Types.NUMERIC:
  case Types.DECIMAL:
    return Type.STRING;
  case Types.CHAR:
  case Types.VARCHAR:
  case Types.LONGVARCHAR:
  case Types.LONGNVARCHAR:
  case Types.NVARCHAR:
  case Types.NCHAR:
    return Type.STRING;
  case Types.DATE:
  case Types.TIME:
  case Types.TIMESTAMP:
    return Type.LONG;
  case Types.BLOB:
  case Types.BINARY:
  case Types.VARBINARY:
  case Types.LONGVARBINARY:
    return Type.BYTES;
  default:
    throw new IllegalArgumentException("Cannot convert SQL type "
        + sqlType);
  }
}