【问题标题】:Not able to use JdbcOperator Airflow无法使用 JdbcOperator 气流
【发布时间】:2020-03-24 19:30:46
【问题描述】:

我正在尝试使用 JdbcOperator 连接到配置单元表。我的代码如下:

import datetime as dt
from datetime import timedelta

import airflow
from airflow.models import DAG
from airflow.operators.jdbc_operator.JdbcOperator import JdbcOperator

args = {
    'owner': 'Airflow',
    'start_date': dt.datetime(2020, 3, 24),
    'retries': 1,
    'retry_delay': dt.timedelta(minutes=5),
}

dag_hive = DAG(dag_id="import_hive",default_args=args, schedule_interval= " 0 * * * *",dagrun_timeout=timedelta(minutes=60))
hql_query = """USE testdb;
CREATE TABLE airflow-test-table LIKE testtable;"""
hive_task = JdbcOperator(sql = hql_query, task_id="hive_script_task", jdbc_conn_id="hive_conn_default",dag=dag_hive)

hive_task

我遇到了错误

ModuleNotFoundError: 没有命名模块 'airflow.operators.jdbc_operator.JdbcOperator'; 'airflow.operators.jdbc_operator' 不是一个包

我已经交叉检查了 sitepackages 文件夹中的包,它可用。无法弄清楚为什么我会收到此错误。

【问题讨论】:

    标签: airflow airflow-scheduler


    【解决方案1】:

    通过运行以下命令安装使用 JDBC 运算符的依赖项:

    pip install 'apache-airflow[jdbc]'
    

    然后在你的 DAG 文件中导入JdbcOperator,就像提到的@mk_sta 一样,如下所示:

    from airflow.operators.jdbc_operator import JdbcOperator
    

    【讨论】:

    • 现在我收到新错误:AttributeError: type object 'java.sql.Types' has no attribute 'javaclass'。 JayDeBeapi 包中出现此错误
    • 不确定,抱歉。这是 JayDebeApi 特有的
    【解决方案2】:

    导入JdbcOperator()模块的正确方法如下:

    from airflow.operators.jdbc_operator import JdbcOperator
    

    请记住,JDBCOperator 还需要依赖的 jaydebeapi Python 包,该包需要提供给当前的 Airflow 环境。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-15
      • 1970-01-01
      • 1970-01-01
      • 2020-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-20
      相关资源
      最近更新 更多