airflow.hooks.dbapi_hook

Module Contents

class airflow.hooks.dbapi_hook.DbApiHook(*args, **kwargs)[source]

Bases: airflow.hooks.base_hook.BaseHook

Abstract base class for sql hooks.

conn_name_attr :Optional[str][source]
default_conn_name = default_conn_id[source]
supports_autocommit = False[source]
connector[source]
get_conn(self)[source]

Returns a connection object

get_uri(self)[source]
get_sqlalchemy_engine(self, engine_kwargs=None)[source]
get_pandas_df(self, sql, parameters=None)[source]

Executes the sql and returns a pandas dataframe

Parameters
  • sql (str or list) – the sql statement to be executed (str) or a list of sql statements to execute

  • parameters (mapping or iterable) – The parameters to render the SQL query with.

get_records(self, sql, parameters=None)[source]

Executes the sql and returns a set of records.

Parameters
  • sql (str or list) – the sql statement to be executed (str) or a list of sql statements to execute

  • parameters (mapping or iterable) – The parameters to render the SQL query with.

get_first(self, sql, parameters=None)[source]

Executes the sql and returns the first resulting row.

Parameters
  • sql (str or list) – the sql statement to be executed (str) or a list of sql statements to execute

  • parameters (mapping or iterable) – The parameters to render the SQL query with.

run(self, sql, autocommit=False, parameters=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 (mapping or iterable) – The parameters to render the SQL query with.

set_autocommit(self, conn, autocommit)[source]

Sets the autocommit flag on the connection

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

bool

get_cursor(self)[source]

Returns a cursor

insert_rows(self, table, rows, target_fields=None, commit_every=1000, replace=False)[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

static _serialize_cell(cell, conn=None)[source]

Returns the SQL literal of the cell as a string.

Parameters
  • cell (object) – The cell to insert into the table

  • conn (connection object) – The database connection

Returns

The serialized cell

Return type

str

bulk_dump(self, table, tmp_file)[source]

Dumps a database table into a tab-delimited file

Parameters
  • table (str) – The name of the source table

  • tmp_file (str) – The path of the target file

bulk_load(self, table, tmp_file)[source]

Loads a tab-delimited file into a database table

Parameters
  • table (str) – The name of the target table

  • tmp_file (str) – The path of the file to load into the table