org.apache.avro.LogicalTypes.date()方法的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(8.6k)|赞(0)|评价(0)|浏览(99)

本文整理了Java中org.apache.avro.LogicalTypes.date()方法的一些代码示例,展示了LogicalTypes.date()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。LogicalTypes.date()方法的具体详情如下:
包路径:org.apache.avro.LogicalTypes
类名称:LogicalTypes
方法名:date

LogicalTypes.date介绍

暂无

代码示例

代码示例来源: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/avro

@Override
 public Schema getRecommendedSchema() {
  return LogicalTypes.date().addToSchema(Schema.create(Schema.Type.INT));
 }
}

代码示例来源:origin: apache/avro

@Override
 public Schema getRecommendedSchema() {
  return LogicalTypes.date().addToSchema(Schema.create(Schema.Type.INT));
 }
}

代码示例来源:origin: apache/flink

if (logicalType == LogicalTypes.date()) {
  return Types.SQL_DATE;
} else if (logicalType == LogicalTypes.timeMillis()) {

代码示例来源:origin: apache/avro

@Test
public void testDateConversion() throws Exception {
 DateConversion conversion = new DateConversion();
 LocalDate Jan_6_1970 = new LocalDate(1970, 1, 6);    //  5
 LocalDate Jan_1_1970 = new LocalDate(1970, 1, 1);    //  0
 LocalDate Dec_27_1969 = new LocalDate(1969, 12, 27); // -5
 Assert.assertEquals("6 Jan 1970 should be 5", 5,
   (int) conversion.toInt(Jan_6_1970, DATE_SCHEMA, LogicalTypes.date()));
 Assert.assertEquals("1 Jan 1970 should be 0", 0,
   (int) conversion.toInt(Jan_1_1970, DATE_SCHEMA, LogicalTypes.date()));
 Assert.assertEquals("27 Dec 1969 should be -5", -5,
   (int) conversion.toInt(Dec_27_1969, DATE_SCHEMA, LogicalTypes.date()));
 Assert.assertEquals("6 Jan 1970 should be 5",
   conversion.fromInt(5, DATE_SCHEMA, LogicalTypes.date()), Jan_6_1970);
 Assert.assertEquals("1 Jan 1970 should be 0",
   conversion.fromInt(0, DATE_SCHEMA, LogicalTypes.date()), Jan_1_1970);
 Assert.assertEquals("27 Dec 1969 should be -5",
   conversion.fromInt(-5, DATE_SCHEMA, LogicalTypes.date()), Dec_27_1969);
}

代码示例来源:origin: apache/avro

@Test
public void testDateConversion() throws Exception {
 DateConversion conversion = new DateConversion();
 LocalDate Jan_6_1970 = LocalDate.of(1970, 1, 6);    //  5
 LocalDate Jan_1_1970 = LocalDate.of(1970, 1, 1);    //  0
 LocalDate Dec_27_1969 = LocalDate.of(1969, 12, 27); // -5
 Assert.assertEquals("6 Jan 1970 should be 5", 5,
   (int) conversion.toInt(Jan_6_1970, DATE_SCHEMA, LogicalTypes.date()));
 Assert.assertEquals("1 Jan 1970 should be 0", 0,
   (int) conversion.toInt(Jan_1_1970, DATE_SCHEMA, LogicalTypes.date()));
 Assert.assertEquals("27 Dec 1969 should be -5", -5,
   (int) conversion.toInt(Dec_27_1969, DATE_SCHEMA, LogicalTypes.date()));
 Assert.assertEquals("6 Jan 1970 should be 5",
   conversion.fromInt(5, DATE_SCHEMA, LogicalTypes.date()), Jan_6_1970);
 Assert.assertEquals("1 Jan 1970 should be 0",
   conversion.fromInt(0, DATE_SCHEMA, LogicalTypes.date()), Jan_1_1970);
 Assert.assertEquals("27 Dec 1969 should be -5",
   conversion.fromInt(-5, DATE_SCHEMA, LogicalTypes.date()), Dec_27_1969);
}

代码示例来源:origin: confluentinc/ksql

@Test
public void shouldDeserializeDateToBigint() {
 shouldDeserializeTypeCorrectly(
   LogicalTypes.date().addToSchema(
     org.apache.avro.SchemaBuilder.builder().intType()),
   ChronoUnit.DAYS.between(LocalDate.ofEpochDay(0), LocalDate.now()),
   Schema.OPTIONAL_INT64_SCHEMA
 );
}

代码示例来源:origin: confluentinc/ksql

@Test
public void shouldDeserializeDateToInteger() {
 shouldDeserializeTypeCorrectly(
   LogicalTypes.date().addToSchema(
     org.apache.avro.SchemaBuilder.builder().intType()),
   (int) ChronoUnit.DAYS.between(LocalDate.ofEpochDay(0), LocalDate.now()),
   Schema.OPTIONAL_INT32_SCHEMA
 );
}

代码示例来源:origin: apache/nifi

case DATE:
  schema = Schema.create(Type.INT);
  LogicalTypes.date().addToSchema(schema);
  break;
case DOUBLE:

代码示例来源: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

@BeforeClass
public static void createSchemas() {
 TestTimeConversions.DATE_SCHEMA = LogicalTypes.date()
   .addToSchema(Schema.create(Schema.Type.INT));
 TestTimeConversions.TIME_MILLIS_SCHEMA = LogicalTypes.timeMillis()
   .addToSchema(Schema.create(Schema.Type.INT));
 TestTimeConversions.TIME_MICROS_SCHEMA = LogicalTypes.timeMicros()
   .addToSchema(Schema.create(Schema.Type.LONG));
 TestTimeConversions.TIMESTAMP_MILLIS_SCHEMA = LogicalTypes.timestampMillis()
   .addToSchema(Schema.create(Schema.Type.LONG));
 TestTimeConversions.TIMESTAMP_MICROS_SCHEMA = LogicalTypes.timestampMicros()
   .addToSchema(Schema.create(Schema.Type.LONG));
}

代码示例来源:origin: apache/nifi

? u.type(LogicalTypes.date().addToSchema(SchemaBuilder.builder().intType()))
        : u.stringType());
break;

代码示例来源:origin: apache/avro

case DATE:
 jj_consume_token(DATE);
    {if (true) return LogicalTypes.date().addToSchema(Schema.create(Type.INT));}
 break;
case TIME:

代码示例来源:origin: org.talend.daikon/daikon

/**
 * Checks whether specified schema has logical date type
 * Its type should be INT and
 * Its logical type should be "date"
 * 
 * @param schema avro schema
 * @return true, if schema has logical date type
 */
public static boolean isLogicalDate(Schema schema) {
  LogicalType logicalType = schema.getLogicalType();
  return Type.INT == schema.getType() && LogicalTypes.date().equals(logicalType);
}

代码示例来源:origin: org.apache.flink/flink-avro

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: com.alibaba.blink/flink-avro

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: org.talend.daikon/daikon

/**
 * Returns schema for Avro Date logical type. It's Avro type is Int
 * 
 * @return Date logical type schema
 */
public static Schema _logicalDate() {
  return LogicalTypes.date().addToSchema(Schema.create(Schema.Type.INT));
}

代码示例来源: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: 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

@Test
public void testStringToDateLiteral() {
 Literal<CharSequence> dateStr = Literal.of("2017-08-18");
 Literal<Integer> date = dateStr.to(Types.DateType.get());
 // use Avro's date conversion to validate the result
 Schema avroSchema = LogicalTypes.date().addToSchema(Schema.create(Schema.Type.INT));
 TimeConversions.DateConversion avroConversion = new TimeConversions.DateConversion();
 int avroValue = avroConversion.toInt(
   new LocalDate(2017, 8, 18),
   avroSchema, avroSchema.getLogicalType());
 Assert.assertEquals("Date should match", avroValue, (int) date.value());
}

相关文章