## Licensed to the Apache Software Foundation (ASF) under one# or more contributor license agreements. See the NOTICE file# distributed with this work for additional information# regarding copyright ownership. The ASF licenses this file# to you under the Apache License, Version 2.0 (the# "License"); you may not use this file except in compliance# with the License. You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing,# software distributed under the License is distributed on an# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY# KIND, either express or implied. See the License for the# specific language governing permissions and limitations# under the License.fromcontextlibimportclosingfromdatetimeimportdatetimefromtypingimportAny,Optionalfromurllib.parseimportquote_plus,urlunsplitfromsqlalchemyimportcreate_enginefromairflow.exceptionsimportAirflowExceptionfromairflow.hooks.baseimportBaseHookfromairflow.typing_compatimportProtocol
[docs]classConnectorProtocol(Protocol):"""A protocol where you can connect to a database."""
""" Connect to a database. :param host: The database host to connect to. :param port: The database port to connect to. :param username: The database username used for the authentication. :param schema: The database schema to connect to. :return: the authorized connection object. """########################################################################################## ## Note! Be extra careful when changing this file. This hook is used as a base for ## a number of DBApi-related hooks and providers depend on the methods implemented ## here. Whatever you add here, has to backwards compatible unless ## `>=<Airflow version>` is added to providers' requirements using the new feature ## ##########################################################################################
[docs]classDbApiHook(BaseHook):""" Abstract base class for sql hooks. :param schema: Optional DB schema that overrides the schema specified in the connection. Make sure that if you change the schema parameter value in the constructor of the derived Hook, such change should be done before calling the ``DBApiHook.__init__()``. :type schema: Optional[str] """# Override to provide the connection name.
def__init__(self,*args,schema:Optional[str]=None,**kwargs):super().__init__()ifnotself.conn_name_attr:raiseAirflowException("conn_name_attr is not defined")eliflen(args)==1:setattr(self,self.conn_name_attr,args[0])elifself.conn_name_attrnotinkwargs:setattr(self,self.conn_name_attr,self.default_conn_name)else:setattr(self,self.conn_name_attr,kwargs[self.conn_name_attr])# We should not make schema available in deriving hooks for backwards compatibility# If a hook deriving from DBApiHook has a need to access schema, then it should retrieve it# from kwargs and store it on its own. We do not run "pop" here as we want to give the# Hook deriving from the DBApiHook to still have access to the field in it's constructorself.__schema=schema
[docs]defget_conn(self):"""Returns a connection object"""db=self.get_connection(getattr(self,self.conn_name_attr))returnself.connector.connect(host=db.host,port=db.port,username=db.login,schema=db.schema)
[docs]defget_uri(self)->str:""" Extract the URI from the connection. :return: the extracted uri. """conn=self.get_connection(getattr(self,self.conn_name_attr))login=''ifconn.login:login=f'{quote_plus(conn.login)}:{quote_plus(conn.password)}@'host=conn.hostifconn.portisnotNone:host+=f':{conn.port}'schema=self.__schemaorconn.schemaor''returnurlunsplit((conn.conn_type,f'{login}{host}',schema,'',''))
[docs]defget_sqlalchemy_engine(self,engine_kwargs=None):""" Get an sqlalchemy_engine object. :param engine_kwargs: Kwargs used in :func:`~sqlalchemy.create_engine`. :return: the created engine. """ifengine_kwargsisNone:engine_kwargs={}returncreate_engine(self.get_uri(),**engine_kwargs)
[docs]defget_pandas_df(self,sql,parameters=None,**kwargs):""" Executes the sql and returns a pandas dataframe :param sql: the sql statement to be executed (str) or a list of sql statements to execute :type sql: str or list :param parameters: The parameters to render the SQL query with. :type parameters: dict or iterable :param kwargs: (optional) passed into pandas.io.sql.read_sql method :type kwargs: dict """try:frompandas.ioimportsqlaspsqlexceptImportError:raiseException("pandas library not installed, run: pip install 'apache-airflow[pandas]'.")withclosing(self.get_conn())asconn:returnpsql.read_sql(sql,con=conn,params=parameters,**kwargs)
[docs]defget_records(self,sql,parameters=None):""" Executes the sql and returns a set of records. :param sql: the sql statement to be executed (str) or a list of sql statements to execute :type sql: str or list :param parameters: The parameters to render the SQL query with. :type parameters: dict or iterable """withclosing(self.get_conn())asconn:withclosing(conn.cursor())ascur:ifparametersisnotNone:cur.execute(sql,parameters)else:cur.execute(sql)returncur.fetchall()
[docs]defget_first(self,sql,parameters=None):""" Executes the sql and returns the first resulting row. :param sql: the sql statement to be executed (str) or a list of sql statements to execute :type sql: str or list :param parameters: The parameters to render the SQL query with. :type parameters: dict or iterable """withclosing(self.get_conn())asconn:withclosing(conn.cursor())ascur:ifparametersisnotNone:cur.execute(sql,parameters)else:cur.execute(sql)returncur.fetchone()
[docs]defrun(self,sql,autocommit=False,parameters=None,handler=None):""" Runs a command or a list of commands. Pass a list of sql statements to the sql parameter to get them to execute sequentially :param sql: the sql statement to be executed (str) or a list of sql statements to execute :type sql: str or list :param autocommit: What to set the connection's autocommit setting to before executing the query. :type autocommit: bool :param parameters: The parameters to render the SQL query with. :type parameters: dict or iterable :param handler: The result handler which is called with the result of each statement. :type handler: callable :return: query results if handler was provided. """scalar=isinstance(sql,str)ifscalar:sql=[sql]withclosing(self.get_conn())asconn:ifself.supports_autocommit:self.set_autocommit(conn,autocommit)withclosing(conn.cursor())ascur:results=[]forsql_statementinsql:self._run_command(cur,sql_statement,parameters)ifhandlerisnotNone:result=handler(cur)results.append(result)# If autocommit was set to False for db that supports autocommit,# or if db does not supports autocommit, we do a manual commit.ifnotself.get_autocommit(conn):conn.commit()ifhandlerisNone:returnNoneifscalar:returnresults[0]returnresults
def_run_command(self,cur,sql_statement,parameters):"""Runs a statement using an already open cursor."""self.log.info("Running statement: %s, parameters: %s",sql_statement,parameters)ifparameters:cur.execute(sql_statement,parameters)else:cur.execute(sql_statement)# According to PEP 249, this is -1 when query result is not applicable.ifcur.rowcount>=0:self.log.info("Rows affected: %s",cur.rowcount)
[docs]defset_autocommit(self,conn,autocommit):"""Sets the autocommit flag on the connection"""ifnotself.supports_autocommitandautocommit:self.log.warning("%s connection doesn't support autocommit but autocommit activated.",getattr(self,self.conn_name_attr),)conn.autocommit=autocommit
[docs]defget_autocommit(self,conn):""" Get autocommit setting for the provided connection. Return True if conn.autocommit is set to True. Return False if conn.autocommit is not set or set to False or conn does not support autocommit. :param conn: Connection to get autocommit setting from. :type conn: connection object. :return: connection autocommit setting. :rtype: bool """returngetattr(conn,'autocommit',False)andself.supports_autocommit
[docs]defget_cursor(self):"""Returns a cursor"""returnself.get_conn().cursor()
@staticmethoddef_generate_insert_sql(table,values,target_fields,replace,**kwargs):""" Static helper method that generate the INSERT SQL statement. The REPLACE variant is specific to MySQL syntax. :param table: Name of the target table :type table: str :param values: The row to insert into the table :type values: tuple of cell values :param target_fields: The names of the columns to fill in the table :type target_fields: iterable of strings :param replace: Whether to replace instead of insert :type replace: bool :return: The generated INSERT or REPLACE SQL statement :rtype: str """placeholders=["%s",]*len(values)iftarget_fields:target_fields=", ".join(target_fields)target_fields=f"({target_fields})"else:target_fields=''ifnotreplace:sql="INSERT INTO "else:sql="REPLACE INTO "sql+=f"{table}{target_fields} VALUES ({','.join(placeholders)})"returnsql
[docs]definsert_rows(self,table,rows,target_fields=None,commit_every=1000,replace=False,**kwargs):""" A generic way to insert a set of tuples into a table, a new transaction is created every commit_every rows :param table: Name of the target table :type table: str :param rows: The rows to insert into the table :type rows: iterable of tuples :param target_fields: The names of the columns to fill in the table :type target_fields: iterable of strings :param commit_every: The maximum number of rows to insert in one transaction. Set to 0 to insert all rows in one transaction. :type commit_every: int :param replace: Whether to replace instead of insert :type replace: bool """i=0withclosing(self.get_conn())asconn:ifself.supports_autocommit:self.set_autocommit(conn,False)conn.commit()withclosing(conn.cursor())ascur:fori,rowinenumerate(rows,1):lst=[]forcellinrow:lst.append(self._serialize_cell(cell,conn))values=tuple(lst)sql=self._generate_insert_sql(table,values,target_fields,replace,**kwargs)self.log.debug("Generated sql: %s",sql)cur.execute(sql,values)ifcommit_everyandi%commit_every==0:conn.commit()self.log.info("Loaded %s rows into %s so far",i,table)conn.commit()self.log.info("Done loading. Loaded a total of %s rows",i)
@staticmethoddef_serialize_cell(cell,conn=None):""" Returns the SQL literal of the cell as a string. :param cell: The cell to insert into the table :type cell: object :param conn: The database connection :type conn: connection object :return: The serialized cell :rtype: str """ifcellisNone:returnNoneifisinstance(cell,datetime):returncell.isoformat()returnstr(cell)
[docs]defbulk_dump(self,table,tmp_file):""" Dumps a database table into a tab-delimited file :param table: The name of the source table :type table: str :param tmp_file: The path of the target file :type tmp_file: str """raiseNotImplementedError()
[docs]defbulk_load(self,table,tmp_file):""" Loads a tab-delimited file into a database table :param table: The name of the target table :type table: str :param tmp_file: The path of the file to load into the table :type tmp_file: str """raiseNotImplementedError()
[docs]deftest_connection(self):"""Tests the connection by executing a select 1 query"""status,message=False,''try:withclosing(self.get_conn())asconn:withclosing(conn.cursor())ascur:cur.execute("select 1")ifcur.fetchone():status=Truemessage='Connection successfully tested'exceptExceptionase:status=Falsemessage=str(e)returnstatus,message