|
| 1 | +/* |
| 2 | + * Copyright (c) 2020. Hans-Peter Grahsl (grahslhp@gmail.com) |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + */ |
| 17 | + |
| 18 | +package com.github.hpgrahsl.ksqldb.functions; |
| 19 | + |
| 20 | +import com.github.hpgrahsl.ksqldb.functions.schemas.DateTimeSchemas; |
| 21 | +import com.github.hpgrahsl.ksqldb.functions.structs.StructsConverter; |
| 22 | +import io.confluent.ksql.function.udf.Udf; |
| 23 | +import io.confluent.ksql.function.udf.UdfDescription; |
| 24 | +import io.confluent.ksql.function.udf.UdfParameter; |
| 25 | +import java.time.OffsetDateTime; |
| 26 | +import java.time.format.DateTimeFormatter; |
| 27 | +import java.util.Locale; |
| 28 | +import org.apache.kafka.connect.data.Struct; |
| 29 | + |
| 30 | +@UdfDescription( |
| 31 | + name = "dt_offsetdatetime", |
| 32 | + description = "Factory functions for OffsetDateTime struct creation", |
| 33 | + author = "Hans-Peter Grahsl (follow @hpgrahsl)", |
| 34 | + version = "0.1.0" |
| 35 | + ) |
| 36 | +public class UdfOffsetDateTime { |
| 37 | + |
| 38 | + @Udf(description = "Create an OffsetDateTime struct based on the system clock's current date-time and default time-zone", |
| 39 | + schema = DateTimeSchemas.OFFSETDATETIME_SCHEMA_DESCRIPTOR) |
| 40 | + public Struct createOffsetDateTime() { |
| 41 | + return StructsConverter.toOffsetDateTimeStruct(OffsetDateTime.now()); |
| 42 | + } |
| 43 | + |
| 44 | + @Udf(description = "Create an OffsetDateTime struct based on the system clock's date-time in the specified time-zone", |
| 45 | + schema = DateTimeSchemas.OFFSETDATETIME_SCHEMA_DESCRIPTOR) |
| 46 | + public Struct createOffsetDateTime( |
| 47 | + @UdfParameter( |
| 48 | + value = "zoneId", |
| 49 | + description = "the ZoneId struct for the OffsetDateTime", |
| 50 | + schema = DateTimeSchemas.ZONEID_SCHEMA_DESCRIPTOR) |
| 51 | + final Struct zoneId) { |
| 52 | + return zoneId != null ? StructsConverter.toOffsetDateTimeStruct(OffsetDateTime.now(StructsConverter.fromZoneIdStruct(zoneId))) : null; |
| 53 | + } |
| 54 | + |
| 55 | + @Udf(description = "Create an OffsetDateTime struct based on given LocalDateTime and ZoneOffset structs", |
| 56 | + schema = DateTimeSchemas.OFFSETDATETIME_SCHEMA_DESCRIPTOR) |
| 57 | + public Struct createOffsetDateTime( |
| 58 | + @UdfParameter( |
| 59 | + value = "localDateTime", |
| 60 | + description = "the LocalDateTime struct for the OffsetDateTime", |
| 61 | + schema = DateTimeSchemas.LOCALDATETIME_SCHEMA_DESCRIPTOR) |
| 62 | + final Struct localDateTime, |
| 63 | + @UdfParameter( |
| 64 | + value = "zoneOffset", |
| 65 | + description = "the ZoneOffset struct for the OffsetDateTime", |
| 66 | + schema = DateTimeSchemas.ZONEOFFSET_SCHEMA_DESCRIPTOR) |
| 67 | + final Struct zoneOffset) { |
| 68 | + if (localDateTime == null || zoneOffset == null) |
| 69 | + return null; |
| 70 | + return StructsConverter.toOffsetDateTimeStruct(OffsetDateTime.of( |
| 71 | + StructsConverter.fromLocalDateTimeStruct(localDateTime), |
| 72 | + StructsConverter.fromZoneOffsetStruct(zoneOffset) |
| 73 | + )); |
| 74 | + } |
| 75 | + |
| 76 | + @Udf(description = "Create an OffsetDateTime struct based on given LocalDate, LocalTime and ZoneOffset structs", |
| 77 | + schema = DateTimeSchemas.OFFSETDATETIME_SCHEMA_DESCRIPTOR) |
| 78 | + public Struct createOffsetDateTime( |
| 79 | + @UdfParameter( |
| 80 | + value = "localDate", |
| 81 | + description = "the LocalDate struct for the OffsetDateTime", |
| 82 | + schema = DateTimeSchemas.LOCALDATE_SCHEMA_DESCRIPTOR) |
| 83 | + final Struct localDate, |
| 84 | + @UdfParameter( |
| 85 | + value = "localTime", |
| 86 | + description = "the LocalTime struct for the OffsetDateTime", |
| 87 | + schema = DateTimeSchemas.LOCALTIME_SCHEMA_DESCRIPTOR) |
| 88 | + final Struct localTime, |
| 89 | + @UdfParameter( |
| 90 | + value = "zoneOffset", |
| 91 | + description = "the ZoneOffset struct for the OffsetDateTime", |
| 92 | + schema = DateTimeSchemas.ZONEOFFSET_SCHEMA_DESCRIPTOR) |
| 93 | + final Struct zoneOffset) { |
| 94 | + if (localDate == null || localTime == null || zoneOffset == null) |
| 95 | + return null; |
| 96 | + return StructsConverter.toOffsetDateTimeStruct( |
| 97 | + OffsetDateTime.of( |
| 98 | + StructsConverter.fromLocalDateStruct(localDate), |
| 99 | + StructsConverter.fromLocalTimeStruct(localTime), |
| 100 | + StructsConverter.fromZoneOffsetStruct(zoneOffset)) |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + @Udf(description = "Create an OffsetDateTime struct from its string representation using java.time.format.DateTimeFormatter#ISO_OFFSET_DATE_TIME", |
| 105 | + schema = DateTimeSchemas.OFFSETDATETIME_SCHEMA_DESCRIPTOR) |
| 106 | + public Struct createOffsetDateTime( |
| 107 | + @UdfParameter( |
| 108 | + value = "text", |
| 109 | + description = "the string representation of the OffsetDateTime") |
| 110 | + final String text) { |
| 111 | + return text != null ? StructsConverter.toOffsetDateTimeStruct(OffsetDateTime.parse(text)) : null; |
| 112 | + } |
| 113 | + |
| 114 | + @Udf(description = "Create an OffsetDateTime struct from its string representation using the specified java.time.format.DateTimeFormatter format string", |
| 115 | + schema = DateTimeSchemas.OFFSETDATETIME_SCHEMA_DESCRIPTOR) |
| 116 | + public Struct createOffsetDateTime( |
| 117 | + @UdfParameter( |
| 118 | + value = "text", |
| 119 | + description = "the string representation of the OffsetDateTime") |
| 120 | + final String text, |
| 121 | + @UdfParameter( |
| 122 | + value = "format", |
| 123 | + description = "the specified java.time.format.DateTimeFormatter format string") |
| 124 | + final String format) { |
| 125 | + if (text == null || format == null) |
| 126 | + return null; |
| 127 | + return StructsConverter.toOffsetDateTimeStruct(OffsetDateTime.parse(text, DateTimeFormatter.ofPattern(format, |
| 128 | + Locale.ENGLISH))); |
| 129 | + } |
| 130 | + |
| 131 | +} |
0 commit comments