|
| 1 | +""" |
| 2 | +How to ingest DataFrame with default tags. |
| 3 | +""" |
| 4 | + |
| 5 | +import pandas as pd |
| 6 | + |
| 7 | +from influxdb_client import InfluxDBClient |
| 8 | +from influxdb_client.client.write_api import SYNCHRONOUS, PointSettings |
| 9 | + |
| 10 | +""" |
| 11 | +Load DataFrame form CSV File |
| 12 | +""" |
| 13 | +df = pd.read_csv("vix-daily.csv") |
| 14 | +print(df.head()) |
| 15 | + |
| 16 | +client = InfluxDBClient(url="http://localhost:9999", token="my-token", org="my-org") |
| 17 | + |
| 18 | +""" |
| 19 | +Ingest DataFrame with default tags |
| 20 | +""" |
| 21 | +point_settings = PointSettings(**{"type": "vix-daily"}) |
| 22 | +point_settings.add_default_tag("example-name", "ingest-data-frame") |
| 23 | + |
| 24 | +write_api = client.write_api(write_options=SYNCHRONOUS, point_settings=point_settings) |
| 25 | +write_api.write(bucket="my-bucket", record=df, data_frame_measurement_name="financial-analysis-df") |
| 26 | + |
| 27 | +""" |
| 28 | +Querying ingested data |
| 29 | +""" |
| 30 | +query = 'from(bucket:"my-bucket")' \ |
| 31 | + ' |> range(start: 0, stop: now())' \ |
| 32 | + ' |> filter(fn: (r) => r._measurement == "financial-analysis-df")' \ |
| 33 | + ' |> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")' \ |
| 34 | + ' |> limit(n:10, offset: 0)' |
| 35 | +result = client.query_api().query(query=query) |
| 36 | + |
| 37 | +""" |
| 38 | +Processing results |
| 39 | +""" |
| 40 | +print() |
| 41 | +print("=== results ===") |
| 42 | +print() |
| 43 | +for table in result: |
| 44 | + for record in table.records: |
| 45 | + print('{4}: Open {0}, Close {1}, High {2}, Low {3}'.format(record["VIX Open"], record["VIX Close"], |
| 46 | + record["VIX High"], record["VIX Low"], |
| 47 | + record["type"])) |
| 48 | + |
| 49 | +""" |
| 50 | +Close client |
| 51 | +""" |
| 52 | +client.__del__() |
0 commit comments