本文整理了Java中org.apache.avro.LogicalTypes
类的一些代码示例,展示了LogicalTypes
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。LogicalTypes
类的具体详情如下:
包路径:org.apache.avro.LogicalTypes
类名称:LogicalTypes
暂无
代码示例来源:origin: apache/avro
@BeforeClass
public static void createSchemas() {
TestJsr310TimeConversions.DATE_SCHEMA = LogicalTypes.date()
.addToSchema(Schema.create(Schema.Type.INT));
TestJsr310TimeConversions.TIME_MILLIS_SCHEMA = LogicalTypes.timeMillis()
.addToSchema(Schema.create(Schema.Type.INT));
TestJsr310TimeConversions.TIME_MICROS_SCHEMA = LogicalTypes.timeMicros()
.addToSchema(Schema.create(Schema.Type.LONG));
TestJsr310TimeConversions.TIMESTAMP_MILLIS_SCHEMA = LogicalTypes.timestampMillis()
.addToSchema(Schema.create(Schema.Type.LONG));
TestJsr310TimeConversions.TIMESTAMP_MICROS_SCHEMA = LogicalTypes.timestampMicros()
.addToSchema(Schema.create(Schema.Type.LONG));
}
代码示例来源:origin: apache/avro
/** Create a Decimal LogicalType with the given precision and scale 0 */
public static Decimal decimal(int precision) {
return decimal(precision, 0);
}
代码示例来源:origin: apache/avro
@Test
public void testLogicalTypeEquals() {
LogicalTypes.Decimal decimal90 = LogicalTypes.decimal(9);
LogicalTypes.Decimal decimal80 = LogicalTypes.decimal(8);
LogicalTypes.Decimal decimal92 = LogicalTypes.decimal(9, 2);
assertEqualsTrue("Same decimal", LogicalTypes.decimal(9, 0), decimal90);
assertEqualsTrue("Same decimal", LogicalTypes.decimal(8, 0), decimal80);
assertEqualsTrue("Same decimal", LogicalTypes.decimal(9, 2), decimal92);
assertEqualsFalse("Different logical type", LogicalTypes.uuid(), decimal90);
assertEqualsFalse("Different precision", decimal90, decimal80);
assertEqualsFalse("Different scale", decimal90, decimal92);
}
代码示例来源:origin: apache/flink
if (logicalType == LogicalTypes.date()) {
return Types.SQL_DATE;
} else if (logicalType == LogicalTypes.timeMillis()) {
return Types.SQL_TIME;
case LONG:
if (schema.getLogicalType() == LogicalTypes.timestampMillis()) {
return Types.SQL_TIMESTAMP;
代码示例来源:origin: apache/avro
@Test
public void testSchemaRejectsSecondLogicalType() {
final Schema schema = Schema.createFixed("aDecimal", null, null, 4);
LogicalTypes.decimal(9).addToSchema(schema);
assertThrows("Should reject second logical type",
AvroRuntimeException.class,
"Can't overwrite property: scale", new Callable() {
@Override
public Object call() throws Exception {
LogicalTypes.decimal(9, 2).addToSchema(schema);
return null;
}
}
);
Assert.assertEquals("First logical type should still be set on schema",
LogicalTypes.decimal(9), LogicalTypes.fromSchemaIgnoreInvalid(schema));
}
代码示例来源:origin: Talend/components
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;
}
}
代码示例来源:origin: Netflix/iceberg
Schema.create(Schema.Type.FLOAT),
Schema.create(Schema.Type.DOUBLE),
LogicalTypes.date().addToSchema(Schema.create(Schema.Type.INT)),
LogicalTypes.timeMicros().addToSchema(Schema.create(Schema.Type.LONG)),
addAdjustToUtc(LogicalTypes.timestampMicros().addToSchema(Schema.create(Schema.Type.LONG)), true),
addAdjustToUtc(LogicalTypes.timestampMicros().addToSchema(Schema.create(Schema.Type.LONG)), false),
Schema.create(Schema.Type.STRING),
LogicalTypes.uuid().addToSchema(Schema.createFixed("uuid_fixed", null, null, 16)),
Schema.createFixed("fixed_12", null, null, 12),
Schema.create(Schema.Type.BYTES),
LogicalTypes.decimal(9, 4).addToSchema(Schema.createFixed("decimal_9_4", null, null, 4))
);
代码示例来源:origin: apache/nifi
final LogicalTypes.Decimal decimal = LogicalTypes.decimal(decimalPrecision, decimalScale);
addNullableField(builder, columnName,
u -> u.type(decimal.addToSchema(SchemaBuilder.builder().bytesType())));
? u.type(LogicalTypes.date().addToSchema(SchemaBuilder.builder().intType()))
: u.stringType());
break;
addNullableField(builder, columnName,
u -> options.useLogicalTypes
? u.type(LogicalTypes.timeMillis().addToSchema(SchemaBuilder.builder().intType()))
: u.stringType());
break;
addNullableField(builder, columnName,
u -> options.useLogicalTypes
? u.type(LogicalTypes.timestampMillis().addToSchema(SchemaBuilder.builder().longType()))
: u.stringType());
break;
代码示例来源:origin: apache/flink
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.");
}
}
代码示例来源:origin: apache/flink
private int convertFromDate(Schema schema, Date date) {
final LogicalType logicalType = schema.getLogicalType();
if (logicalType == LogicalTypes.date()) {
// adopted from Apache Calcite
final long time = date.getTime();
final long converted = time + (long) LOCAL_TZ.getOffset(time);
return (int) (converted / 86400000L);
} else {
throw new RuntimeException("Unsupported date type.");
}
}
代码示例来源:origin: apache/flink
private int convertFromTime(Schema schema, Time date) {
final LogicalType logicalType = schema.getLogicalType();
if (logicalType == LogicalTypes.timeMillis()) {
// adopted from Apache Calcite
final long time = date.getTime();
final long converted = time + (long) LOCAL_TZ.getOffset(time);
return (int) (converted % 86400000L);
} else {
throw new RuntimeException("Unsupported time type.");
}
}
代码示例来源:origin: GoogleCloudPlatform/DataflowTemplates
private Optional<Timestamp> readTimestamp(
GenericRecord record, Schema.Type avroType, LogicalType logicalType, String fieldName) {
switch (avroType) {
case LONG:
if (LogicalTypes.timestampMillis().equals(logicalType)) {
return Optional.ofNullable((Long) record.get(fieldName))
.map(x -> Timestamp.ofTimeMicroseconds(1000L * x));
}
if (LogicalTypes.timestampMicros().equals(logicalType)) {
return Optional.ofNullable((Long) record.get(fieldName))
.map(Timestamp::ofTimeMicroseconds);
}
// Default to micro-seconds.
return Optional.ofNullable((Long) record.get(fieldName)).map(Timestamp::ofTimeMicroseconds);
case STRING:
return Optional.ofNullable((Utf8) record.get(fieldName))
.map(Utf8::toString)
.map(Timestamp::parseTimestamp);
default:
throw new IllegalArgumentException("Cannot interpret " + avroType + " as TIMESTAMP");
}
}
代码示例来源:origin: apache/avro
@Override
public Schema getRecommendedSchema() {
return LogicalTypes.timestampMicros().addToSchema(Schema.create(Schema.Type.LONG));
}
}
代码示例来源:origin: apache/avro
@Override
public Schema getRecommendedSchema() {
return LogicalTypes.timeMicros().addToSchema(Schema.create(Schema.Type.LONG));
}
}
代码示例来源:origin: apache/avro
@Override
public Schema getRecommendedSchema() {
return LogicalTypes.uuid().addToSchema(Schema.create(Schema.Type.STRING));
}
代码示例来源:origin: apache/avro
@Test
public void testTimeMicrosConversion() throws Exception {
TimeMicrosConversion conversion = new TimeMicrosConversion();
LocalTime oneAM = new LocalTime(1, 0);
LocalTime afternoon = new LocalTime(15, 14, 15, 926);
long afternoonMicros = ((long) (15 * 60 + 14) * 60 + 15) * 1000000 + 926551;
Assert.assertEquals("Midnight should be 0",
LocalTime.MIDNIGHT,
conversion.fromLong(0L, TIME_MICROS_SCHEMA, LogicalTypes.timeMicros()));
Assert.assertEquals("01:00 should be 3,600,000,000",
oneAM,
conversion.fromLong(
3600000000L, TIME_MICROS_SCHEMA, LogicalTypes.timeMicros()));
Assert.assertEquals("15:14:15.926000 should be " + afternoonMicros,
afternoon,
conversion.fromLong(
afternoonMicros, TIME_MICROS_SCHEMA, LogicalTypes.timeMicros()));
try {
conversion.toLong(afternoon,
TIMESTAMP_MICROS_SCHEMA, LogicalTypes.timestampMicros());
Assert.fail("Should not convert LocalTime to long");
} catch (UnsupportedOperationException e) {
// expected
}
}
代码示例来源:origin: apache/avro
result.logicalType = LogicalTypes.fromSchemaIgnoreInvalid(result);
代码示例来源:origin: apache/nifi
case DATE:
schema = Schema.create(Type.INT);
LogicalTypes.date().addToSchema(schema);
break;
case DOUBLE:
case TIME:
schema = Schema.create(Type.INT);
LogicalTypes.timeMillis().addToSchema(schema);
break;
case TIMESTAMP:
schema = Schema.create(Type.LONG);
LogicalTypes.timestampMillis().addToSchema(schema);
break;
default:
代码示例来源:origin: org.talend.components/components-snowflake-runtime
protected Object getFieldValue(Object inputValue, Field field) {
Schema s = AvroUtils.unwrapIfNullable(field.schema());
if (inputValue != null && inputValue instanceof String && ((String) inputValue).isEmpty()) {
return emptyStringValue;
} else if (null == inputValue || inputValue instanceof String) {
return inputValue;
} else if (AvroUtils.isSameType(s, AvroUtils._date())) {
Date date = (Date) inputValue;
return date.getTime();
} else 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;
}
}
代码示例来源:origin: Netflix/iceberg
optionalField(23, "float", Schema.create(Schema.Type.FLOAT)),
optionalField(24, "double", Schema.create(Schema.Type.DOUBLE)),
optionalField(25, "date", LogicalTypes.date().addToSchema(Schema.create(Schema.Type.INT))),
optionalField(27, "time", LogicalTypes.timeMicros().addToSchema(Schema.create(Schema.Type.LONG))),
optionalField(28, "timestamptz", addAdjustToUtc(LogicalTypes.timestampMicros().addToSchema(Schema.create(Schema.Type.LONG)), true)),
optionalField(29, "timestamp", addAdjustToUtc(LogicalTypes.timestampMicros().addToSchema(Schema.create(Schema.Type.LONG)), false)),
optionalField(30, "string", Schema.create(Schema.Type.STRING)),
optionalField(31, "uuid", LogicalTypes.uuid().addToSchema(Schema.createFixed("uuid_fixed", null, null, 16))),
optionalField(32, "fixed", Schema.createFixed("fixed_16", null, null, 16)),
optionalField(33, "binary", Schema.create(Schema.Type.BYTES)),
optionalField(34, "decimal", LogicalTypes.decimal(14, 2).addToSchema(Schema.createFixed("decimal_14_2", null, null, 6)))
);
内容来源于网络,如有侵权,请联系作者删除!