airflow.hooks.dbapi
¶
Module Contents¶
Classes¶
A protocol where you can connect to a database. |
|
Abstract base class for sql hooks. |
- class airflow.hooks.dbapi.ConnectorProtocol[source]¶
Bases:
airflow.typing_compat.Protocol
A protocol where you can connect to a database.
- connect(self, host: str, port: int, username: str, schema: str) Any [source]¶
Connect to a database.
- Parameters
host – The database host to connect to.
port – The database port to connect to.
username – The database username used for the authentication.
schema – The database schema to connect to.
- Returns
the authorized connection object.
- class airflow.hooks.dbapi.DbApiHook(*args, schema: Optional[str] = None, **kwargs)[source]¶
Bases:
airflow.hooks.base.BaseHook
Abstract base class for sql hooks.
- Parameters
schema (Optional[str]) – 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__()
.
- get_sqlalchemy_engine(self, engine_kwargs=None)[source]¶
Get an sqlalchemy_engine object.
- Parameters
engine_kwargs – Kwargs used in
create_engine()
.- Returns
the created engine.
- get_pandas_df(self, sql, parameters=None, **kwargs)[source]¶
Executes the sql and returns a pandas dataframe
- get_first(self, sql, parameters=None)[source]¶
Executes the sql and returns the first resulting row.
- run(self, sql, autocommit=False, parameters=None, handler=None)[source]¶
Runs a command or a list of commands. Pass a list of sql statements to the sql parameter to get them to execute sequentially
- Parameters
sql (str or list) – the sql statement to be executed (str) or a list of sql statements to execute
autocommit (bool) – What to set the connection’s autocommit setting to before executing the query.
parameters (dict or iterable) – The parameters to render the SQL query with.
handler (callable) – The result handler which is called with the result of each statement.
- Returns
query results if handler was provided.
- get_autocommit(self, conn)[source]¶
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.
- Parameters
conn (connection object.) – Connection to get autocommit setting from.
- Returns
connection autocommit setting.
- Return type
- insert_rows(self, table, rows, target_fields=None, commit_every=1000, replace=False, **kwargs)[source]¶
A generic way to insert a set of tuples into a table, a new transaction is created every commit_every rows
- Parameters
table (str) – Name of the target table
rows (iterable of tuples) – The rows to insert into the table
target_fields (iterable of strings) – The names of the columns to fill in the table
commit_every (int) – The maximum number of rows to insert in one transaction. Set to 0 to insert all rows in one transaction.
replace (bool) – Whether to replace instead of insert