【问题标题】:How to connect Cloud Composer Airflow to Oracle database?如何将 Cloud Composer Airflow 连接到 Oracle 数据库?
【发布时间】:2022-01-04 15:48:54
【问题描述】:

我正在尝试通过 GCP 气流 使用 python 作业连接到 oracle 数据库

即使我用 OracleOperatorOracleHook 都试过这个,我总是得到同样的错误。

您可以在下面看到错误和我的代码。 如何解决 GCP Cloud Composer 上的此错误?

请注意,我没有在本地计算机上执行这些操作。

另外,我如何打印发送到 oracle 数据库的选择查询的结果? 提前致谢。

我的代码:

from airflow.utils.dates import days_ago
from datetime import datetime,timedelta
from airflow import DAG
from airflow.operators.oracle_operator import OracleOperator

default_args = {
'owner': 'Airflow_Test',
'depends_on_past': False,
'start_date':  days_ago(1),   
'email': ['Airflow_Test@Airflow_Test.com'],
'email_on_failure': True,
'email_on_retry': False,
'retries': 1,
'retry_delay': timedelta(minutes=5)
}

schedule_interval = "* * * * *"

dag_gtb = DAG(
    'Airflow_Test_Python_Job',
    default_args=default_args,
    schedule_interval=schedule_interval
    )

oracle_opr = OracleOperator(
    task_id='task_sql',
    oracle_conn_id='oracle_connection',
    sql= 'select * from temp_table',
    autocommit ='True',
    dag=dag_gtb)


oracle_opr

错误:

cx_Oracle.DatabaseError: DPI-1047: Cannot locate a 64-bit Oracle Client library: "libclntsh.so: cannot open shared object file: 
No such file or directory". See https://cx-oracle.readthedocs.io/en/latest/user_guide/installation.html for hel

【问题讨论】:

标签: python oracle google-cloud-platform airflow google-cloud-composer


【解决方案1】:

一个简单的解决方案是:

下载64位版本的oracleinstantClient

  • 将dll文件从instantClient目录复制到Python目录。

如果你还没有安装 cx_Oracle,你可以运行这个命令:

python -m pip install cx_Oracle --upgrade

此外,您可以执行这些命令来验证是否已安装所有内容。

import sqlalchemy as sqla
import pandas as pd
import cx_Oracle
 
# Test to see if it will print the version of sqlalchemy
print(sqla.__version__)    
 
# Test to see if the cx_Oracle is recognized
print(cx_Oracle.version)   
 
# This fails, but this will change after the solution
cx_Oracle.clientversion()  

要查看有关 cx_Oracle 的更多文档,请转到 here

此外,您还可以执行这些命令来更改 oracle 客户端文件的路径。

import os
import platform
 
# This is the path to the ORACLE client files
lib_dir = r"C:\put_your_path_here\instantclient-basic-windows.x64- 19.9.0.0.0dbru\instantclient_19_9"
 
# Diagnostic output to verify 64 bit arch and list files
print("ARCH:", platform.architecture())
print("FILES AT lib_dir:")
for name in os.listdir(lib_dir):
    print(name)

如果您有正确的路径,您应该会看到所有 Oracle 文件的列表,例如:(adrci.exe、oci.dll、oci.sym 等)。

如果您有正确的路径,您应该会看到诸如“adrci.exe、oci.dll、oci.sym….”之类的 Oracle 文件。这是 Python 需要能够找到 Oracle 驱动程序的位置。

您可以毫无错误地执行此代码。

# This fails, but this will change after the solution
cx_Oracle.clientversion()  

【讨论】:

  • 请注意,我没有在本地计算机上执行这些操作。如何在 Cloud Composer 上执行此操作?
  • @tarik 您可以在服务器端执行此代码“python -m pip install cx_Oracle --upgrade”。
猜你喜欢
  • 2020-11-02
  • 2016-08-25
  • 2020-12-28
  • 2022-07-05
  • 2020-08-05
  • 2020-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多