我有一个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)
我注意到以前的时间戳结果也被计算在内,即使我在做时间>时间戳。当我做一个>而不是> =时,它仅计数最后一个。我还注意到,对于以前的时间戳,例如,如果我有像这样的时间戳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。
您的代码中出了什么问题
虽然我没有复制和测试,但我相信您将毫秒,10^-3秒和 nanseconds ,10^-9秒。自时代以来,date.getTime()
为您提供了毫秒,但是您在time(timestamp, TimeUnit.NANOSECONDS)
中传递了数字。如果我今天(2月21日)在我的时区(CET)中进行一天的开始,并以自时代以来使用毫秒为纳秒,我会得到1970-01-01T00:25:50.703600Z
。所以我认为您在那个时候有一段时间后得到了一切。
其他要点:
- 您使用的类
Date
和SimpleDateFormat
的类别设计不佳且过时,所以我建议您避免使用它们。现代Java.Time可以使用。 - 将您的日期时间格式化为日期字符串,然后解析它是找到一天开始的弯路。
链接
Oracle教程:说明如何使用Java.Time。