MssqlHook airflow connection

末鹿安然 提交于 2019-12-02 06:12:45

问题


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:


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.



来源:https://stackoverflow.com/questions/51859197/mssqlhook-airflow-connection

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!