3
votes

I am new to using airflow and what I need to do is to use MssqlHook but I do not know how. What elements should I give in the constructor?

I have a connection in airflow with name connection_test.

I do not fully understand the attributes in the class:

class MsSqlHook(DbApiHook):
    """
    Interact with Microsoft SQL Server.
    """

    conn_name_attr = 'mssql_conn_id'
    default_conn_name = 'mssql_default'
    supports_autocommit = True

I have the following code:

sqlhook=MsSqlHook(connection_test)
sqlhook.get_conn()

And when I do this the error is Connection failed for unknown reason.

How should I do in order to make it work with the airflow connection ?

What I need is to call function .get_conn() for the MsSqlHook.

1

1 Answers

4
votes

See the standard examples of Airflow.

https://github.com/gtoonstra/etl-with-airflow/blob/master/examples/mssql-example/dags/mssql_bcp_example.py

E.g.:

t1 = MsSqlImportOperator(task_id='import_data',
                         table_name='test.test',
                         generate_synth_data=generate_synth_data,
                         mssql_conn_id='mssql',
                         dag=dag)

EDIT

hook = MsSqlHook(mssql_conn_id="my_mssql_conn")
hook.run(sql)

You need to provide the connection defined in Connections. Also if using Hooks looking in the respective Operators usually yields some information about usage. This code is from the MSSQLOperator.