将Python Dict写入InfluxDB?

时间:2017-04-28 14:34:08

标签: python influxdb influxdb-python

我一直在努力想弄清楚如何在没有运气的情况下将一个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'

关于我做错了什么的任何建议?

2 个答案:

答案 0 :(得分:4)

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

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

答案 1 :(得分:0)

我上周在实习项目上遇到了类似的问题。似乎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)

希望有所帮助