adx.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #
  2. # Licensed to the Apache Software Foundation (ASF) under one
  3. # or more contributor license agreements. See the NOTICE file
  4. # distributed with this work for additional information
  5. # regarding copyright ownership. The ASF licenses this file
  6. # to you under the Apache License, Version 2.0 (the
  7. # "License"); you may not use this file except in compliance
  8. # with the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing,
  13. # software distributed under the License is distributed on an
  14. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. # KIND, either express or implied. See the License for the
  16. # specific language governing permissions and limitations
  17. # under the License.
  18. #
  19. """This module contains Azure Data Explorer operators"""
  20. from typing import TYPE_CHECKING, Optional, Sequence, Union
  21. from azure.kusto.data._models import KustoResultTable
  22. from airflow.configuration import conf
  23. from airflow.models import BaseOperator
  24. from airflow.providers.microsoft.azure.hooks.adx import AzureDataExplorerHook
  25. if TYPE_CHECKING:
  26. from airflow.utils.context import Context
  27. class AzureDataExplorerQueryOperator(BaseOperator):
  28. """
  29. Operator for querying Azure Data Explorer (Kusto).
  30. :param query: KQL query to run (templated).
  31. :param database: Database to run the query on (templated).
  32. :param options: Optional query options. See:
  33. https://docs.microsoft.com/en-us/azure/kusto/api/netfx/request-properties#list-of-clientrequestproperties
  34. :param azure_data_explorer_conn_id: Reference to the
  35. :ref:`Azure Data Explorer connection<howto/connection:adx>`.
  36. """
  37. ui_color = '#00a1f2'
  38. template_fields: Sequence[str] = ('query', 'database')
  39. template_ext: Sequence[str] = ('.kql',)
  40. def __init__(
  41. self,
  42. *,
  43. query: str,
  44. database: str,
  45. options: Optional[dict] = None,
  46. azure_data_explorer_conn_id: str = 'azure_data_explorer_default',
  47. **kwargs,
  48. ) -> None:
  49. super().__init__(**kwargs)
  50. self.query = query
  51. self.database = database
  52. self.options = options
  53. self.azure_data_explorer_conn_id = azure_data_explorer_conn_id
  54. def get_hook(self) -> AzureDataExplorerHook:
  55. """Returns new instance of AzureDataExplorerHook"""
  56. return AzureDataExplorerHook(self.azure_data_explorer_conn_id)
  57. def execute(self, context: "Context") -> Union[KustoResultTable, str]:
  58. """
  59. Run KQL Query on Azure Data Explorer (Kusto).
  60. Returns `PrimaryResult` of Query v2 HTTP response contents
  61. (https://docs.microsoft.com/en-us/azure/kusto/api/rest/response2)
  62. """
  63. hook = self.get_hook()
  64. response = hook.run_query(self.query, self.database, self.options)
  65. if conf.getboolean('core', 'enable_xcom_pickling'):
  66. return response.primary_results[0]
  67. else:
  68. return str(response.primary_results[0])