airflow.providers.jdbc.hooks.jdbc

Module Contents

Classes

JdbcHook

General hook for JDBC access.

Functions

suppress_and_warn(*exceptions)

Context manager that suppresses the given exceptions and logs a warning message.

airflow.providers.jdbc.hooks.jdbc.suppress_and_warn(*exceptions)[source]

Context manager that suppresses the given exceptions and logs a warning message.

class airflow.providers.jdbc.hooks.jdbc.JdbcHook(*args, driver_path=None, driver_class=None, **kwargs)[source]

Bases: airflow.providers.common.sql.hooks.sql.DbApiHook

General hook for JDBC access.

JDBC URL, username and password will be taken from the predefined connection. Note that the whole JDBC URL must be specified in the “host” field in the DB. Raises an airflow error if the given connection id doesn’t exist.

To configure driver parameters, you can use the following methods:
  1. Supply them as constructor arguments when instantiating the hook.

  2. Set the “driver_path” and/or “driver_class” parameters in the “hook_params” dictionary when creating the hook using SQL operators.

  3. Set the “driver_path” and/or “driver_class” extra in the connection and correspondingly enable the “allow_driver_path_in_extra” and/or “allow_driver_class_in_extra” options in the “providers.jdbc” section of the Airflow configuration. If you’re enabling these options in Airflow configuration, you should make sure that you trust the users who can edit connections in the UI to not use it maliciously.

  4. Define the “sqlalchemy_scheme” property in the extra of the connection if you want to use the SQLAlchemy engine from the JdbcHook. When using the JdbcHook, the “sqlalchemy_scheme” will by default have the “jdbc” value, which is a protocol, not a database scheme or dialect. So in order to be able to use SQLAlchemy with the JdbcHook, you need to define the “sqlalchemy_scheme” property in the extra of the connection.

  5. Patch the JdbcHook.default_driver_path and/or JdbcHook.default_driver_class values in the local_settings.py file.

See JDBC connection for full documentation.

Parameters
  • args – passed to DbApiHook

  • driver_path (str | None) – path to the JDBC driver jar file. See above for more info

  • driver_class (str | None) – name of the JDBC driver class. See above for more info

  • kwargs – passed to DbApiHook

property driver_path: str | None[source]
property driver_class: str | None[source]
property sqlalchemy_url: sqlalchemy.engine.URL[source]

Return a Sqlalchemy.engine.URL object from the connection.

Needs to be implemented in the provider subclass to return the sqlalchemy.engine.URL object.

Returns

the extracted sqlalchemy.engine.URL object.

Return type

sqlalchemy.engine.URL

conn_name_attr = 'jdbc_conn_id'[source]
default_conn_name = 'jdbc_default'[source]
conn_type = 'jdbc'[source]
hook_name = 'JDBC Connection'[source]
supports_autocommit = True[source]
default_driver_path: str | None[source]
default_driver_class: str | None[source]
classmethod get_ui_field_behaviour()[source]

Get custom field behaviour.

get_sqlalchemy_engine(engine_kwargs=None)[source]

Get an sqlalchemy_engine object.

Parameters

engine_kwargs – Kwargs used in create_engine().

Returns

the created engine.

get_conn()[source]

Return a connection object.

set_autocommit(conn, autocommit)[source]

Set autocommit for the given connection.

Parameters
  • conn (jaydebeapi.Connection) – The connection.

  • autocommit (bool) – The connection’s autocommit setting.

get_autocommit(conn)[source]

Get autocommit setting for the provided connection.

Parameters

conn (jaydebeapi.Connection) – Connection to get autocommit setting from.

Returns

connection autocommit setting. True if autocommit is set to True on the connection. False if it is either not set, set to False, or the connection does not support auto-commit.

Return type

bool

Was this entry helpful?