gpt4 book ai didi

python - 将 Python Dict 写入 InfluxDB?

转载 作者:太空宇宙 更新时间:2023-11-04 10:00:51 27 4
gpt4 key购买 nike

我一直在尝试弄清楚如何将 python 字典写入 InfluxDB,但没有成功。我尝试过使用 InfluxDBClient 以及仅通过 API 使用 Requests Post。

我有以下代码:

for server in  data['service_group_stat']['member_stat_list']:
metrics = {}
metrics['measurement'] = "LB01"
metrics['tags'] = {}
metrics['fields'] = {}
metrics['tags']['SGNAME'] = name
metrics['tags']['SRVNAME'] = server['server']
metrics['fields']['CURCONNS'] = server['cur_conns']
metrics['fields']['TOTCONNS'] = server['tot_conns']
metrics['fields']['REQBYTES'] = server['req_bytes']
metrics['fields']['REQPKTS'] = server['req_pkts']
metrics['fields']['RESPBYTES'] = server['resp_bytes']
metrics['fields']['RESPPKTS'] = server['resp_pkts']
pprint(metrics)

这将给我以下输出:

{'fields': {'CURCONNS': 33,
'REQBYTES': 3151292236,
'REQPKTS': 21160834,
'RESPBYTES': 66671993850,
'RESPPKTS': 51709706,
'TOTCONNS': 332177},
'measurement': 'LB01',
'tags': {'SGNAME': 'SG_ACCOUNT.BUSINESS.COM_443',
'SRVNAME': u'WWW0006'}}
{'fields': {'CURCONNS': 39,
'REQBYTES': 3387948728,
'REQPKTS': 23103920,
'RESPBYTES': 71703285665,
'RESPPKTS': 55687510,
'TOTCONNS': 369628},
'measurement': 'LB01',
'tags': {'SGNAME': 'SG_ACCOUNT.BUSINESS.COM_443',
'SRVNAME': u'WWW0005'}}
{'fields': {'CURCONNS': 16,
'REQBYTES': 3401134891,
'REQPKTS': 24014042,
'RESPBYTES': 70917802336,
'RESPPKTS': 55029480,
'TOTCONNS': 342360},
'measurement': 'LB01',
'tags': {'SGNAME': 'SG_ACCOUNT.BUSINESS.COM_443',
'SRVNAME': u'WWW0004'}}

我试过只使用 requests.post 来利用 InfluxDB API:

url = "http://localhost:8086/write?db=metrics"

r = requests.post(url, metrics)

但是我似乎总是得到 400 响应:

reply: 'HTTP/1.1 400 Bad Request\r\n'

我也尝试过使用 InfluxDBClient:

from influxdb import InfluxDBClient

# Define InfluxDB Client Information

client = InfluxDBClient(host='127.0.0.1', port=8086, username='root', password='root', database='metrics')

for server in data['service_group_stat']['member_stat_list']:
metrics = {}
metrics['measurement'] = "LB01"
metrics['tags'] = {}
metrics['fields'] = {}
metrics['tags']['SGNAME'] = name
metrics['tags']['SRVNAME'] = server['server']
metrics['fields']['CURCONNS'] = server['cur_conns']
metrics['fields']['TOTCONNS'] = server['tot_conns']
metrics['fields']['REQBYTES'] = server['req_bytes']
metrics['fields']['REQPKTS'] = server['req_pkts']
metrics['fields']['RESPBYTES'] = server['resp_bytes']
metrics['fields']['RESPPKTS'] = server['resp_pkts']

client.write_points(metrics)

这最终给我以下错误:

    client.write_points(metrics)
File "/usr/lib/python2.7/site-packages/influxdb/client.py", line 456, in write_points
tags=tags, protocol=protocol)
File "/usr/lib/python2.7/site-packages/influxdb/client.py", line 506, in _write_points
protocol=protocol
File "/usr/lib/python2.7/site-packages/influxdb/client.py", line 292, in write
data = make_lines(data, precision).encode('utf-8')
File "/usr/lib/python2.7/site-packages/influxdb/line_protocol.py", line 126, in make_lines
point.get('measurement', data.get('measurement'))
AttributeError: 'str' object has no attribute 'get'

对我做错了什么有什么建议吗?

最佳答案

使用 json 协议(protocol)(默认)时,您必须将字典列表传递给 InfluxDBClient.write_points()。
你可以查看the source code make_lines() 方法要确信它迭代点。

答案:使用client.write_points([metrics])

关于python - 将 Python Dict 写入 InfluxDB?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43682736/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com