List of usage examples for org.joda.time Instant EPOCH
Instant EPOCH
To view the source code for org.joda.time Instant EPOCH.
Click Source Link
From source file:org.apache.beam.sdk.schemas.utils.AvroUtils.java
License:Apache License
@Nullable private static Object genericFromBeamField(Schema.FieldType fieldType, org.apache.avro.Schema avroSchema, @Nullable Object value) { TypeWithNullability typeWithNullability = new TypeWithNullability(avroSchema); if (!fieldType.getNullable().equals(typeWithNullability.nullable)) { throw new IllegalArgumentException("FieldType " + fieldType + " and AVRO schema " + avroSchema + " don't have matching nullability"); }/*from www. j a va 2s. com*/ if (value == null) { return value; } switch (fieldType.getTypeName()) { case BYTE: case INT16: case INT32: case INT64: case FLOAT: case DOUBLE: case BOOLEAN: return value; case STRING: return new Utf8((String) value); case DECIMAL: BigDecimal decimal = (BigDecimal) value; LogicalType logicalType = typeWithNullability.type.getLogicalType(); return new Conversions.DecimalConversion().toBytes(decimal, null, logicalType); case DATETIME: if (typeWithNullability.type.getType() == Type.INT) { ReadableInstant instant = (ReadableInstant) value; return (int) Days.daysBetween(Instant.EPOCH, instant).getDays(); } else if (typeWithNullability.type.getType() == Type.LONG) { ReadableInstant instant = (ReadableInstant) value; return (long) instant.getMillis(); } else { throw new IllegalArgumentException( "Can't represent " + fieldType + " as " + typeWithNullability.type.getType()); } case BYTES: return ByteBuffer.wrap((byte[]) value); case LOGICAL_TYPE: FixedBytesField fixedBytesField = FixedBytesField.fromBeamFieldType(fieldType); if (fixedBytesField != null) { byte[] byteArray = (byte[]) value; if (byteArray.length != fixedBytesField.getSize()) { throw new IllegalArgumentException("Incorrectly sized byte array."); } return GenericData.get().createFixed(null, (byte[]) value, typeWithNullability.type); } throw new RuntimeException("Unknown logical type " + fieldType.getLogicalType().getIdentifier()); case ARRAY: List array = (List) value; List<Object> translatedArray = Lists.newArrayListWithExpectedSize(array.size()); for (Object arrayElement : array) { translatedArray.add(genericFromBeamField(fieldType.getCollectionElementType(), typeWithNullability.type.getElementType(), arrayElement)); } return translatedArray; case MAP: Map map = Maps.newHashMap(); Map<Object, Object> valueMap = (Map<Object, Object>) value; for (Map.Entry entry : valueMap.entrySet()) { Utf8 key = new Utf8((String) entry.getKey()); map.put(key, genericFromBeamField(fieldType.getMapValueType(), typeWithNullability.type.getValueType(), entry.getValue())); } return map; case ROW: return toGenericRecord((Row) value, typeWithNullability.type); default: throw new IllegalArgumentException("Unsupported type " + fieldType); } }
From source file:org.apache.beam.sdk.schemas.utils.AvroUtils.java
License:Apache License
/** * Strict conversion from AVRO to Beam, strict because it doesn't do widening or narrowing during * conversion.//from www.ja v a 2 s . co m * * @param value {@link GenericRecord} or any nested value * @param avroSchema schema for value * @param fieldType target beam field type * @return value converted for {@link Row} */ @SuppressWarnings("unchecked") @Nullable public static Object convertAvroFieldStrict(@Nullable Object value, @Nonnull org.apache.avro.Schema avroSchema, @Nonnull Schema.FieldType fieldType) { if (value == null) { return null; } TypeWithNullability type = new TypeWithNullability(avroSchema); LogicalType logicalType = LogicalTypes.fromSchema(type.type); if (logicalType != null) { if (logicalType instanceof LogicalTypes.Decimal) { ByteBuffer byteBuffer = (ByteBuffer) value; BigDecimal bigDecimal = new Conversions.DecimalConversion().fromBytes(byteBuffer.duplicate(), type.type, logicalType); return convertDecimal(bigDecimal, fieldType); } else if (logicalType instanceof LogicalTypes.TimestampMillis) { if (value instanceof ReadableInstant) { return convertDateTimeStrict(((ReadableInstant) value).getMillis(), fieldType); } else { return convertDateTimeStrict((Long) value, fieldType); } } else if (logicalType instanceof LogicalTypes.Date) { if (value instanceof ReadableInstant) { int epochDays = Days.daysBetween(Instant.EPOCH, (ReadableInstant) value).getDays(); return convertDateStrict(epochDays, fieldType); } else { return convertDateStrict((Integer) value, fieldType); } } } switch (type.type.getType()) { case FIXED: return convertFixedStrict((GenericFixed) value, fieldType); case BYTES: return convertBytesStrict((ByteBuffer) value, fieldType); case STRING: return convertStringStrict((CharSequence) value, fieldType); case INT: return convertIntStrict((Integer) value, fieldType); case LONG: return convertLongStrict((Long) value, fieldType); case FLOAT: return convertFloatStrict((Float) value, fieldType); case DOUBLE: return convertDoubleStrict((Double) value, fieldType); case BOOLEAN: return convertBooleanStrict((Boolean) value, fieldType); case RECORD: return convertRecordStrict((GenericRecord) value, fieldType); case ENUM: // enums are either Java enums, or GenericEnumSymbol, // they don't share common interface, but override toString() return convertEnumStrict(value, fieldType); case ARRAY: return convertArrayStrict((List<Object>) value, type.type.getElementType(), fieldType); case MAP: return convertMapStrict((Map<CharSequence, Object>) value, type.type.getValueType(), fieldType); case UNION: throw new IllegalArgumentException("Can't convert 'union', only nullable fields are supported"); case NULL: throw new IllegalArgumentException("Can't convert 'null' to non-nullable field"); default: throw new AssertionError("Unexpected AVRO Schema.Type: " + type.type.getType()); } }
From source file:org.apache.beam.sdk.schemas.utils.AvroUtils.java
License:Apache License
private static Object convertDateStrict(Integer epochDays, Schema.FieldType fieldType) { checkTypeName(fieldType.getTypeName(), TypeName.DATETIME, "date"); return Instant.EPOCH.plus(Duration.standardDays(epochDays)); }