如何使用时间字段向inflox db添加度量数据?

2024-04-26 21:34:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用以下代码行在influxDB中添加度量数据。

    def add_job_influx_db_metrics(tags_dict={}, values_dict={}, measurement='test'):
       influxdb_client = InfluxDB.get_connection()
       db_name = "mydb"
       influxdb_client.switch_database(database=db_name)
       current_time = time.strftime('%Y-%m-%dT%H:%M:%SZ',time.localtime(time.time()))
       json_body = [
        {
          "measurement": measurement,
          "tags": tags_dict,
          "time": current_time,
          "fields": values_dict
         }
       ]
      response = influxdb_client.write_points(points=json_body)
      print "write_operation response", response

当我将它与Grafana集成时,数据不会出现,但当我检查它时,它显示时间是1970-01-01T00:00:00Z。可能它将开始纪元时间作为默认值。 我想花点时间填写表格“2015-8-19T07:15:00Z”。 如何获取infloxdb(python客户端)中的时间字段,什么是时间精度?


Tags: 数据nameclientjsondbtimeresponse时间
2条回答

您需要UNIX时间戳,因此应该是:

"time": int(time.time())
 from datetime import datetime
 current_time = datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ')

在json-body中使用这样的“time”—

"time": current_time

它起作用了。

相关问题 更多 >