def __init__(self, url, token, org, bucket): print( f'Connecting to InfluxDB 2 url: {url}, org: {org}, bucket: {bucket}' ) self.client = InfluxDBClientV2(url=url, token=token, org=org) self.write_api = self.client.write_api(write_options=SYNCHRONOUS) self.bucket = bucket if self.client: print("Connected successfully to InfluxDB")
def get_influx_connection(conf, test_write=False, test_read=False): """Create the correct influx connection for the API version.""" kwargs = { CONF_TIMEOUT: TIMEOUT, } precision = conf.get(CONF_PRECISION) if conf[CONF_API_VERSION] == API_VERSION_2: kwargs[CONF_URL] = conf[CONF_URL] kwargs[CONF_TOKEN] = conf[CONF_TOKEN] kwargs[INFLUX_CONF_ORG] = conf[CONF_ORG] kwargs[CONF_VERIFY_SSL] = conf[CONF_VERIFY_SSL] if CONF_SSL_CA_CERT in conf: kwargs[CONF_SSL_CA_CERT] = conf[CONF_SSL_CA_CERT] bucket = conf.get(CONF_BUCKET) influx = InfluxDBClientV2(**kwargs) query_api = influx.query_api() initial_write_mode = SYNCHRONOUS if test_write else ASYNCHRONOUS write_api = influx.write_api(write_options=initial_write_mode) def write_v2(json): """Write data to V2 influx.""" data = {"bucket": bucket, "record": json} if precision is not None: data["write_precision"] = precision try: write_api.write(**data) except (urllib3.exceptions.HTTPError, OSError) as exc: raise ConnectionError(CONNECTION_ERROR % exc) from exc except ApiException as exc: if exc.status == CODE_INVALID_INPUTS: raise ValueError(WRITE_ERROR % (json, exc)) from exc raise ConnectionError(CLIENT_ERROR_V2 % exc) from exc def query_v2(query, _=None): """Query V2 influx.""" try: return query_api.query(query) except (urllib3.exceptions.HTTPError, OSError) as exc: raise ConnectionError(CONNECTION_ERROR % exc) from exc except ApiException as exc: if exc.status == CODE_INVALID_INPUTS: raise ValueError(QUERY_ERROR % (query, exc)) from exc raise ConnectionError(CLIENT_ERROR_V2 % exc) from exc def close_v2(): """Close V2 influx client.""" influx.close() buckets = [] if test_write: # Try to write b"" to influx. If we can connect and creds are valid # Then invalid inputs is returned. Anything else is a broken config try: write_v2(b"") except ValueError: pass write_api = influx.write_api(write_options=ASYNCHRONOUS) if test_read: tables = query_v2(TEST_QUERY_V2) if tables and tables[0].records: buckets = [bucket.values["name"] for bucket in tables[0].records] else: buckets = [] return InfluxClient(buckets, write_v2, query_v2, close_v2) # Else it's a V1 client if CONF_SSL_CA_CERT in conf and conf[CONF_VERIFY_SSL]: kwargs[CONF_VERIFY_SSL] = conf[CONF_SSL_CA_CERT] else: kwargs[CONF_VERIFY_SSL] = conf[CONF_VERIFY_SSL] if CONF_DB_NAME in conf: kwargs[CONF_DB_NAME] = conf[CONF_DB_NAME] if CONF_USERNAME in conf: kwargs[CONF_USERNAME] = conf[CONF_USERNAME] if CONF_PASSWORD in conf: kwargs[CONF_PASSWORD] = conf[CONF_PASSWORD] if CONF_HOST in conf: kwargs[CONF_HOST] = conf[CONF_HOST] if CONF_PATH in conf: kwargs[CONF_PATH] = conf[CONF_PATH] if CONF_PORT in conf: kwargs[CONF_PORT] = conf[CONF_PORT] if CONF_SSL in conf: kwargs[CONF_SSL] = conf[CONF_SSL] influx = InfluxDBClient(**kwargs) def write_v1(json): """Write data to V1 influx.""" try: influx.write_points(json, time_precision=precision) except ( requests.exceptions.RequestException, exceptions.InfluxDBServerError, OSError, ) as exc: raise ConnectionError(CONNECTION_ERROR % exc) from exc except exceptions.InfluxDBClientError as exc: if exc.code == CODE_INVALID_INPUTS: raise ValueError(WRITE_ERROR % (json, exc)) from exc raise ConnectionError(CLIENT_ERROR_V1 % exc) from exc def query_v1(query, database=None): """Query V1 influx.""" try: return list(influx.query(query, database=database).get_points()) except ( requests.exceptions.RequestException, exceptions.InfluxDBServerError, OSError, ) as exc: raise ConnectionError(CONNECTION_ERROR % exc) from exc except exceptions.InfluxDBClientError as exc: if exc.code == CODE_INVALID_INPUTS: raise ValueError(QUERY_ERROR % (query, exc)) from exc raise ConnectionError(CLIENT_ERROR_V1 % exc) from exc def close_v1(): """Close the V1 Influx client.""" influx.close() databases = [] if test_write: write_v1([]) if test_read: databases = [db["name"] for db in query_v1(TEST_QUERY_V1)] return InfluxClient(databases, write_v1, query_v1, close_v1)
#!python3 # This script migrates data from InfluxCloud to a InfluxDB v1 instance # https://github.com/influxdata/influxdb-client-python from influxdb_client import InfluxDBClient as InfluxDBClientV2 # https://github.com/influxdata/influxdb-python from influxdb import InfluxDBClient as InfluxDBClientV1 import json import pandas as pd client = InfluxDBClientV2( url="https://us-west-2-1.aws.cloud2.influxdata.com", token="", org="" ) client_local = InfluxDBClientV1(username="", password="", database="") query_api = client.query_api() records = query_api.query_stream('from(bucket:"savr") |> range(start: -7d, stop: now())') i = 0 for record in records: i += 1 json_body = [{ "measurement": record["_measurement"], "tags": { }, "time": record.get_time().isoformat(),
def __init__(self, hass, influx_conf, query, use_v2_api): """Initialize the sensor.""" self._name = query.get(CONF_NAME) self._unit_of_measurement = query.get(CONF_UNIT_OF_MEASUREMENT) value_template = query.get(CONF_VALUE_TEMPLATE) if value_template is not None: self._value_template = value_template self._value_template.hass = hass else: self._value_template = None self._state = None self._hass = hass if use_v2_api: influx = InfluxDBClientV2(**influx_conf) query_api = influx.query_api() query_clause = query.get(CONF_QUERY) query_clause.hass = hass bucket = query[CONF_BUCKET] else: if CONF_DB_NAME in query: kwargs = influx_conf.copy() kwargs[CONF_DB_NAME] = query[CONF_DB_NAME] else: kwargs = influx_conf influx = InfluxDBClient(**kwargs) where_clause = query.get(CONF_WHERE) where_clause.hass = hass query_api = None try: if query_api is not None: query_api.query( f'from(bucket: "{bucket}") |> range(start: -1ms) |> keep(columns: ["_time"]) |> limit(n: 1)' ) self.connected = True self.data = InfluxSensorDataV2( query_api, bucket, query.get(CONF_RANGE_START), query.get(CONF_RANGE_STOP), query_clause, query.get(CONF_IMPORTS), query.get(CONF_GROUP_FUNCTION), ) else: influx.query("SHOW SERIES LIMIT 1;") self.connected = True self.data = InfluxSensorDataV1( influx, query.get(CONF_GROUP_FUNCTION), query.get(CONF_FIELD), query.get(CONF_MEASUREMENT_NAME), where_clause, ) except exceptions.InfluxDBClientError as exc: _LOGGER.error( "Database host is not accessible due to '%s', please" " check your entries in the configuration file and" " that the database exists and is READ/WRITE", exc, ) self.connected = False except ApiException as exc: _LOGGER.error( "Bucket is not accessible due to '%s', please " "check your entries in the configuration file (url, org, " "bucket, etc.) and verify that the org and bucket exist and the " "provided token has READ access.", exc, ) self.connected = False
def get_influx_connection(conf, test_write=False, test_read=False): """Create the correct influx connection for the API version.""" kwargs = { CONF_TIMEOUT: TIMEOUT, } if conf[CONF_API_VERSION] == API_VERSION_2: kwargs[CONF_URL] = conf[CONF_URL] kwargs[CONF_TOKEN] = conf[CONF_TOKEN] kwargs[INFLUX_CONF_ORG] = conf[CONF_ORG] bucket = conf.get(CONF_BUCKET) influx = InfluxDBClientV2(**kwargs) query_api = influx.query_api() initial_write_mode = SYNCHRONOUS if test_write else ASYNCHRONOUS write_api = influx.write_api(write_options=initial_write_mode) def write_v2(json): """Write data to V2 influx.""" try: write_api.write(bucket=bucket, record=json) except (urllib3.exceptions.HTTPError, OSError) as exc: raise ConnectionError(CONNECTION_ERROR % exc) except ApiException as exc: if exc.status == CODE_INVALID_INPUTS: raise ValueError(WRITE_ERROR % (json, exc)) raise ConnectionError(CLIENT_ERROR_V2 % exc) def query_v2(query, _=None): """Query V2 influx.""" try: return query_api.query(query) except (urllib3.exceptions.HTTPError, OSError) as exc: raise ConnectionError(CONNECTION_ERROR % exc) except ApiException as exc: if exc.status == CODE_INVALID_INPUTS: raise ValueError(QUERY_ERROR % (query, exc)) raise ConnectionError(CLIENT_ERROR_V2 % exc) def close_v2(): """Close V2 influx client.""" influx.close() influx_client = InfluxClient(write_v2, query_v2, close_v2) if test_write: # Try to write [] to influx. If we can connect and creds are valid # Then invalid inputs is returned. Anything else is a broken config try: influx_client.write([]) except ValueError: pass write_api = influx.write_api(write_options=ASYNCHRONOUS) if test_read: influx_client.query(TEST_QUERY_V2) return influx_client # Else it's a V1 client kwargs[CONF_VERIFY_SSL] = conf[CONF_VERIFY_SSL] if CONF_DB_NAME in conf: kwargs[CONF_DB_NAME] = conf[CONF_DB_NAME] if CONF_USERNAME in conf: kwargs[CONF_USERNAME] = conf[CONF_USERNAME] if CONF_PASSWORD in conf: kwargs[CONF_PASSWORD] = conf[CONF_PASSWORD] if CONF_HOST in conf: kwargs[CONF_HOST] = conf[CONF_HOST] if CONF_PATH in conf: kwargs[CONF_PATH] = conf[CONF_PATH] if CONF_PORT in conf: kwargs[CONF_PORT] = conf[CONF_PORT] if CONF_SSL in conf: kwargs[CONF_SSL] = conf[CONF_SSL] influx = InfluxDBClient(**kwargs) def write_v1(json): """Write data to V1 influx.""" try: influx.write_points(json) except ( requests.exceptions.RequestException, exceptions.InfluxDBServerError, OSError, ) as exc: raise ConnectionError(CONNECTION_ERROR % exc) except exceptions.InfluxDBClientError as exc: if exc.code == CODE_INVALID_INPUTS: raise ValueError(WRITE_ERROR % (json, exc)) raise ConnectionError(CLIENT_ERROR_V1 % exc) def query_v1(query, database=None): """Query V1 influx.""" try: return list(influx.query(query, database=database).get_points()) except ( requests.exceptions.RequestException, exceptions.InfluxDBServerError, OSError, ) as exc: raise ConnectionError(CONNECTION_ERROR % exc) except exceptions.InfluxDBClientError as exc: if exc.code == CODE_INVALID_INPUTS: raise ValueError(QUERY_ERROR % (query, exc)) raise ConnectionError(CLIENT_ERROR_V1 % exc) def close_v1(): """Close the V1 Influx client.""" influx.close() influx_client = InfluxClient(write_v1, query_v1, close_v1) if test_write: influx_client.write([]) if test_read: influx_client.query(TEST_QUERY_V1) return influx_client