blob: d19998422e88fd08df3362f6cf69fd3ead781720 [file] [log] [blame]
:py:mod:`airflow.hooks.dbapi`
=============================
.. py:module:: airflow.hooks.dbapi
Module Contents
---------------
Classes
~~~~~~~
.. autoapisummary::
airflow.hooks.dbapi.ConnectorProtocol
airflow.hooks.dbapi.DbApiHook
.. py:class:: ConnectorProtocol
Bases: :py:obj:`airflow.typing_compat.Protocol`
A protocol where you can connect to a database.
.. py:method:: connect(self, host: str, port: int, username: str, schema: str) -> Any
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.
.. py:class:: DbApiHook(*args, schema: Optional[str] = None, **kwargs)
Bases: :py:obj:`airflow.hooks.base.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]
.. py:attribute:: conn_name_attr
:annotation: :str
.. py:attribute:: default_conn_name
:annotation: = default_conn_id
.. py:attribute:: supports_autocommit
:annotation: = False
.. py:attribute:: connector
:annotation: :Optional[ConnectorProtocol]
.. py:method:: get_conn(self)
Returns a connection object
.. py:method:: get_uri(self) -> str
Extract the URI from the connection.
:return: the extracted uri.
.. py:method:: get_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.
.. py:method:: get_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
.. py:method:: get_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
.. py:method:: get_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
.. py:method:: run(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.
.. py:method:: set_autocommit(self, conn, autocommit)
Sets the autocommit flag on the connection
.. py:method:: get_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
.. py:method:: get_cursor(self)
Returns a cursor
.. py:method:: insert_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
.. py:method:: bulk_dump(self, table, tmp_file)
:abstractmethod:
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
.. py:method:: bulk_load(self, table, tmp_file)
:abstractmethod:
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
.. py:method:: test_connection(self)
Tests the connection by executing a select 1 query