1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
|
Migration Guide
===============
This guide is meant to help you migrate your Python code from
`influxdb-python <https://github.com/influxdata/influxdb-python>`__ to
``influxdb-client-python`` by providing code examples that cover common
usages.
If there is something missing, please feel free to create a `new
request <https://github.com/influxdata/influxdb-client-python/issues/new?assignees=&labels=documentation&title=docs(migration%20guide):%20&template=feature_request.md>`__
for a guide enhancement.
Before You Start
----------------
Please take a moment to review the following client docs:
- `User Guide <https://influxdb-client.readthedocs.io/en/stable/usage.html>`__, `README.rst <https://github.com/influxdata/influxdb-client-python#influxdb-client-python>`__
- `Examples <https://github.com/influxdata/influxdb-client-python/tree/master/examples#examples>`__
- `API Reference <https://influxdb-client.readthedocs.io/en/stable/api.html>`__
- `CHANGELOG.md <https://github.com/influxdata/influxdb-client-python/blob/master/CHANGELOG.md>`__
Content
-------
- `Initializing Client <#initializing-client>`__
- `Creating Database/Bucket <#creating-databasebucket>`__
- `Dropping Database/Bucket <#dropping-databasebucket>`__
- Writes
- `LineProtocol <#writing-lineprotocol>`__
- `Dictionary-style object <#writing-dictionary-style-object>`__
- `Structured data <#writing-structured-data>`__
- `Pandas DataFrame <#writing-pandas-dataframe>`__
- `Querying <#querying>`__
Initializing Client
-------------------
**influxdb-python**
.. code:: python
from influxdb import InfluxDBClient
client = InfluxDBClient(host='127.0.0.1', port=8086, username='root', password='root', database='dbname')
**influxdb-client-python**
.. code:: python
from influxdb_client import InfluxDBClient
with InfluxDBClient(url='http://localhost:8086', token='my-token', org='my-org') as client:
pass
Creating Database/Bucket
------------------------
**influxdb-python**
.. code:: python
from influxdb import InfluxDBClient
client = InfluxDBClient(host='127.0.0.1', port=8086, username='root', password='root', database='dbname')
dbname = 'example'
client.create_database(dbname)
client.create_retention_policy('awesome_policy', '60m', 3, database=dbname, default=True)
**influxdb-client-python**
.. code:: python
from influxdb_client import InfluxDBClient, BucketRetentionRules
org = 'my-org'
with InfluxDBClient(url='http://localhost:8086', token='my-token', org=org) as client:
buckets_api = client.buckets_api()
# Create Bucket with retention policy set to 3600 seconds and name "bucket-by-python"
retention_rules = BucketRetentionRules(type="expire", every_seconds=3600)
created_bucket = buckets_api.create_bucket(bucket_name="bucket-by-python",
retention_rules=retention_rules,
org=org)
Dropping Database/Bucket
------------------------
**influxdb-python**
.. code:: python
from influxdb import InfluxDBClient
client = InfluxDBClient(host='127.0.0.1', port=8086, username='root', password='root', database='dbname')
dbname = 'example'
client.drop_database(dbname)
**influxdb-client-python**
.. code:: python
from influxdb_client import InfluxDBClient
with InfluxDBClient(url='http://localhost:8086', token='my-token', org='my-org') as client:
buckets_api = client.buckets_api()
bucket = buckets_api.find_bucket_by_name("my-bucket")
buckets_api.delete_bucket(bucket)
Writing LineProtocol
--------------------
**influxdb-python**
.. code:: python
from influxdb import InfluxDBClient
client = InfluxDBClient(host='127.0.0.1', port=8086, username='root', password='root', database='dbname')
client.write('h2o_feet,location=coyote_creek water_level=1.0 1', protocol='line')
**influxdb-client-python**
.. code:: python
from influxdb_client import InfluxDBClient
from influxdb_client.client.write_api import SYNCHRONOUS
with InfluxDBClient(url='http://localhost:8086', token='my-token', org='my-org') as client:
write_api = client.write_api(write_options=SYNCHRONOUS)
write_api.write(bucket='my-bucket', record='h2o_feet,location=coyote_creek water_level=1.0 1')
Writing Dictionary-style object
-------------------------------
**influxdb-python**
.. code:: python
from influxdb import InfluxDBClient
record = [
{
"measurement": "cpu_load_short",
"tags": {
"host": "server01",
"region": "us-west"
},
"time": "2009-11-10T23:00:00Z",
"fields": {
"Float_value": 0.64,
"Int_value": 3,
"String_value": "Text",
"Bool_value": True
}
}
]
client = InfluxDBClient(host='127.0.0.1', port=8086, username='root', password='root', database='dbname')
client.write_points(record)
**influxdb-client-python**
.. code:: python
from influxdb_client import InfluxDBClient
from influxdb_client.client.write_api import SYNCHRONOUS
with InfluxDBClient(url='http://localhost:8086', token='my-token', org='my-org') as client:
write_api = client.write_api(write_options=SYNCHRONOUS)
record = [
{
"measurement": "cpu_load_short",
"tags": {
"host": "server01",
"region": "us-west"
},
"time": "2009-11-10T23:00:00Z",
"fields": {
"Float_value": 0.64,
"Int_value": 3,
"String_value": "Text",
"Bool_value": True
}
}
]
write_api.write(bucket='my-bucket', record=record)
Writing Structured Data
-----------------------
**influxdb-python**
.. code:: python
from influxdb import InfluxDBClient
from influxdb import SeriesHelper
my_client = InfluxDBClient(host='127.0.0.1', port=8086, username='root', password='root', database='dbname')
class MySeriesHelper(SeriesHelper):
class Meta:
client = my_client
series_name = 'events.stats.{server_name}'
fields = ['some_stat', 'other_stat']
tags = ['server_name']
bulk_size = 5
autocommit = True
MySeriesHelper(server_name='us.east-1', some_stat=159, other_stat=10)
MySeriesHelper(server_name='us.east-1', some_stat=158, other_stat=20)
MySeriesHelper.commit()
The ``influxdb-client-python`` doesn't have an equivalent implementation for ``MySeriesHelper``, but there is an option
to use Python `Data Classes <https://docs.python.org/3/library/dataclasses.html>`__ way:
**influxdb-client-python**
.. code:: python
from dataclasses import dataclass
from influxdb_client import InfluxDBClient
from influxdb_client.client.write_api import SYNCHRONOUS
@dataclass
class Car:
"""
DataClass structure - Car
"""
engine: str
type: str
speed: float
with InfluxDBClient(url='http://localhost:8086', token='my-token', org='my-org') as client:
write_api = client.write_api(write_options=SYNCHRONOUS)
car = Car('12V-BT', 'sport-cars', 125.25)
write_api.write(bucket="my-bucket",
record=car,
record_measurement_name="performance",
record_tag_keys=["engine", "type"],
record_field_keys=["speed"])
Writing Pandas DataFrame
------------------------
**influxdb-python**
.. code:: python
import pandas as pd
from influxdb import InfluxDBClient
df = pd.DataFrame(data=list(range(30)),
index=pd.date_range(start='2014-11-16', periods=30, freq='H'),
columns=['0'])
client = InfluxDBClient(host='127.0.0.1', port=8086, username='root', password='root', database='dbname')
client.write_points(df, 'demo', protocol='line')
**influxdb-client-python**
.. code:: python
import pandas as pd
from influxdb_client import InfluxDBClient
from influxdb_client.client.write_api import SYNCHRONOUS
with InfluxDBClient(url='http://localhost:8086', token='my-token', org='my-org') as client:
write_api = client.write_api(write_options=SYNCHRONOUS)
df = pd.DataFrame(data=list(range(30)),
index=pd.date_range(start='2014-11-16', periods=30, freq='H'),
columns=['0'])
write_api.write(bucket='my-bucket', record=df, data_frame_measurement_name='demo')
Querying
--------
**influxdb-python**
.. code:: python
from influxdb import InfluxDBClient
client = InfluxDBClient(host='127.0.0.1', port=8086, username='root', password='root', database='dbname')
points = client.query('SELECT * from cpu').get_points()
for point in points:
print(point)
**influxdb-client-python**
.. code:: python
from influxdb_client import InfluxDBClient
with InfluxDBClient(url='http://localhost:8086', token='my-token', org='my-org', debug=True) as client:
query = '''from(bucket: "my-bucket")
|> range(start: -10000d)
|> filter(fn: (r) => r["_measurement"] == "cpu")
|> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")
'''
tables = client.query_api().query(query)
for record in [record for table in tables for record in table.records]:
print(record.values)
If you would like to omit boilerplate columns such as ``_result``, ``_table``, ``_start``, ... you can filter the record values by
following expression:
.. code:: python
print({k: v for k, v in record.values.items() if k not in ['result', 'table', '_start', '_stop', '_measurement']})
For more info see `Flux Response Format <https://github.com/influxdata/flux/blob/master/docs/SPEC.md#response-format>`__.
|