Reputation: 333
I am working on a Python program. I am trying to write a json (see below) into influxDB
by using influxdb-python
using the below code -
DBclient = client(host, port, user, password, dbname)
influx_data = json.dumps(write_to_influx_json)
print influx_data
# Writing Data in Influx
DBclient.write_points(influx_data)
JSON -
[{
"fields": [{
"PATH": "/",
"DISK_USED_PERCENT": "10"
}, {
"PATH": "/xxxxxxxxx",
"DISK_USED_PERCENT": "0"
}, {
"PATH": "/dev/xxxxxxxxx",
"DISK_USED_PERCENT": "0"
}, {
"PATH": "/xxxxxxxxx",
"DISK_USED_PERCENT": "3"
}, {
"PATH": "/sys/xxxxxxxxx/xxxxxxxxx",
"DISK_USED_PERCENT": "0"
}, {
"PATH": "/run/xxxxxxxxx",
"DISK_USED_PERCENT": "0"
}, {
"PATH": "xxxxxxxxx",
"DISK_USED_PERCENT": "0"
}],
"tags": {
"host_identifier": "xxxxxx",
"name": "mount_point_percentage"
},
"time": "xxxxx",
"measurement": "xxxxxxx"
}]
But i keep getting the below error -
AttributeError: 'str' object has no attribute 'get'
Can someone please point me in the right direction?
Upvotes: 1
Views: 8859
Reputation: 51
There are 2 problems in your code.
First of all, you are not calling write_points()
correctly (like @Davidgs and @danny said). The docs state the following:
write_points(points)
points (list of dictionaries, each dictionary represents a point) – the list of points to be written in the database
In your code, you are calling DBclient.write_points(influx_data)
, but the argument influx_data
is a string. The method expects it to be a list of dictionaries. This can be solved by sending the JSON data write_to_influx_json
instead of influx_data
. So your code should look like this:
DBclient.write_points(write_to_influx_json)
The second problem is the JSON structure, and to be more specific, the fields
element. It should be a dictionary, and not a list (like the tags
element). From what I can see, you are trying to send multiple data points. The data points must be separate dictionaries (as the docs are stating). So your JSON data structure should look like this instead:
[
{
"fields": {
"PATH": "/",
"DISK_USED_PERCENT": "10"
},
"tags": {
"host_identifier": "xxxxxx",
"name": "mount_point_percentage"
},
"time": "xxxxx",
"measurement": "xxxxxxx"
},
{
"fields": {
"PATH": "/xxxxxxxxx",
"DISK_USED_PERCENT": "0"
},
"tags": {
"host_identifier": "xxxxxx",
"name": "mount_point_percentage"
},
"time": "xxxxx",
"measurement": "xxxxxxx"
},
{
"fields": {
"PATH": "/dev/xxxxxxxxx",
"DISK_USED_PERCENT": "0"
},
"tags": {
"host_identifier": "xxxxxx",
"name": "mount_point_percentage"
},
"time": "xxxxx",
"measurement": "xxxxxxx"
},
{
"fields": {
"PATH": "/xxxxxxxxx",
"DISK_USED_PERCENT": "3"
},
"tags": {
"host_identifier": "xxxxxx",
"name": "mount_point_percentage"
},
"time": "xxxxx",
"measurement": "xxxxxxx"
}
]
and so on.
This error just happened in my code as well! :)
Upvotes: 0
Reputation: 5270
influx_data = json.dumps(write_to_influx_json)
Data needs to be a list of dictionaries, not json.
See docs.
write_points(points)
points (list of dictionaries, each dictionary represents a point) – the list of points to be written in the database
Upvotes: 0
Reputation: 419
It doesn't seem that you're calling the write_points() correctly. The Docs for that seem to indicate that you need to provide a lot more info.
write_points(points, time_precision=None, database=None, retention_policy=None, tags=None, batch_size=None, protocol=u'json')
Parameters:
points (list of dictionaries, each dictionary represents a point) – the list of points to be written in the database
time_precision (str) – Either s
, m
, ms
or u
, defaults to None
database (str) – the database to write the points to. Defaults to the client’s current database
tags (dict) – a set of key-value pairs associated with each point. Both keys and values must be strings. These are shared tags and will be merged with point-specific tags, defaults to None
retention_policy (str) – the retention policy for the points. Defaults to None
batch_size (int) – value to write the points in batches instead of all at one time. Useful for when doing data dumps from one database to another or when doing a massive write operation, defaults to None
protocol (str) – Protocol for writing data. Either line
or json
.
Upvotes: 0