Snowflake cloud data platform 雪花JDBC时间数据类型值转换为UTC

Snowflake cloud data platform 雪花JDBC时间数据类型值转换为UTC,snowflake-cloud-data-platform,Snowflake Cloud Data Platform,我们使用JDBC驱动程序连接到Snowflake并执行插入。 在使用时间数据类型时,我们在insert中使用setTime将时间值设置为10:10:10,在使用getTime检索时,我们得到02:10:10 文档中说-TIME在内部存储“wallclock”时间,所有关于时间值的操作都是在不考虑任何时区的情况下执行的 为什么要进行这种转换?如何确保我们收回插入的内容 示例程序- try (Connection con = getConnection()) { try (PreparedS

我们使用JDBC驱动程序连接到Snowflake并执行插入。 在使用时间数据类型时,我们在insert中使用setTime将时间值设置为10:10:10,在使用getTime检索时,我们得到02:10:10

文档中说-TIME在内部存储“wallclock”时间,所有关于时间值的操作都是在不考虑任何时区的情况下执行的

为什么要进行这种转换?如何确保我们收回插入的内容

示例程序-

try (Connection con = getConnection()) {
    try (PreparedStatement ps = con.prepareStatement("insert into Test_Time values (?)"))  {
        ps.setTime('10:10:10');
        ps.executeUpdate();
    }

    try (Statement statement = con.createStatement()) {
        try (ResultSet resultSet = statement.executeQuery("select * from Test_Time ")) {
            while (resultSet.next()) {
                System.out.println(resultSet.getTime(1));
            }
        }
    }
}
我们两个都试过了-

ps.setTime(Time.valueOf('10:10:10'))
ps.setTime('10:10:10')
在WEB门户工作表中尝试类似操作时,我们会看到相同的插入值10:10:10。Web门户能够显示正确的期望结果,所以想知道我的案例中是否缺少一些基本的东西

提前谢谢你的帮助

更新-
对此做了更多的测试-发现“setTime(intidx,Time t)”和“setTime(intidx,Time t,Calendar c)”之间没有区别。如果您已经知道数据的时区,则无法提供自定义时区

转换由Java完成。如果使用bind字符串变量而不是Time变量,您将看到它按预期工作:

CREATE TABLE test_time ( t TIME );
当我们运行下面的Java块时,它将在表中插入“10:10:10”:

try (Connection con = getConnection()) {
    try (PreparedStatement ps = con.prepareStatement("insert into Test_Time values (?)"))  {
        ps.setString(1, "10:10:10" );
        ps.executeUpdate();
    }

    try (Statement statement = con.createStatement()) {
        try (ResultSet resultSet = statement.executeQuery("select * from Test_Time ")) {
            while (resultSet.next()) {
                System.out.println(resultSet.getString(1));
                System.out.println(resultSet.getTime(1));
            }
        }
    }
}
您将看到以下输出:

10:10:10
02:10:10
第一个是将时间读取为字符串,第二个是使用time对象,因此它将转换为您的时区。如果在web UI上查询该表,您将看到它将返回预期值:

SELECT * FROM test_time;

+----------+
|    T     |
+----------+
| 10:10:10 |
+----------+

至少,它对我有用:)

转换是由Java完成的。如果使用bind字符串变量而不是Time变量,您将看到它按预期工作:

CREATE TABLE test_time ( t TIME );
当我们运行下面的Java块时,它将在表中插入“10:10:10”:

try (Connection con = getConnection()) {
    try (PreparedStatement ps = con.prepareStatement("insert into Test_Time values (?)"))  {
        ps.setString(1, "10:10:10" );
        ps.executeUpdate();
    }

    try (Statement statement = con.createStatement()) {
        try (ResultSet resultSet = statement.executeQuery("select * from Test_Time ")) {
            while (resultSet.next()) {
                System.out.println(resultSet.getString(1));
                System.out.println(resultSet.getTime(1));
            }
        }
    }
}
您将看到以下输出:

10:10:10
02:10:10
第一个是将时间读取为字符串,第二个是使用time对象,因此它将转换为您的时区。如果在web UI上查询该表,您将看到它将返回预期值:

SELECT * FROM test_time;

+----------+
|    T     |
+----------+
| 10:10:10 |
+----------+

至少,它对我起了作用:)

从我在搜索中看到的情况来看(例如),它似乎取决于你正在使用的驱动程序,而不是数据库。我也看到了不同的结果-在我的脚本中,我插入了
10:10:10
并返回
10:10:10
,但当我看到雪花表时,它显示
09:10:10
。这是因为我住在伦敦,目前是英国夏令时(GMT+1),所以我的驱动程序在插入雪花之前(当运行
ps.setTime
时)以及从数据库检索时(当运行
results.getTime
时),将我的本地时区转换为GMT

scala中的示例,但代码基本相同:

val ps:PreparedStatement=connection.get.prepareStatement(“将覆盖插入test\u db.public.test\u表值(?))
val insertedTime:Time=Time.valueOf(“10:10:10”)
println(s“在表中插入${insertedTime}”)
ps.setTime(1,插入时间)
ps.executeUpdate()
val结果:ResultSet=connection.get.createStatement().executeQuery(“从test\u db.public.test\u表中选择时间列”)
while(results.next){
val returnedTime=results.getTime(1)
println(s“返回的时间:${returnedTime}”)
}
上面的脚本打印出来:

Inserting 10:10:10 into table
Time returned: 10:10:10
在雪花中,时间是:

+----------+
| time_col |
+----------+
| 09:10:10 |
+----------+
要解决此问题,可以将全局时区设置为GMT并运行相同的脚本。下面是将时区更改为GMT的相同示例:


TimeZone.setDefault(TimeZone.getTimeZone(“GMT+00:00”))/从我所看到的搜索结果来看(例如),它似乎取决于您使用的驱动程序,而不是数据库。我也看到了不同的结果-在我的脚本中,我插入了
10:10:10
并返回
10:10:10
,但当我看到雪花表时,它显示
09:10:10
。这是因为我住在伦敦,目前是英国夏令时(GMT+1),所以我的驱动程序在插入雪花之前(当运行
ps.setTime
时)以及从数据库检索时(当运行
results.getTime
时),将我的本地时区转换为GMT

scala中的示例,但代码基本相同:

val ps:PreparedStatement=connection.get.prepareStatement(“将覆盖插入test\u db.public.test\u表值(?))
val insertedTime:Time=Time.valueOf(“10:10:10”)
println(s“在表中插入${insertedTime}”)
ps.setTime(1,插入时间)
ps.executeUpdate()
val结果:ResultSet=connection.get.createStatement().executeQuery(“从test\u db.public.test\u表中选择时间列”)
while(results.next){
val returnedTime=results.getTime(1)
println(s“返回的时间:${returnedTime}”)
}
上面的脚本打印出来:

Inserting 10:10:10 into table
Time returned: 10:10:10
在雪花中,时间是:

+----------+
| time_col |
+----------+
| 09:10:10 |
+----------+
要解决此问题,可以将全局时区设置为GMT并运行相同的脚本。下面是将时区更改为GMT的相同示例:


TimeZone.setDefault(TimeZone.getTimeZone(“GMT+00:00”)//谢谢你,西蒙德。将默认时区设置为GMT有助于使一切同步。谢谢你,西蒙德。将默认时区设置为GMT有助于使一切同步。