0

I have created a database in InfluxD using Python with a code like this:

from influxdb import InfluxDBClient

host     = 'xx.xxx.xx.xxx'
port     = 8086
username = 'user'
password = 'password'
database = 'dbname'

client = InfluxDBClient(host       = host, 
                        port       = port, 
                        username   = username, 
                        password   = password,
                        verify_ssl = True)

client.create_database(database)

I can see that the database has been created correctly by using:

client.get_list_database()

Result:

[{'name':'dbname'}]

After that, I sent some data (time series) to the database with the next code. As you can see, the data is stored in a data table (measurement) called Hourly_data. I have checked that the information has been stored in the database by using Grafana. Just to let you know, the data that I sent to the database (dataframe in the code) is a dataframe with a datetime index called date and several time series for different variables.

def data_to_influxdb(data,table_name):
    connection_info = {'host'         : "XX.XXX.XX.XXX",
                       'port'        : 8086,
                       'user'        : 'user',
                       'password'    : 'password',
                       'database'    : 'dbname',
                       'server_tz'   : 'Atlantic/Canary',
                       'reference_tz': 'UTC'}

    client = DataFrameClient(connection_info['host'], connection_info['port'], \
                            connection_info['user'], connection_info['password'], \
                            connection_info['database'])
    
    client.write_points(data, measurement=table_name, \
                        database=connection_info['database'], protocol='json',
                        batch_size = 20000)
    
    # Close session
    client.close()
    
    return


table_name = 'Hourly_data'
data_to_influxdb(dataFrame, tablename)

I want to get the date of the most recent data in the database (the date corresponding to the last written point). The problem comes when I try to query some data from the database using the DataFrameClient. I always get an empty dictionary, no matters what I code. I have tried with this code and several modifications, but nothing works:

connection_info = {'host'        : "XX.XXX.XX.XXX",
                    'port'        : 8086,
                    'user'        : 'user',
                    'password'    : 'password',
                    'database'    : 'dbname'}

client = DataFrameClient(
                         host       = connection_info['host'], 
                         port       = connection_info['port'],
                         username   = connection_info['user'], 
                         password   = connection_info['password'], 
                         database   = connection_info['database'],
                         )

query = 'SELECT "*" FROM "dbname"."autogen"."Hourly_data"'
client.query(query = query)

This is what I get: {}

Other alternatives that I have used:

query = 'SELECT * FROM dbname'
query = 'SELECT * FROM XXX' # wrong name on purpose also turns out into an empty dict {}

I checked the retention policy and it seems to be fine:

client.query('SHOW RETENTION POLICIES')
ResultSet({'('results', None)': [{'name': 'autogen', 'duration': '0s', 'shardGroupDuration': '168h0m0s', 'replicaN': 1, 'default': True}]})
Josue9740
  • 1
  • 1

1 Answers1

0

I finally solved the problem by using this query, in which I use the name of the measurement or data table instead of that of the database:

query = 'SELECT * from "Hourly_data"'

I guess it is related with the fact that I am specifying the database name ('dbname') in the DataFrameClient connection, so when accessing to the data it is only necessary to specify the measurment name ('Hourly_data'). Otherwise, the DataFrameClient would be looking for a measurement called 'dbname'.

Josue9740
  • 1
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 16 '23 at 09:57