How to change a Python code of InfluxDB1.8 for write nested json in InfluxDB2.0

Hi there!
Could you point me out the easiest way or a link with an example or docs to switch to InfluxDB2.0?
The problem is that in the old version we use a database (database), but in 2.0 we have a bucket…
A chunk of python script is:

from influxdb import InfluxDBClient    
client = InfluxDBClient('localhost', 9999, 'root', 'root', 'database')
client.write_points([json_for_influxdb])  # Should be a list of nested dictionaries - json

Meanwhile have got a solution

from influxdb_client import InfluxDBClient, Point
''' adopted from https://github.com/influxdata/influxdb-client-python/blob/master/examples/influxdb_18_example.py'''

org = 'your_bucket_ID'
token = 'your_token_obtained_via_influxDB_UI'
url = 'http://localhost:9999'
bucket = 'bucket_new'

client = InfluxDBClient(url=url, token=token, org=org)

print('*** Write Points ***')
write_api = client.write_api()

points = []
#  json parse is required here to populate corresponding fields
points.append(Point("measurements").tag("host", "host1").field("used_percent", 25.43234543))
points.append(Point("measurements").tag("host", "host1").field("used_percent", 26.43234543))
# ...

print(f'*** Write Points *** to bucket - {bucket}')
write_api.write(bucket=bucket, record=points)
write_api.__del__()

client.close()
1 Like