taosdata / TDengine

High-performance, scalable time-series database designed for Industrial IoT (IIoT) scenarios
https://tdengine.com
GNU Affero General Public License v3.0
23.22k stars 4.83k forks source link

In Java, is it possible to receive nanosecond timestamps using the Long data type? #10643

Closed fatcarter closed 4 months ago

fatcarter commented 2 years ago

在Java中,是否可以用Long类型接收纳秒级时间戳? 问这个问题的原因是我在实际测试中,使用Long类型接收了纳秒级时间戳字段,结果接收到的值只有13位,实际应该是19位; 请问这个问题是我使用的问题或者程序中的bug导致的吗? 还是说JDBC的设计就是最长13位?只到毫秒?

fenghuazzm commented 2 years ago

TDengine 、 jdbcdriver 都是什么版本呢?

fatcarter commented 2 years ago

td server: 2.4.0.0 td client: 2.4.0.0 jdbcdriver: 2.0.37

fatcarter commented 2 years ago

需要我在jdbc的url上配置什么参数吗? 现在是没有配置任何参数的

taos> show databases;
               name               |      created_time       |   ntables   |           keep           | precision | update |   status   |
========================================================================================================================================
 zhgd_iot                         | 2022-03-04 14:11:25.281 |           2 | 30                       | ns        |      0 | ready      |

taos> show create stable device_message;
                                                                                              Table                                                                                               |                                                                  Create Table                                                                   |
=====================================================================================================================================================================================================================================================================================================================================================
 device_message                                                                                                                                                                                   | create table `device_message` (`ts` TIMESTAMP,`payload` NCHAR(2048),`receive_time` TIMESTAMP,`send_time` TIMESTAMP) TAGS (`tag_name` NCHAR(64)) |
fatcarter commented 2 years ago

实际数据

taos> select ts, receive_time, send_time from device_message limit 2;
              ts               |         receive_time          |           send_time           |
================================================================================================
 2022-03-08 16:30:54.534000001 | 2022-03-08 16:30:54.000000000 | 2022-03-08 16:30:54.235000000 |
 2022-03-08 16:30:54.534000002 | 2022-03-08 16:30:54.000000000 | 2022-03-08 16:30:54.235000000 |
Query OK, 2 row(s) in set (0.005582s)

实体类

import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

@Data
@TableName("f_device_message")
public class DeviceMessage {
    private Long ts;
    // private String payload;
    private Long receiveTime;
    private Long sendTime;
}

返回的结果

[{
    "receiveTime": 1646728254000,
    "sendTime": 1646728254235,
    "ts": 1646728254534
}, {
    "receiveTime": 1646728254000,
    "sendTime": 1646728254235,
    "ts": 1646728254534
}, {
    "receiveTime": 1646728254001,
    "sendTime": 1646728254236,
    "ts": 1646728254534
}]
fatcarter commented 2 years ago

我好像找到问题了 在方法com.taosdata.jdbc.TSDBResultSet#getLong(int)中如果字段类型为Timestamp类型,会调用Timestamp的getTime()方法, 而getTime()方法返回的正是毫秒时间戳;

请问这个是正常实现吗?

fenghuazzm commented 2 years ago

19位的是纳秒精度的时间戳,要拿到纳秒的时间戳:

  1. 用getLong方法,返回的就是19位的long
  2. 用getTimestamp,然后调用Timestamp的getNanos,返回的是纳秒精度
fatcarter commented 2 years ago

第一种方法, 我这边测试getLong方法返回数值的长度是13位