2

我需要使用 Python API 通过 Flux 查询语言获取 InfluxDB 2.0 中特定测量的最高(又名最近)时间戳。

我已使用以下查询来获取时间戳,但我不确定如何确保通过此方法提取的时间戳确实是最新的。

特别是,我使用的索引last_data[0]是任意last_data的,就像对象列表一样<influxdb_client.client.flux_table.FluxTable object at 0x00000193FA906AC8>,我不确定如何解释。时间戳似乎没有排序。

from influxdb_client import InfluxDBClient, WriteOptions

client = InfluxDBClient(url=self.influx_url, token=self.token, org=self.org_id, debug=False)

last_data = client.query_api().query(
    f'from(bucket:"{self.influx_bucket}") |> range(start: -100d) |> filter(fn: (r) => r["_measurement"] == "{devices[0]}") |> last()'
)
timestamp = last_data[0].records[0]["_stop"]
print(timestamp)
4

1 回答 1

1

First of all, _stop is used to tell you the end of your time range. In your case, it would be just a timestamp of the query execution time as you don't enter the end date for the range. To get the timestamp for measurement you need to use _time. To get the result you want you would probably need to add |> group() before calling the last function. So it would be

last_data = client.query_api().query(
    f'from(bucket:"{self.influx_bucket}") |> range(start: -100d) |> filter(fn: (r) => r["_measurement"] == "{devices[0]}") |> group() |> last()'
于 2021-09-24T12:52:30.523 回答