TDengine icon indicating copy to clipboard operation
TDengine copied to clipboard

timezone issue of JDBC

Open jinglings opened this issue 5 years ago • 3 comments

1.使用连接信息如下: driver-class-name: com.taosdata.jdbc.TSDBDriver

            url: jdbc:TAOS://server01:6030/energy?timezone=GMT%2B8&charset=utf-8
            username: root
            password: taosdata
            charset: UTF-8
            locale: en_US.UTF-8
            timezone: UTC-8

2.客户端使用的默认配置 3.其他地方是否还需要配置?

jinglings avatar Jul 07 '21 00:07 jinglings

咱们数据库和jdbc分别是什么版本?只有JDBC应用这样?在客户端有同样现象吗?

yu285 avatar Jul 10 '21 08:07 yu285

插入数据的使用长整型bigint,查询的时候,也是使用时间戳(长整型)进行比较,就可以避免客户端时区,服务端时区,连接字符串时区带来的转换问题。

springboot结合mybatis的例子如下,供你参考:

create stable if not exists s_device (ts TIMESTAMP,
  temperature double,
  humidity double
) TAGS (device_sn BINARY(1000));
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@TableName(value = "s_device")
public class TestSuperDeviceEntity {
    private Timestamp ts;
    private Float temperature;
    private Float humidity;
    @TableField(value = "device_sn")
    private String device_sn ;
}
@Mapper
public interface TestSuperDeviceMapper extends BaseMapper<TestSuperDeviceEntity> {

    /**
     * 单个插入
     * @param entity
     * @return
     */
    @Insert({
            "INSERT INTO 's_device_${entity.deviceSn}' (ts ,temperature, humidity ) ",
            "USING s_device (device_sn) TAGS (#{entity.deviceSn}) ",
            "VALUES (#{entity.ts}, #{entity.temperature}, #{entity.humidity})"
    })
    int insertOne(@Param(value = "entity") TestSuperDeviceEntity entity);


    /**
     * 批量插入
     * @param entities
     * @return
     */
    @Insert({
            "<script>",
            "INSERT INTO ",
            "<foreach collection='list' item='item' separator=' '>",
            "'s_device_${item.deviceSn}' (ts ,temperature, humidity) USING s_device (device_sn) TAGS (#{item.deviceSn}) ",
            "VALUES (#{item.ts}, #{item.temperature}, #{item.humidity})",
            "</foreach>",
            "</script>"
    })
    int batchInsert(@Param("list") List<TestSuperDeviceEntity> entities);

    /**
     *  查询过去一段时间范围的平均温度,每小时1个数据点
     * @param deviceSn
     * @param startTime inclusive
     * @param endTime   exclusive
     * @return
     */
    @Select("select avg(temperature) temperature from s_device where  device_sn = #{deviceSn} and ts >= #{startTime} and ts < #{endTime}  interval(1h)")
    List<TempSevenDaysTemperature> selectSevenDaysTemperature(
            @Param(value = "deviceSn") String deviceSn,
            @Param(value = "startTime") long startTime,
            @Param(value = "endTime") long endTime);

    @AllArgsConstructor
    @NoArgsConstructor
    @Data
    @Builder
    class TempSevenDaysTemperature {
        private Timestamp ts;
        private float temperature;
    }
}

stringhuang avatar Jul 13 '21 01:07 stringhuang

感谢,日常使用时,请尽量确保客户端和服务端时区一致。

yu285 avatar Aug 08 '21 10:08 yu285