瀏覽代碼

added insert_dataframe method to influx handler

tanja 3 年之前
父節點
當前提交
4c6223c959
共有 1 個文件被更改,包括 23 次插入0 次删除
  1. 23 0
      cdplib/db_handlers/InfluxdbHandler.py

+ 23 - 0
cdplib/db_handlers/InfluxdbHandler.py

@@ -115,3 +115,26 @@ class InfluxdbHandler:
                 '\' tz(\'Europe/Berlin\');'
 
         return self.query_to_dataframe(query)
+
+    def insert_dataframe(self, dataframe: pd.DataFrame,
+                         batch_size: int = 10000,
+                         time_precision: str = 'u'):
+        """
+        :param dataframe: DESCRIPTION
+        :type dataframe: pd.DataFrame
+        :return: DESCRIPTION
+        :rtype: TYPE
+
+        """
+        for column in dataframe.columns:
+            try:
+                self.client.write_points(
+                    dataframe=dataframe[column].to_frame(),
+                    measurement=column,
+                    protocol='line',
+                    batch_size=batch_size,
+                    time_precision=time_precision)
+
+            except Exception as error:
+                self._logger.loger_and_raise_error(
+                    ('Could not insert data, Error: {}'.format(error)))