将 Python 字典写入 InfluxDB?

Posted

技术标签:

【中文标题】将 Python 字典写入 InfluxDB?【英文标题】:Write Python Dict to InfluxDB? 【发布时间】:2017-09-26 17:19:21 【问题描述】:

我一直在尝试弄清楚如何将 python dict 写入 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'

关于我做错了什么有什么建议吗?

【问题讨论】:

【参考方案1】:

在使用 json 协议(默认)时,您必须将 dicts 列表传递给 InfluxDBClient.write_points()。 您可以检查 make_lines() 方法的the source code 以确信它会迭代点。

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

【讨论】:

【参考方案2】:

上周我在实习项目中遇到了类似的问题。似乎python不喜欢这样的语法:

metrics['tags']['SGNAME'] = name

也许你可以试试这样的东西。它对我有用。

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

希望对你有帮助

【讨论】:

以上是关于将 Python 字典写入 InfluxDB?的主要内容,如果未能解决你的问题,请参考以下文章

将具有多个标签的值写入 influxDB(使用 python)

python 多线程将数据写入 influxDB

python 多线程将数据写入 influxDB

python脚本向influxdb写入数据

使用 Influxdb 和 python 在 DB 上写入数据

influxdb:多次写入多点与单点