Java Code Examples for org.apache.avro.LogicalTypes#timestampMillis()

The following examples show how to use org.apache.avro.LogicalTypes#timestampMillis() . 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: AvroSchemaConverter190Int96Avro18.java    From datacollector with Apache License 2.0 6 votes vote down vote up
private LogicalType convertOriginalTypeToLogicalType(OriginalType annotation, DecimalMetadata meta) {
  if (annotation == null) {
    return null;
  }
  switch (annotation) {
    case DECIMAL:
      return LogicalTypes.decimal(meta.getPrecision(), meta.getScale());
    case DATE:
      return LogicalTypes.date();
    case TIME_MILLIS:
      return LogicalTypes.timeMillis();
    case TIME_MICROS:
      return LogicalTypes.timeMicros();
    case TIMESTAMP_MILLIS:
      return LogicalTypes.timestampMillis();
    case TIMESTAMP_MICROS:
      return LogicalTypes.timestampMicros();
    default:
      return null;
  }
}
 
Example 2
Source File: AvroRowSerializationSchema.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private long convertFromTimestamp(Schema schema, Timestamp date) {
	final LogicalType logicalType = schema.getLogicalType();
	if (logicalType == LogicalTypes.timestampMillis()) {
		// adopted from Apache Calcite
		final long time = date.getTime();
		return time + (long) LOCAL_TZ.getOffset(time);
	} else {
		throw new RuntimeException("Unsupported timestamp type.");
	}
}
 
Example 3
Source File: AvroRowSerializationSchema.java    From flink with Apache License 2.0 5 votes vote down vote up
private long convertFromTimestamp(Schema schema, Timestamp date) {
	final LogicalType logicalType = schema.getLogicalType();
	if (logicalType == LogicalTypes.timestampMillis()) {
		// adopted from Apache Calcite
		final long time = date.getTime();
		return time + (long) LOCAL_TZ.getOffset(time);
	} else {
		throw new RuntimeException("Unsupported timestamp type.");
	}
}
 
Example 4
Source File: PulsarMetadata.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
static Type convertType(Schema.Type avroType, LogicalType logicalType) {
    switch (avroType) {
        case BOOLEAN:
            return BooleanType.BOOLEAN;
        case INT:
            if (logicalType == LogicalTypes.timeMillis()) {
                return TIME;
            } else if (logicalType == LogicalTypes.date()) {
                return DATE;
            }
            return IntegerType.INTEGER;
        case LONG:
            if (logicalType == LogicalTypes.timestampMillis()) {
                return TIMESTAMP;
            }
            return BigintType.BIGINT;
        case FLOAT:
            return RealType.REAL;
        case DOUBLE:
            return DoubleType.DOUBLE;
        case BYTES:
            return VarbinaryType.VARBINARY;
        case STRING:
            return VarcharType.VARCHAR;
        case ENUM:
            return VarcharType.VARCHAR;
        default:
            log.error("Cannot convert type: %s", avroType);
            return null;
    }
}
 
Example 5
Source File: ConvertAvroTypeToSQL.java    From components with Apache License 2.0 5 votes vote down vote up
private int convertAvroLogicialType(LogicalType logicalType) {

        Integer sqlType = this.config.CONVERT_LOGICALTYPE_TO_SQLTYPE.get(logicalType);
        if(sqlType != null){
            return sqlType;
        }

        if (logicalType == LogicalTypes.timestampMillis()) {
            sqlType = Types.TIMESTAMP;
        } else if (logicalType instanceof LogicalTypes.Decimal) {
            sqlType = Types.NUMERIC;
        } else if (logicalType == LogicalTypes.date()) {
            sqlType = Types.DATE;
        } else if (logicalType == LogicalTypes.uuid()) {
            sqlType = Types.VARCHAR;
        } else if (logicalType == LogicalTypes.timestampMicros()) {
            sqlType = Types.TIMESTAMP;
        } else if (logicalType == LogicalTypes.timeMillis()) {
            sqlType = Types.TIME;
        } else if (logicalType == LogicalTypes.timeMicros()) {
            sqlType = Types.TIME;
        } else {
            // All logical type should be supported
            throw new UnsupportedOperationException("Logical type " + logicalType + " not supported");
        }

        return sqlType;
    }
 
Example 6
Source File: SnowflakeWriter.java    From components with Apache License 2.0 5 votes vote down vote up
private Object formatIfAnySnowflakeDateType(Object inputValue, Schema s) {
    if (LogicalTypes.fromSchemaIgnoreInvalid(s) == LogicalTypes.timeMillis()) {
        return formatter.formatTimeMillis(inputValue);
    } else if (LogicalTypes.fromSchemaIgnoreInvalid(s) == LogicalTypes.date()) {
        return formatter.formatDate(inputValue);
    } else if (LogicalTypes.fromSchemaIgnoreInvalid(s) == LogicalTypes.timestampMillis()) {
        return formatter.formatTimestampMillis(inputValue);
    } else {
        return inputValue;
    }
}
 
Example 7
Source File: AvroRowSerializationSchema.java    From flink with Apache License 2.0 5 votes vote down vote up
private long convertFromTimestamp(Schema schema, Timestamp date) {
	final LogicalType logicalType = schema.getLogicalType();
	if (logicalType == LogicalTypes.timestampMillis()) {
		// adopted from Apache Calcite
		final long time = date.getTime();
		return time + (long) LOCAL_TZ.getOffset(time);
	} else {
		throw new RuntimeException("Unsupported timestamp type.");
	}
}
 
Example 8
Source File: AvroSchemaConverter.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
private static TypeInformation<?> convertToTypeInfo(Schema schema) {
	switch (schema.getType()) {
		case RECORD:
			final List<Schema.Field> fields = schema.getFields();

			final TypeInformation<?>[] types = new TypeInformation<?>[fields.size()];
			final String[] names = new String[fields.size()];
			for (int i = 0; i < fields.size(); i++) {
				final Schema.Field field = fields.get(i);
				types[i] = convertToTypeInfo(field.schema());
				names[i] = field.name();
			}
			return Types.ROW_NAMED(names, types);
		case ENUM:
			return Types.STRING;
		case ARRAY:
			// result type might either be ObjectArrayTypeInfo or BasicArrayTypeInfo for Strings
			return Types.OBJECT_ARRAY(convertToTypeInfo(schema.getElementType()));
		case MAP:
			return Types.MAP(Types.STRING, convertToTypeInfo(schema.getValueType()));
		case UNION:
			final Schema actualSchema;
			if (schema.getTypes().size() == 2 && schema.getTypes().get(0).getType() == Schema.Type.NULL) {
				actualSchema = schema.getTypes().get(1);
			} else if (schema.getTypes().size() == 2 && schema.getTypes().get(1).getType() == Schema.Type.NULL) {
				actualSchema = schema.getTypes().get(0);
			} else if (schema.getTypes().size() == 1) {
				actualSchema = schema.getTypes().get(0);
			} else {
				// use Kryo for serialization
				return Types.GENERIC(Object.class);
			}
			return convertToTypeInfo(actualSchema);
		case FIXED:
			// logical decimal type
			if (schema.getLogicalType() instanceof LogicalTypes.Decimal) {
				return Types.BIG_DEC;
			}
			// convert fixed size binary data to primitive byte arrays
			return Types.PRIMITIVE_ARRAY(Types.BYTE);
		case STRING:
			// convert Avro's Utf8/CharSequence to String
			return Types.STRING;
		case BYTES:
			// logical decimal type
			if (schema.getLogicalType() instanceof LogicalTypes.Decimal) {
				return Types.BIG_DEC;
			}
			return Types.PRIMITIVE_ARRAY(Types.BYTE);
		case INT:
			// logical date and time type
			final LogicalType logicalType = schema.getLogicalType();
			if (logicalType == LogicalTypes.date()) {
				return Types.SQL_DATE;
			} else if (logicalType == LogicalTypes.timeMillis()) {
				return Types.SQL_TIME;
			}
			return Types.INT;
		case LONG:
			// logical timestamp type
			if (schema.getLogicalType() == LogicalTypes.timestampMillis()) {
				return Types.SQL_TIMESTAMP;
			}
			return Types.LONG;
		case FLOAT:
			return Types.FLOAT;
		case DOUBLE:
			return Types.DOUBLE;
		case BOOLEAN:
			return Types.BOOLEAN;
		case NULL:
			return Types.VOID;
	}
	throw new IllegalArgumentException("Unsupported Avro type '" + schema.getType() + "'.");
}
 
Example 9
Source File: AvroSchemaConverter.java    From flink with Apache License 2.0 4 votes vote down vote up
private static TypeInformation<?> convertToTypeInfo(Schema schema) {
	switch (schema.getType()) {
		case RECORD:
			final List<Schema.Field> fields = schema.getFields();

			final TypeInformation<?>[] types = new TypeInformation<?>[fields.size()];
			final String[] names = new String[fields.size()];
			for (int i = 0; i < fields.size(); i++) {
				final Schema.Field field = fields.get(i);
				types[i] = convertToTypeInfo(field.schema());
				names[i] = field.name();
			}
			return Types.ROW_NAMED(names, types);
		case ENUM:
			return Types.STRING;
		case ARRAY:
			// result type might either be ObjectArrayTypeInfo or BasicArrayTypeInfo for Strings
			return Types.OBJECT_ARRAY(convertToTypeInfo(schema.getElementType()));
		case MAP:
			return Types.MAP(Types.STRING, convertToTypeInfo(schema.getValueType()));
		case UNION:
			final Schema actualSchema;
			if (schema.getTypes().size() == 2 && schema.getTypes().get(0).getType() == Schema.Type.NULL) {
				actualSchema = schema.getTypes().get(1);
			} else if (schema.getTypes().size() == 2 && schema.getTypes().get(1).getType() == Schema.Type.NULL) {
				actualSchema = schema.getTypes().get(0);
			} else if (schema.getTypes().size() == 1) {
				actualSchema = schema.getTypes().get(0);
			} else {
				// use Kryo for serialization
				return Types.GENERIC(Object.class);
			}
			return convertToTypeInfo(actualSchema);
		case FIXED:
			// logical decimal type
			if (schema.getLogicalType() instanceof LogicalTypes.Decimal) {
				return Types.BIG_DEC;
			}
			// convert fixed size binary data to primitive byte arrays
			return Types.PRIMITIVE_ARRAY(Types.BYTE);
		case STRING:
			// convert Avro's Utf8/CharSequence to String
			return Types.STRING;
		case BYTES:
			// logical decimal type
			if (schema.getLogicalType() instanceof LogicalTypes.Decimal) {
				return Types.BIG_DEC;
			}
			return Types.PRIMITIVE_ARRAY(Types.BYTE);
		case INT:
			// logical date and time type
			final LogicalType logicalType = schema.getLogicalType();
			if (logicalType == LogicalTypes.date()) {
				return Types.SQL_DATE;
			} else if (logicalType == LogicalTypes.timeMillis()) {
				return Types.SQL_TIME;
			}
			return Types.INT;
		case LONG:
			// logical timestamp type
			if (schema.getLogicalType() == LogicalTypes.timestampMillis()) {
				return Types.SQL_TIMESTAMP;
			}
			return Types.LONG;
		case FLOAT:
			return Types.FLOAT;
		case DOUBLE:
			return Types.DOUBLE;
		case BOOLEAN:
			return Types.BOOLEAN;
		case NULL:
			return Types.VOID;
	}
	throw new IllegalArgumentException("Unsupported Avro type '" + schema.getType() + "'.");
}
 
Example 10
Source File: AvroSchemaConverter.java    From flink with Apache License 2.0 4 votes vote down vote up
private static TypeInformation<?> convertToTypeInfo(Schema schema) {
	switch (schema.getType()) {
		case RECORD:
			final List<Schema.Field> fields = schema.getFields();

			final TypeInformation<?>[] types = new TypeInformation<?>[fields.size()];
			final String[] names = new String[fields.size()];
			for (int i = 0; i < fields.size(); i++) {
				final Schema.Field field = fields.get(i);
				types[i] = convertToTypeInfo(field.schema());
				names[i] = field.name();
			}
			return Types.ROW_NAMED(names, types);
		case ENUM:
			return Types.STRING;
		case ARRAY:
			// result type might either be ObjectArrayTypeInfo or BasicArrayTypeInfo for Strings
			return Types.OBJECT_ARRAY(convertToTypeInfo(schema.getElementType()));
		case MAP:
			return Types.MAP(Types.STRING, convertToTypeInfo(schema.getValueType()));
		case UNION:
			final Schema actualSchema;
			if (schema.getTypes().size() == 2 && schema.getTypes().get(0).getType() == Schema.Type.NULL) {
				actualSchema = schema.getTypes().get(1);
			} else if (schema.getTypes().size() == 2 && schema.getTypes().get(1).getType() == Schema.Type.NULL) {
				actualSchema = schema.getTypes().get(0);
			} else if (schema.getTypes().size() == 1) {
				actualSchema = schema.getTypes().get(0);
			} else {
				// use Kryo for serialization
				return Types.GENERIC(Object.class);
			}
			return convertToTypeInfo(actualSchema);
		case FIXED:
			// logical decimal type
			if (schema.getLogicalType() instanceof LogicalTypes.Decimal) {
				return Types.BIG_DEC;
			}
			// convert fixed size binary data to primitive byte arrays
			return Types.PRIMITIVE_ARRAY(Types.BYTE);
		case STRING:
			// convert Avro's Utf8/CharSequence to String
			return Types.STRING;
		case BYTES:
			// logical decimal type
			if (schema.getLogicalType() instanceof LogicalTypes.Decimal) {
				return Types.BIG_DEC;
			}
			return Types.PRIMITIVE_ARRAY(Types.BYTE);
		case INT:
			// logical date and time type
			final org.apache.avro.LogicalType logicalType = schema.getLogicalType();
			if (logicalType == LogicalTypes.date()) {
				return Types.SQL_DATE;
			} else if (logicalType == LogicalTypes.timeMillis()) {
				return Types.SQL_TIME;
			}
			return Types.INT;
		case LONG:
			// logical timestamp type
			if (schema.getLogicalType() == LogicalTypes.timestampMillis()) {
				return Types.SQL_TIMESTAMP;
			}
			return Types.LONG;
		case FLOAT:
			return Types.FLOAT;
		case DOUBLE:
			return Types.DOUBLE;
		case BOOLEAN:
			return Types.BOOLEAN;
		case NULL:
			return Types.VOID;
	}
	throw new IllegalArgumentException("Unsupported Avro type '" + schema.getType() + "'.");
}