-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support temporal types in Kafka JSON encoder
- Loading branch information
1 parent
5634658
commit 0893602
Showing
11 changed files
with
746 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
.../src/main/java/io/prestosql/plugin/kafka/encoder/json/format/CustomDateTimeFormatter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.prestosql.plugin.kafka.encoder.json.format; | ||
|
||
import io.prestosql.spi.type.SqlDate; | ||
import io.prestosql.spi.type.SqlTime; | ||
import io.prestosql.spi.type.SqlTimeWithTimeZone; | ||
import io.prestosql.spi.type.SqlTimestamp; | ||
import io.prestosql.spi.type.SqlTimestampWithTimeZone; | ||
import io.prestosql.spi.type.Type; | ||
import org.joda.time.DateTime; | ||
import org.joda.time.DateTimeZone; | ||
import org.joda.time.LocalTime; | ||
import org.joda.time.chrono.ISOChronology; | ||
import org.joda.time.format.DateTimeFormat; | ||
import org.joda.time.format.DateTimeFormatter; | ||
|
||
import java.util.Locale; | ||
import java.util.Optional; | ||
import java.util.TimeZone; | ||
|
||
import static io.prestosql.plugin.kafka.encoder.json.format.util.TimeConversions.PICOSECONDS_PER_SECOND; | ||
import static io.prestosql.plugin.kafka.encoder.json.format.util.TimeConversions.getMillisOfDay; | ||
import static io.prestosql.plugin.kafka.encoder.json.format.util.TimeConversions.scaleEpochMicrosToMillis; | ||
import static io.prestosql.plugin.kafka.encoder.json.format.util.TimeConversions.scalePicosToMillis; | ||
import static io.prestosql.spi.type.DateType.DATE; | ||
import static io.prestosql.spi.type.TimeType.TIME_MILLIS; | ||
import static io.prestosql.spi.type.TimeWithTimeZoneType.TIME_WITH_TIME_ZONE; | ||
import static io.prestosql.spi.type.TimestampType.TIMESTAMP_MILLIS; | ||
import static io.prestosql.spi.type.TimestampWithTimeZoneType.TIMESTAMP_TZ_MILLIS; | ||
import static java.util.concurrent.TimeUnit.DAYS; | ||
import static org.joda.time.DateTimeZone.UTC; | ||
|
||
public class CustomDateTimeFormatter | ||
implements JsonDateTimeFormatter | ||
{ | ||
private final DateTimeFormatter formatter; | ||
|
||
public static boolean isSupportedType(Type type) | ||
{ | ||
return type.equals(DATE) || | ||
type.equals(TIME_MILLIS) || | ||
type.equals(TIME_WITH_TIME_ZONE) || | ||
type.equals(TIMESTAMP_MILLIS) || | ||
type.equals(TIMESTAMP_TZ_MILLIS); | ||
} | ||
|
||
public CustomDateTimeFormatter(Optional<String> pattern) | ||
{ | ||
this.formatter = DateTimeFormat.forPattern(getPattern(pattern)) | ||
.withLocale(Locale.ENGLISH) | ||
.withChronology(ISOChronology.getInstanceUTC()); | ||
} | ||
|
||
private static String getPattern(Optional<String> pattern) | ||
{ | ||
return pattern.orElseThrow(() -> new IllegalArgumentException("No pattern defined for custom date time format")); | ||
} | ||
|
||
@Override | ||
public String formatDate(SqlDate value) | ||
{ | ||
return formatter.withZoneUTC().print(new DateTime(DAYS.toMillis(value.getDays()))); | ||
} | ||
|
||
@Override | ||
public String formatTime(SqlTime value, int precision) | ||
{ | ||
return formatter.withZoneUTC().print(LocalTime.fromMillisOfDay(getMillisOfDay(scalePicosToMillis(value.getPicos())))); | ||
} | ||
|
||
@Override | ||
public String formatTimeWithZone(SqlTimeWithTimeZone value) | ||
{ | ||
int offsetMinutes = value.getOffsetMinutes(); | ||
DateTimeZone dateTimeZone = DateTimeZone.forOffsetHoursMinutes(offsetMinutes / 60, offsetMinutes % 60); | ||
long picos = value.getPicos() - (offsetMinutes * 60 * PICOSECONDS_PER_SECOND); | ||
return formatter.withZone(dateTimeZone).print(new DateTime(scalePicosToMillis(picos), dateTimeZone)); | ||
} | ||
|
||
@Override | ||
public String formatTimestamp(SqlTimestamp value) | ||
{ | ||
return formatter.withZoneUTC().print(new DateTime(scaleEpochMicrosToMillis(value.getEpochMicros()), UTC)); | ||
} | ||
|
||
@Override | ||
public String formatTimestampWithZone(SqlTimestampWithTimeZone value) | ||
{ | ||
DateTimeZone dateTimeZone = DateTimeZone.forTimeZone(TimeZone.getTimeZone(value.getTimeZoneKey().getZoneId())); | ||
return formatter.withZone(dateTimeZone).print(new DateTime(value.getEpochMillis(), dateTimeZone)); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
presto-kafka/src/main/java/io/prestosql/plugin/kafka/encoder/json/format/DateTimeFormat.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.prestosql.plugin.kafka.encoder.json.format; | ||
|
||
import io.prestosql.spi.type.Type; | ||
|
||
import java.util.Locale; | ||
import java.util.Optional; | ||
import java.util.function.Function; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
public enum DateTimeFormat | ||
{ | ||
CUSTOM_DATE_TIME(CustomDateTimeFormatter::new, CustomDateTimeFormatter::isSupportedType), | ||
ISO8601(pattern -> new ISO8601DateTimeFormatter(), ISO8601DateTimeFormatter::isSupportedType), | ||
RFC2822(pattern -> new RFC2822DateTimeFormatter(), RFC2822DateTimeFormatter::isSupportedType), | ||
MILLISECONDS_SINCE_EPOCH(pattern -> new MillisecondsSinceEpochFormatter(), MillisecondsSinceEpochFormatter::isSupportedType), | ||
SECONDS_SINCE_EPOCH(pattern -> new SecondsSinceEpochFormatter(), SecondsSinceEpochFormatter::isSupportedType); | ||
|
||
private final Function<Optional<String>, JsonDateTimeFormatter> formatterConstructor; | ||
private final Function<Type, Boolean> isSupportedType; | ||
|
||
DateTimeFormat(Function<Optional<String>, JsonDateTimeFormatter> formatterConstructor, Function<Type, Boolean> isSupportedType) | ||
{ | ||
this.formatterConstructor = requireNonNull(formatterConstructor, "formatterConstructor is null"); | ||
this.isSupportedType = requireNonNull(isSupportedType, "isSupportedType is null"); | ||
} | ||
|
||
public boolean isSupportedType(Type type) | ||
{ | ||
return isSupportedType.apply(type); | ||
} | ||
|
||
public JsonDateTimeFormatter getFormatter(Optional<String> pattern) | ||
{ | ||
return formatterConstructor.apply(pattern); | ||
} | ||
|
||
@Override | ||
public String toString() | ||
{ | ||
return name().toLowerCase(Locale.ENGLISH).replaceAll("_", "-"); | ||
} | ||
} |
Oops, something went wrong.