Query data from influxdb and store in a python data frame

I just started using Influxdb client for python and I’m kind of lost on how to access influxdb data base (on a remote server) in order to query some data from it and store the imported data in a data frame.

I found in the documentation, the following lines to access influxdb of a remote host and to query the data:

client = InfluxDBClient(host=‘mydomain.com’, port=8086, username=‘myuser’, password=‘mypass’ ssl=True, verify_ssl=True)

client.query(‘SELECT “duration” FROM “pyexample”.“autogen”.“brushEvents” WHERE time > now() - 4d GROUP BY “user”’)

My problem:

1- I don’t know how to get the ‘host’ and ‘port’ values for the remote host.
2-I don’t know how to store the imported data in a python data frame.

Thanks in advance

Hello @ThePheonix,
What version of InfluxDB are you using? Just checking are you using this client, it’s the only one that is supported? GitHub - influxdata/influxdb-client-python: InfluxDB 2.0 python client

Please look at InfluxDB 1.8 compatibility:

How and where are you running InfluxDB remotely? If you’re using InfluxDB Cloud for example, your client would look like this for 1.8:
username = ‘username’
password = ‘password’

database = ‘telegraf’
retention_policy = ‘autogen’

bucket = f’{database}/{retention_policy}’
client = InfluxDBClient(url=“https://us-west-2-1.aws.cloud2.influxdata.com/”, token=f’{username}:{password}‘, org=’-')

hi @Anaisdg thank you for your answer :slight_smile:
I am using InfluxDB version 1.8.2.
I can access a remote server using TeamViewer, there it’s easy for me to access the databases on influxdb.
However I would like to access these databases from my local machine (without logging in on TeamViewer) using the InfluxDB python client. I can get the IP address of the remote server.
But if I try to do this:

 from influxdb import InfluxDBClient, DataFrameClient  
   client=InfluxDBClient(host="169.XXXXXX",port=8086)
   client.get_list_database()   

I get this error:

ConnectionError: HTTPConnectionPool(host=‘169.XXXXXX’, port=8086): Max retries exceeded with url: /query?q=SHOW+DATABASES (Caused by NewConnectionError(‘<urllib3.connection.HTTPConnection object at 0x000001FC55786BC8>: Failed to establish a new connection: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond’))

Do you have any ideas, what could be the problem?

Hello. @ThePheonix,
It looks like you’re using the wrong client. Can you please try using the client above instead?
Thank you.

When using ssl=True, you need to use port=443. As I recall when using ssl=True you also need to set path=‘influxdb’. Try the following initialization:

client = InfluxDBClient(host=“169.XX.XX.XX”, port=443, username=‘myuser’, password=‘mypass’, ssl=True, verify_ssl=True, path=‘influxdb’)

Now you should be able to connect (and can get the list of databases). If you can, then to get a dataframe (assuming the measurement is “brushEvents”), use (some variant of) the following code snippet:

result = client.query(query_string, database="pyexample")
if result:
    df = result["brushEvents"]

I hope this is helpful.

@BlueCorn can you please explain what path do I have to enter, Is it the remote server’s influxd.exe path or my computer’s where influxd.exe is present

any luck man, I am stuck at the same error while trying to access remotely almost like you?

The following seems to be true, based upon what I remember (from when I wrote Python code to access remote Influx databases).

The “path” variable is the path from the top level IP/Port (of the remote system) to the influx endpoint, as specified on the system On many systems, this will be https://<remote_ip_address>/influxdb.

If you can access influxdb via a browser, then you will know the path.

You should be able to enter the URL 'https://<ip>/<path>/ping?verbose=true' in a browser address bar to determine if influxdb is responding at the URL “https://<ip>/<path>”. When the influxdb endpoint is https://<ip>/<path>, the URL https://<ip>/<path>/ping?verbose=true will display a web page with the Influxdb version information. The “<path>” between the <ip> and the “ping” is the path to use during initialization of the Python client. If you get an error message (along the lines of “404 page not found”), then you do not have the correct “path”.

At least, that’s my recollection. Hopefully, I got that correct.