How to Connect Airflow to oracle database

余生颓废 提交于 2019-12-04 11:34:07

问题


I am trying to create a connection to an oracle db instance (oracle:thin) using Airflow.

According to their documentation I entered my hostname followed by port number and SID:

Host: example.com:1524/sid

filled other fields as:

Conn Type: Oracle

Schema: username ( documentation says: use your username for schema )

Login: username

Password: * * *

After connection is setup, it gives the save error code for every query that I tried to execute (ORA-12514). It seems like oracle doesn't let airflow to connect:

ORA-12514: TNS:listener does not currently know of service requested in connect descriptor

Has someone experienced the same problem before. I mean connecting to a database shouldn't be a problem for a big platform like this. Or I am probably doing something wrong. Thanks

Version: Airflow v1.7.0, Oracle11g

EDIT:

I am using the same hostname which I use in Oracle SQLDeveloper client:


回答1:


After digging into the source code, this is what finally how it worked for me:

Conn Type: Oracle

Host: example.com

schema: username

login: username

port: port number

extra: {"sid": "my sid", "dsn": "example.com"}




回答2:


You have a problem in your connection settings, either your setting is not loading properly to the oracle hook or you are missing a python package that save/load your connection settings. You can test it by hard coding your credentials.

https://github.com/airbnb/airflow/blob/master/airflow/hooks/oracle_hook.py

conn = self.get_connection(self.oracle_conn_id)
dsn = conn.extra_dejson.get('dsn', None)
sid = conn.extra_dejson.get('sid', None)
service_name = conn.extra_dejson.get('service_name', None)
if dsn and sid and not service_name:
    dsn = cx_Oracle.makedsn(dsn, conn.port, sid)
    conn = cx_Oracle.connect(conn.login, conn.password, dsn=dsn)
elif dsn and service_name and not sid:
    dsn = cx_Oracle.makedsn(dsn, conn.port, service_name=service_name)
    conn = cx_Oracle.connect(conn.login, conn.password, dsn=dsn)
else:
    conn = cx_Oracle.connect(conn.login, conn.password, conn.host)



回答3:


for service name usage, if you leave (port, schema and extra) empty, you can put the full oracle connection descriptor under Host:

(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = mysidname)))




回答4:


this worked for me in extra field

{ "dsn":"192.168.x.x" , "service_name":"some.service.name" }

I get from https://github.com/apache/airflow/blob/master/airflow/hooks/oracle_hook.py#L49




回答5:


If anyone just does not see the connection in the Ad hoc query dropdown - you need to install the adapter: pip install cx_Oracle on the airflow server.



来源:https://stackoverflow.com/questions/36945811/how-to-connect-airflow-to-oracle-database

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