我有10~ mysql表和20~ DATETIME字段。并尝试将表从mysql迁移到google bigquery。
在这个过程中,我碰巧使用了mysql -connector-j8.0.x,据我所知,mysql DATETIME并不了解时区。而bigquery的TIMESTAMP将时间戳保存为UTC。
所以我应该用时区信息来改变mysql的DATETIME。
我猜这会由SqlTimestampValueFactory来处理。我的猜测正确吗?如果这是真的,我如何定制SqlTimestampValueFactory的tz
发布于 2018-07-24 16:29:15
您可以通过设置serverTimezone属性来自定义SqlTimestampValueFactory的Tz值。
即使mysql DATETIME数据类型没有时区信息。时区假设由服务器变量time_zone完成。mysql-connector-j使用ServerSession.getDefaultTimezone()通过TimestampValueFactory处理转换。
因此,在我的例子中,serverTimezone属性设置就足够了。
TL;DR
在ResultSetImpl处初始化SqlTimestampValueFactory。
# com.mysql.cj.jdbc.result.ResultSetImpl#ResultSetImpl
this.defaultTimestampValueFactory =
decorateDateTimeValueFactory(
new SqlTimestampValueFactory(
this.session.getServerSession().getDefaultTimeZone()),
this.zeroDateTimeBehavior);NativeProtocol正在初始化defaultTimeZone
String canonicalTimezone = getPropertySet().getStringReadableProperty(PropertyDefinitions.PNAME_serverTimezone).getValue();
...
...
if (canonicalTimezone != null && canonicalTimezone.length() > 0) {
this.serverSession.setServerTimeZone(TimeZone.getTimeZone(canonicalTimezone));
...https://stackoverflow.com/questions/51493163
复制相似问题