def query_data_frame_stream(self, query: str, org=None, data_frame_index: List[str] = None, params: dict = None): """ Execute synchronous Flux query and return stream of Pandas DataFrame as a Generator['pd.DataFrame']. Note that if a query returns more then one table than the client generates a DataFrame for each of them. :param query: the Flux query :param org: organization name (optional if already specified in InfluxDBClient) :param data_frame_index: the list of columns that are used as DataFrame index :param params: bind parameters :return: """ if org is None: org = self._influxdb_client.org response = self._query_api.post_query(org=org, query=self._create_query( query, self.default_dialect, params), async_req=False, _preload_content=False, _return_http_data_only=False) _parser = FluxCsvParser( response=response, serialization_mode=FluxSerializationMode.dataFrame, data_frame_index=data_frame_index) return _parser.generator()
def query(self, query: str, org=None, params: dict = None) -> List['FluxTable']: """ Execute synchronous Flux query and return result as a List['FluxTable']. :param query: the Flux query :param org: organization name (optional if already specified in InfluxDBClient) :param params: bind parameters :return: """ if org is None: org = self._influxdb_client.org response = self._query_api.post_query(org=org, query=self._create_query( query, self.default_dialect, params), async_req=False, _preload_content=False, _return_http_data_only=False) _parser = FluxCsvParser( response=response, serialization_mode=FluxSerializationMode.tables) list(_parser.generator()) return _parser.tables
def query_data_frame_stream(self, query: str, org=None, data_frame_index: List[str] = None, params: dict = None): """ Execute synchronous Flux query and return stream of Pandas DataFrame as a Generator['pd.DataFrame']. Note that if a query returns tables with differing schemas than the client generates a DataFrame for each of them. :param query: the Flux query :param str, Organization org: specifies the organization for executing the query; take the ID, Name or Organization; if it's not specified then is used default from client.org. :param data_frame_index: the list of columns that are used as DataFrame index :param params: bind parameters :return: """ org = self._org_param(org) response = self._query_api.post_query(org=org, query=self._create_query( query, self.default_dialect, params), async_req=False, _preload_content=False, _return_http_data_only=False) _parser = FluxCsvParser( response=response, serialization_mode=FluxSerializationMode.dataFrame, data_frame_index=data_frame_index, query_options=self._get_query_options()) return _parser.generator()
def retrieve_result(self, query_string): # TODO: this method is hopefully temporary until InfluxDB /api/v2/query API simplifies passing # mimic default dialect dialect = Dialect(header=True, delimiter=",", comment_prefix="#", annotations=["datatype", "group", "default"], date_time_format="RFC3339") query = Query(query=query_string, dialect=dialect, extern=self._extern) try: response = self.query_service.post_query( org=self.client.org, query=query, async_req=False, _preload_content=False, _return_http_data_only=False) _parser = FluxCsvParser( response=response, serialization_mode=FluxSerializationMode.tables) list(_parser.generator()) tables = _parser.tables except rest.ApiException as error: # TODO: log error print('Error while retrieving data:', error) tables = [] return tables
def query_stream( self, query: str, org=None, params: dict = None) -> Generator['FluxRecord', Any, None]: """ Execute synchronous Flux query and return stream of FluxRecord as a Generator['FluxRecord']. :param query: the Flux query :param str, Organization org: specifies the organization for executing the query; take the ID, Name or Organization; if it's not specified then is used default from client.org. :param params: bind parameters :return: """ org = self._org_param(org) response = self._query_api.post_query(org=org, query=self._create_query( query, self.default_dialect, params), async_req=False, _preload_content=False, _return_http_data_only=False) _parser = FluxCsvParser( response=response, serialization_mode=FluxSerializationMode.stream, query_options=self._get_query_options()) return _parser.generator()
def query_data_frame(self, query: str, org=None, data_frame_index: List[str] = None): """ Synchronously executes the Flux query and return Pandas DataFrame. Note that if a query returns more then one table than the client generates a DataFrame for each of them. :param query: the Flux query :param org: organization name (optional if already specified in InfluxDBClient) :param data_frame_index: the list of columns that are used as DataFrame index :return: """ if org is None: org = self._influxdb_client.org response = self._query_api.post_query(org=org, query=self._create_query(query, self.default_dialect), async_req=False, _preload_content=False, _return_http_data_only=False) _parser = FluxCsvParser(response=response, serialization_mode=FluxSerializationMode.dataFrame, data_frame_index=data_frame_index) _dataFrames = list(_parser.generator()) if len(_dataFrames) == 0: return DataFrame(columns=[], index=None) elif len(_dataFrames) == 1: return _dataFrames[0] else: return _dataFrames
def _parse_to_tables(data: str): fp = BytesIO(str.encode(data)) _parser = FluxCsvParser( response=HTTPResponse(fp, preload_content=False), serialization_mode=FluxSerializationMode.tables) list(_parser.generator()) tables = _parser.tables return tables
def query_stream(self, query: str, org=None) -> Generator['FluxRecord', Any, None]: """ Synchronously executes the Flux query and return stream of FluxRecord as a Generator['FluxRecord'] :param query: the Flux query :param org: organization name (optional if already specified in InfluxDBClient) :return: """ if org is None: org = self._influxdb_client.org response = self._query_api.post_query(org=org, query=self._create_query(query, self.default_dialect), async_req=False, _preload_content=False, _return_http_data_only=False) _parser = FluxCsvParser(response=response, serialization_mode=FluxSerializationMode.stream) return _parser.generator()
def _to_data_frame_stream_parser(self, data_frame_index, query_options, response, response_metadata_mode): return FluxCsvParser( response=response, serialization_mode=FluxSerializationMode.dataFrame, data_frame_index=data_frame_index, query_options=query_options, response_metadata_mode=response_metadata_mode)
def query(self, query: str, org=None, dialect=default_dialect) -> List['FluxTable']: """ Synchronously executes the Flux query and return result as a List['FluxTable'] :param query: the Flux query :param org: organization name (optional if already specified in InfluxDBClient) :param dialect: csv dialect format :return: """ if org is None: org = self._influxdb_client.org response = self._query_api.post_query(org=org, query=self._create_query(query, dialect), async_req=False, _preload_content=False, _return_http_data_only=False) consumer = FluxResponseConsumerTable() parser = FluxCsvParser() parser.parse_flux_response(response=response, cancellable=None, consumer=consumer) return consumer.tables
def _parse(data, serialization_mode, response_metadata_mode): fp = BytesIO(str.encode(data)) return FluxCsvParser(response=HTTPResponse(fp, preload_content=False), serialization_mode=serialization_mode, response_metadata_mode=response_metadata_mode)
def _to_flux_record_stream_parser(self, query_options, response, response_metadata_mode): return FluxCsvParser(response=response, serialization_mode=FluxSerializationMode.stream, query_options=query_options, response_metadata_mode=response_metadata_mode)
def _to_tables_parser(self, response, query_options, response_metadata_mode): return FluxCsvParser(response=response, serialization_mode=FluxSerializationMode.tables, query_options=query_options, response_metadata_mode=response_metadata_mode)