-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improvement: FlinkBaseTypeInfoRegister mechanism
- Loading branch information
Showing
6 changed files
with
121 additions
and
25 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
109 changes: 109 additions & 0 deletions
109
...cala/pl/touk/nussknacker/engine/flink/api/typeinformation/FlinkBaseTypeInfoRegister.scala
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,109 @@ | ||
package pl.touk.nussknacker.engine.flink.api.typeinformation | ||
|
||
import org.apache.flink.api.common.typeinfo.{TypeInfoFactory, TypeInformation, Types} | ||
import org.apache.flink.api.java.typeutils.TypeExtractor | ||
|
||
import java.lang.reflect.Type | ||
import java.sql.{Date => SqlDate, Time => SqlTime, Timestamp => SqlTimestamp} | ||
import java.time.{Instant, LocalDate, LocalDateTime, LocalTime} | ||
import java.util | ||
|
||
object FlinkBaseTypeInfoRegister { | ||
|
||
private case class Base[T](klass: Class[T], factory: TypeInfoFactory[T]) { | ||
lazy val factoryClass: Class[_ <: TypeInfoFactory[T]] = factory.getClass | ||
} | ||
|
||
private val baseTypes = List( | ||
Base(classOf[LocalDate], new LocalDateTypeFactory), | ||
Base(classOf[LocalTime], new LocalTimeTypeFactory), | ||
Base(classOf[LocalDateTime], new LocalDateTimeTypeFactory), | ||
Base(classOf[Instant], new InstantTypeFactory), | ||
Base(classOf[SqlDate], new SqlDateTypeFactory), | ||
Base(classOf[SqlTime], new SqlTimeTypeFactory), | ||
Base(classOf[SqlTimestamp], new SqlTimestampTypeFactory), | ||
) | ||
|
||
def makeSureBaseTypesAreRegistered(): Unit = | ||
baseTypes.foreach { base => | ||
register(base) | ||
} | ||
|
||
private def register(base: Base[_]): Unit = { | ||
val opt = Option(TypeExtractor.getTypeInfoFactory(base.klass)) | ||
if (opt.isEmpty) { | ||
TypeExtractor.registerFactory(base.klass, base.factoryClass) | ||
} | ||
} | ||
|
||
class LocalDateTypeFactory extends TypeInfoFactory[LocalDate] { | ||
|
||
override def createTypeInfo( | ||
t: Type, | ||
genericParameters: util.Map[String, TypeInformation[_]] | ||
): TypeInformation[LocalDate] = | ||
Types.LOCAL_DATE | ||
|
||
} | ||
|
||
class LocalTimeTypeFactory extends TypeInfoFactory[LocalTime] { | ||
|
||
override def createTypeInfo( | ||
t: Type, | ||
genericParameters: util.Map[String, TypeInformation[_]] | ||
): TypeInformation[LocalTime] = | ||
Types.LOCAL_TIME | ||
|
||
} | ||
|
||
class LocalDateTimeTypeFactory extends TypeInfoFactory[LocalDateTime] { | ||
|
||
override def createTypeInfo( | ||
t: Type, | ||
genericParameters: util.Map[String, TypeInformation[_]] | ||
): TypeInformation[LocalDateTime] = | ||
Types.LOCAL_DATE_TIME | ||
|
||
} | ||
|
||
class InstantTypeFactory extends TypeInfoFactory[Instant] { | ||
|
||
override def createTypeInfo( | ||
t: Type, | ||
genericParameters: util.Map[String, TypeInformation[_]] | ||
): TypeInformation[Instant] = | ||
Types.INSTANT | ||
|
||
} | ||
|
||
class SqlDateTypeFactory extends TypeInfoFactory[SqlDate] { | ||
|
||
override def createTypeInfo( | ||
t: Type, | ||
genericParameters: util.Map[String, TypeInformation[_]] | ||
): TypeInformation[SqlDate] = | ||
Types.SQL_DATE | ||
|
||
} | ||
|
||
class SqlTimeTypeFactory extends TypeInfoFactory[SqlTime] { | ||
|
||
override def createTypeInfo( | ||
t: Type, | ||
genericParameters: util.Map[String, TypeInformation[_]] | ||
): TypeInformation[SqlTime] = | ||
Types.SQL_TIME | ||
|
||
} | ||
|
||
class SqlTimestampTypeFactory extends TypeInfoFactory[SqlTimestamp] { | ||
|
||
override def createTypeInfo( | ||
t: Type, | ||
genericParameters: util.Map[String, TypeInformation[_]] | ||
): TypeInformation[SqlTimestamp] = | ||
Types.SQL_TIMESTAMP | ||
|
||
} | ||
|
||
} |
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
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