def get_hook(self) -> Optional[Union[MsSqlHook, 'DbApiHook']]: """ Will retrieve hook as determined by :meth:`~.Connection.get_hook` if one is defined, and :class:`~.MsSqlHook` otherwise. For example, if the connection ``conn_type`` is ``'odbc'``, :class:`~.OdbcHook` will be used. """ if not self._hook: conn = MsSqlHook.get_connection(conn_id=self.mssql_conn_id) try: self._hook = conn.get_hook() self._hook.schema = self.database # type: ignore[union-attr] except AirflowException: self._hook = MsSqlHook(mssql_conn_id=self.mssql_conn_id, schema=self.database) return self._hook
def get_hook(self): """ Will retrieve hook as determined by Connection. If conn_type is ``'odbc'``, will use :py:class:`~airflow.providers.odbc.hooks.odbc.OdbcHook`. Otherwise, :py:class:`~airflow.providers.microsoft.mssql.hooks.mssql.MsSqlHook` will be used. """ if not self._hook: conn = MsSqlHook.get_connection(conn_id=self.mssql_conn_id) try: self._hook: Union[MsSqlHook, OdbcHook] = conn.get_hook() self._hook.schema = self.database except AirflowException: self._hook = MsSqlHook(mssql_conn_id=self.mssql_conn_id, schema=self.database) return self._hook
def __init__(self, batch_size=20): self.conn = MsSqlHook.get_connection(conn_id="mssql_test_db") self.batch_size = batch_size
def extract(**kwargs): conn = MsSqlHook.get_connection(conn_id="mssql_test_db") hook = conn.get_hook() df = hook.get_pandas_df(sql="SELECT top 5 * FROM dbo.drugs") #do whatever you need on the df print(df)