如何在InfluxDB中存储日期 [英] How to store dates in InfluxDB

查看:1491
本文介绍了如何在InfluxDB中存储日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要开发一个InfluxDB时间序列.时间序列需要包含以下信息:

I need to develop an InfluxDB Time Series. The time series needs to contain the following information:

  • 时间-记录数据的时间.这将使用InfluxDB时间字段.
  • value-时间序列的值.一个简单的整数.
  • date-与值关联的单独日期.此日期与时间"字段无关.它将用于帮助缩小查询范围.

我目前的思路是将上述日期"字段另存为时间序列中的单独列",以便我可以使用"where"子句来过滤使用该日期的数据.但是,我正在努力表达这一点. InfluxDB是否支持任何类型的日期或日期/时间字段?对于时间"字段,似乎仅使用毫秒.但是,如果我在名称不同的字段中尝试相同的操作,则正常的时间查询将不起作用.例如:

My current line of thinking is to save the above "date" field as a separate "column" in the time series so that I can use the "where" clause to filter the data using that date. However I am struggling with how to represent that. Does InfluxDB support any kind of date or date/time fields? For the "time" field it seems to just use milliseconds. However if I try the same in a field with a different name, then the normal time queries don't work. So for example:

select * from myseries where time > now() - 1d

上面的查询将正常工作.

The above query will work just fine.

vs

select * from myseries where date > now() - 1d

此查询将因错误而失败,因为它似乎不知道如何将日期"视为时间值.

This query will fail with an error because it doesn't seem to know how to treat "date" as a time value.

在这种情况下,对日期的表示是否更好?

Is there a better representation for dates in this scenario?

推荐答案

InfluxDB数据类型只能是以下之一:浮点数,整数,布尔值或字符串. time字段是一个特殊的例外.

InfluxDB data types can be only be one of: floats, ints, bools, or strings. The time field is a special exception.

您可以将代表count-since-epoch的整数用于日期字段.像now()这样的便捷功能似乎并不能解决问题(使用v0.13):

You can use integers representing count-since-epoch for the date field. Nice convenient functions like now() don't seem work for that though (using v0.13):

insert test_dates date_int=1573405622000000000i,desc="years from now"
insert test_dates date_int=1373405661000000000i,desc="years ago"

使用now()

select * from test_dates where date_int > now()

给予:

name: test_dates

time                    date_int                desc 
1473404302801938064     1573405622000000000     years from now
1473404315927493772     1373405661000000000     years ago

并且:

select * from test_dates where date_int < now()

给予:

name: test_dates

time                    date_int                desc
1473462286404084162     1573405622000000000     years from now
1473462286408231540     1373405661000000000     years ago

似乎每个date_int都以某种方式大于并且小于now()

Seems every date_int is somehow both greater than and less than now()

因此,如果您使用整数,则比较不是语法错误,但不能按照我们希望的方式进行.

So the comparison isn't a syntax error if you use integers, but doesn't work the way we'd like.

解决此问题的一种方法是在前端应用程序中创建自己的日期到整数转换.然后,前端的日期比较就是InfluxDB中的int比较.笨拙,但是这些是我们拥有的数据类型.

One way to solve this is to create your own date-to-int conversion in the front-end app. Then a date comparison on the front-end is an int comparison within InfluxDB. Clunky, but those are the data types we have.

InfluxDB中存储的日期可以是单个基于纪元的int,也可以在InfluxDB中存储年,月,日的单独int字段.在后一种情况下,查询会更大,更慢,但更易于阅读和调试.

The date as stored in InfluxDB could be a single epoch-based int, or store separate int fields for year, month, day within InfluxDB. The queries are bigger and slower in the latter case, but it's easier to read and debug.

祝你好运!

这篇关于如何在InfluxDB中存储日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆