org.apache.avro.data.TimeConversions Java Examples

The following examples show how to use org.apache.avro.data.TimeConversions. 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: TestStringLiteralConversions.java    From iceberg with Apache License 2.0 6 votes vote down vote up
@Test
public void testStringToTimeLiteral() {
  // use Avro's time conversion to validate the result
  Schema avroSchema = LogicalTypes.timeMicros().addToSchema(Schema.create(Schema.Type.LONG));
  TimeConversions.LossyTimeMicrosConversion avroConversion =
      new TimeConversions.LossyTimeMicrosConversion();

  Literal<CharSequence> timeStr = Literal.of("14:21:01.919");
  Literal<Long> time = timeStr.to(Types.TimeType.get());

  long avroValue = avroConversion.toLong(
      new LocalTime(14, 21, 1, 919),
      avroSchema, avroSchema.getLogicalType());

  Assert.assertEquals("Time should match", avroValue, (long) time.value());
}
 
Example #2
Source File: AvroSchema.java    From pulsar with Apache License 2.0 6 votes vote down vote up
public static void addLogicalTypeConversions(ReflectData reflectData, boolean jsr310ConversionEnabled) {
    reflectData.addLogicalTypeConversion(new Conversions.DecimalConversion());
    reflectData.addLogicalTypeConversion(new TimeConversions.DateConversion());
    reflectData.addLogicalTypeConversion(new TimeConversions.TimeMillisConversion());
    reflectData.addLogicalTypeConversion(new TimeConversions.TimeMicrosConversion());
    reflectData.addLogicalTypeConversion(new TimeConversions.TimestampMicrosConversion());
    if (jsr310ConversionEnabled) {
        reflectData.addLogicalTypeConversion(new TimeConversions.TimestampMillisConversion());
    } else {
        try {
            Class.forName("org.joda.time.DateTime");
            reflectData.addLogicalTypeConversion(new JodaTimeConversions.TimestampConversion());
        } catch (ClassNotFoundException e) {
            // Skip if have not provide joda-time dependency.
        }
    }
}
 
Example #3
Source File: TestStringLiteralConversions.java    From iceberg with Apache License 2.0 5 votes vote down vote up
@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(
      LocalDate.of(2017, 8, 18),
      avroSchema, avroSchema.getLogicalType());

  Assert.assertEquals("Date should match", avroValue, (int) date.value());
}
 
Example #4
Source File: TestStringLiteralConversions.java    From iceberg with Apache License 2.0 5 votes vote down vote up
@Test
public void testStringToTimeLiteral() {
  // use Avro's time conversion to validate the result
  Schema avroSchema = LogicalTypes.timeMicros().addToSchema(Schema.create(Schema.Type.LONG));

  Literal<CharSequence> timeStr = Literal.of("14:21:01.919");
  Literal<Long> time = timeStr.to(Types.TimeType.get());

  long avroValue = new TimeConversions.TimeMicrosConversion().toLong(
      LocalTime.of(14, 21, 1, 919 * 1000000),
      avroSchema, avroSchema.getLogicalType());

  Assert.assertEquals("Time should match", avroValue, (long) time.value());
}
 
Example #5
Source File: TestStringLiteralConversions.java    From iceberg with Apache License 2.0 5 votes vote down vote up
@Test
public void testStringToTimestampLiteral() {
  // use Avro's timestamp conversion to validate the result
  Schema avroSchema = LogicalTypes.timestampMicros().addToSchema(Schema.create(Schema.Type.LONG));
  TimeConversions.TimestampMicrosConversion avroConversion =
      new TimeConversions.TimestampMicrosConversion();

  // Timestamp with explicit UTC offset, +00:00
  Literal<CharSequence> timestampStr = Literal.of("2017-08-18T14:21:01.919+00:00");
  Literal<Long> timestamp = timestampStr.to(Types.TimestampType.withZone());
  long avroValue = avroConversion.toLong(
      LocalDateTime.of(2017, 8, 18, 14, 21, 1, 919 * 1000000).toInstant(ZoneOffset.UTC),
      avroSchema, avroSchema.getLogicalType());

  Assert.assertEquals("Timestamp should match", avroValue, (long) timestamp.value());

  // Timestamp without an explicit zone should be UTC (equal to the previous converted value)
  timestampStr = Literal.of("2017-08-18T14:21:01.919");
  timestamp = timestampStr.to(Types.TimestampType.withoutZone());

  Assert.assertEquals("Timestamp without zone should match UTC",
      avroValue, (long) timestamp.value());

  // Timestamp with an explicit offset should be adjusted to UTC
  timestampStr = Literal.of("2017-08-18T14:21:01.919-07:00");
  timestamp = timestampStr.to(Types.TimestampType.withZone());
  avroValue = avroConversion.toLong(
      LocalDateTime.of(2017, 8, 18, 21, 21, 1, 919 * 1000000).toInstant(ZoneOffset.UTC),
      avroSchema, avroSchema.getLogicalType());

  Assert.assertEquals("Timestamp without zone should match UTC",
      avroValue, (long) timestamp.value());
}
 
Example #6
Source File: TestStringLiteralConversions.java    From iceberg with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example #7
Source File: TestStringLiteralConversions.java    From iceberg with Apache License 2.0 5 votes vote down vote up
@Test
public void testStringToTimestampLiteral() {
  // use Avro's timestamp conversion to validate the result
  Schema avroSchema = LogicalTypes.timestampMicros().addToSchema(Schema.create(Schema.Type.LONG));
  TimeConversions.LossyTimestampMicrosConversion avroConversion =
      new TimeConversions.LossyTimestampMicrosConversion();

  // Timestamp with explicit UTC offset, +00:00
  Literal<CharSequence> timestampStr = Literal.of("2017-08-18T14:21:01.919+00:00");
  Literal<Long> timestamp = timestampStr.to(Types.TimestampType.withZone());
  long avroValue = avroConversion.toLong(
      new LocalDateTime(2017, 8, 18, 14, 21, 1, 919).toDateTime(DateTimeZone.UTC),
      avroSchema, avroSchema.getLogicalType());

  Assert.assertEquals("Timestamp should match", avroValue, (long) timestamp.value());

  // Timestamp without an explicit zone should be UTC (equal to the previous converted value)
  timestampStr = Literal.of("2017-08-18T14:21:01.919");
  timestamp = timestampStr.to(Types.TimestampType.withoutZone());

  Assert.assertEquals("Timestamp without zone should match UTC",
      avroValue, (long) timestamp.value());

  // Timestamp with an explicit offset should be adjusted to UTC
  timestampStr = Literal.of("2017-08-18T14:21:01.919-07:00");
  timestamp = timestampStr.to(Types.TimestampType.withZone());
  avroValue = avroConversion.toLong(
      new LocalDateTime(2017, 8, 18, 21, 21, 1, 919).toDateTime(DateTimeZone.UTC),
      avroSchema, avroSchema.getLogicalType());

  Assert.assertEquals("Timestamp without zone should match UTC",
      avroValue, (long) timestamp.value());
}