java - InfluxDB'请求选择其中时间>时间戳

标签 java timestamp influxdb influxql

我有一个 java 应用程序,它将测量结果发送到 influxDB 数据库。

我正在我的 influxdb 数据库中添加点。 我每次程序运行时添加的点都有当前时间戳。

这就是我添加积分的方式(测量构建):

    BatchPoints batchPoints;

    Date date = new Date();

    String beginofday = Constant.simpledateFormat.format(date);

    date = Constant.simpledateFormat.parse(beginofday);

    long timestamp = date.getTime();

    //adding points
    for buildings ... do
    Point point = Point.measurement("building").tag(tagsToAdd).fields(fieldsToAdd)
                        .time(timestamp, TimeUnit.NANOSECONDS).build();

    batchPoints.point(point);

我的问题是,当我用这样的请求请求我的数据库时:

select count(tag) from building where time > (my timestamp)

我注意到以前的时间戳结果也被计算在内,即使我正在做 time>timestamp。 当我执行 > 而不是 >= 时,它只计算最后一个。 我还注意到,对于前一个时间戳,例如,如果我有像这样的时间戳 1540300800000 ns,influxdb 在开头添加 6,它就会变成 61540300800000 ms。

我真的不明白发生了什么。

有什么想法吗?

最佳答案

java.time

    ZoneId zone = ZoneId.systemDefault();
    Instant beginofday = LocalDate.now(zone).atStartOfDay(zone).toInstant();

    long timestamp = beginofday.toEpochMilli();

    //adding points
    for buildings ... do
    Point point = Point.measurement("building").tag(tagsToAdd).fields(fieldsToAdd)
                        .time(timestamp, TimeUnit.MILLISECONDS).build();

我正在使用 java.time,现代 Java 日期和时间 API。

你的代码出了什么问题

虽然我没有重现和测试,但我相信您混淆了毫秒、10^-3秒和纳秒、10^-9秒。 date.getTime() 为您提供自纪元以来的毫秒数,但您在 time(timestamp, TimeUnit.NANOSECONDS) 中传递该数字。如果我以我所在时区 (CET) 的今天(2 月 21 日)开始,并使用自纪元以来的毫秒数作为纳秒,我会得到 1970-01-01T00:25:50.703600Z。所以我假设你在那个时间点之后得到了一切。

其他要点:

  • 您使用的 DateSimpleDateFormat 类设计不佳且早已过时,因此我建议您避免使用它们。现代 java.time 更适合使用。
  • 将日期时间格式化为日期字符串并将其解析回来是查找一天的开始的绕道。

链接

Oracle tutorial: Date Time解释如何使用 java.time。

关于java - InfluxDB'请求选择其中时间>时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54791266/

相关文章:

java - 如果Android应用程序的OBB文件被意外删除,如何重新下载?

java - 在 CDMA BlackBerry 设备(OS5 及更高版本)中获取 Cell ID 和 LAC

php - 给定一个 Unix 时间戳,如何获得那一天的开始和结束?

database - 如何从 InfluxDB 获取 `MEASUREMENTS` 的大小?

java - InfluxDB "Couldn' t 查找列”当使用 WHERE 子句时

java - Tomcat 连接器如何工作?

Java - 对 ArrayList 进行排序

android - 在 Kotlin 中将时间戳值转换为 12/24 小时时间值

MySQL存储不同时区的播放列表

http - 如何在 v0.8 中的 influxdb 中获取数据库列表