Search code examples
pythoninfluxdbinfluxdb-python

How to insert current time while copying data from one measurement to another?


How to insert current time while copying data from one measurement to another. It copies all the data correctly along with the timestamp of the measurement that is being copied from.

SELECT MIN(column) as value INTO db2.retention_policy2.measurement2 FROM db1.retention_policy1.measurement1 GROUP BY column1, column2

I want the Time column in the new measurement(measurement2) to have the current time and not the time from db1.retention_policy1.measurement1. Is that possible? Thank you.

There is one possible way of converting the query into a list:

list_current_data = list(current_data.get_points())

and then updating the time this way:

for item in list_current_data:
    item['time'] = datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S.%f")[:-4]+"Z"

Finally inserting data from the updated list into a measurement. Anyone knows how to insert data from a list to a measurement?


Solution

  • Writing data from a list to a measurement can be done in the following way:

    current_data = client.query("SELECT statement")
    list_current_data = list(current_data.get_points())
    
    for data_point in current_data.get_points():
        data_to_write = [{'measurement': 'measurement_name',
                        'fields': {'value1': data_point['value1'],
                                   'value2': data_point['value2']}
                        }]
        client.write_points(data_to_write)
    

    This will append new data to the measurement, each time the script is executed.