0

I am trying to query data from the Influx database through pandas using the following code:

from influxdb_client import InfluxDBClient
import pandas as pd

my_token = "my_token"
my_org = "my_org"
bucket = "bucket"

query= '''
from(bucket: "bucket")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "measurement")
  |> filter(fn: (r) => r["_field"] == "count")
  |> filter(fn: (r) => r["unit"] == "unit")
  |> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)
  |> yield(name: "mean")'''

username = 'username'
password = 'password'

client = InfluxDBClient(url="https://us-west-2-1.aws.cloud2.influxdata.com/", token=f'{username}:{password}', org='my_org')

system_stats = client.query_api().query_data_frame(org=my_org, query=query)

After executing the above, I get an error: ApiException: (0) Reason: SSLError [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:997)

I am not familiar with the Influx database but I need to query some data and create a dataframe. Is the syntax I am using right? How should I fix this? Thank you!

MathMan 99
  • 665
  • 1
  • 7
  • 19

1 Answers1

1

Try with certifi:

import certifi

client = InfluxDBClient(
    url='https://us-west-2-1.aws.cloud2.influxdata.com',
    token=f'{username}:{password}', org=my_org,
    ssl_ca_cert=certifi.where()
)

Inspired by this github issue

Or use verify_ssl=False as parameter of InfluxDBClient but it's not a good idea.

Corralien
  • 109,409
  • 8
  • 28
  • 52
  • In both cases, I get the following error: ApiException: (401) Reason: Unauthorized. HTTP response body: b'{"code":"unauthorized","message":"unauthorized access"}' – MathMan 99 Feb 24 '22 at 23:21
  • Not exactly sure what that means – MathMan 99 Feb 24 '22 at 23:22
  • 1
    It means your original problem is solved :) The SSL connection is now established. However, the new problem is your token. Maybe you should pass `token=my_token` instead of `token=f'{username}:{password}'` – Corralien Feb 25 '22 at 07:56
  • Thanks for the help! Turns out the url is wrong and a different token is needed. – MathMan 99 Feb 25 '22 at 16:01