Python 에서 InfluxDB의 자료를 불러와서 plot을 그려보기로 했다. 첫 번째 단계는 자료를 불러오는 것, 그리고 2번째 단계는 불러온 자료를 가공하는 것이다.
PIP로 influxdb_client를 설치한다. 여러 종류가 있는 듯 한데, 불러오는 부분에 있어서 큰 차이는 없는 것 같다.
import influxdb_client
url = "http://www.welovedoctor.com:8086"
token = "my_token"
org = 'my_org'
client = influxdb_client.InfluxDBClient(url=url, token=token, org=org)
query_api = client.query_api()
query = 'from(bucket:"measurement") |> range(start: -1h) |> filter(fn: (r) => r["_measurement"] == "location=home") |> filter(fn: (r) => r["_field"] == "temperature")'
tables = query_api.query(org=org, query=query)
query는 Flux 문법의 스크립트를 그대로 이용할 수 있다. 이렇게 하면 tables에 결과를 저장할 수 있다.
그 다음은 json 으로 포맷을 변환하는 것이다. 구조가 복합하기 때문에 실제로 불러서 확인해 본 후 분석 방법을 결정해야 한다.
from influxdb_client.client.flux_table import FluxStructureEncoder
dt = json.loads(json.dumps(tables, cls=FluxStructureEncoder))