You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by GitBox <gi...@apache.org> on 2021/10/29 14:18:54 UTC

[GitHub] [airflow] cdabella opened a new issue #19313: JdbcOperator should pass handler parameter to JdbcHook.run

cdabella opened a new issue #19313:
URL: https://github.com/apache/airflow/issues/19313


   ### Apache Airflow version
   
   2.2.0 (latest released)
   
   ### Operating System
   
   Debian GNU/Linux 11 (bullseye)
   
   ### Versions of Apache Airflow Providers
   
   apache-airflow-providers-jdbc==1!2.0.1
   
   ### Deployment
   
   Astronomer
   
   ### Deployment details
   
   Astro CLI Version: 0.25.4, Git Commit: e95011fbf800fda9fdef1dc1d5149d62bc017aed
   
   ### What happened
   
   The execute method calls the [hook run method](https://github.com/apache/airflow/blob/20847fdbf8ecd3be394d24d47ce151c26d018ea1/airflow/providers/jdbc/operators/jdbc.py#L70) without passing any optional `handler` parameter to the DbApiHook parent class of JdbcHook.
   
   ### What you expected to happen
   
   Without the handler, the results of the DbApiHook, and the JdbcOperator, [will always be an empty list](https://github.com/apache/airflow/blob/20847fdbf8ecd3be394d24d47ce151c26d018ea1/airflow/hooks/dbapi.py#L206). In the case of the JdbcOperator, the underlying `JayDeBeApi` connection uses a handler of the form `lambda x: x.fetchall()` to return results.
   
   ### How to reproduce
   
   1. Using the astro cli
   2. Download [postgresql-42.3.0.jar](https://jdbc.postgresql.org/download/postgresql-42.3.0.jar) into the working directory
   3. Download [zulu11.52.13-ca-jdk11.0.13-linux_x64.tar.gz](https://cdn.azul.com/zulu/bin/zulu11.52.13-ca-jdk11.0.13-linux_x64.tar.gz) to the working directory
   4. Copy the following to the Dockerfile
   ```
   FROM quay.io/astronomer/ap-airflow:2.2.0-buster-onbuild
   
   COPY postgresql-42.3.0.jar /usr/local/airflow/.
   
   USER root
   RUN cd /opt && mkdir java
   COPY zulu11.52.13-ca-jdk11.0.13-linux_x64.tar.gz /opt/java
   RUN cd /opt/java && pwd && ls && tar xfvz ./zulu11.52.13-ca-jdk11.0.13-linux_x64.tar.gz
   
   ENV JAVA_HOME /opt/java/zulu11.52.13-ca-jdk11.0.13-linux_x64
   RUN export JAVA_HOME
   ```
   5.  Copy the following into the `airflow_settings.yaml` file
   ```yml
   airflow:
     connections:
       - conn_id: my_jdbc_connection
         conn_type: jdbc
         conn_host: "jdbc:postgresql://postgres:5432/"
         conn_schema:
         conn_login: postgres
         conn_password: postgres
         conn_port: 
         conn_extra: '{"extra__jdbc__drv_clsname":"org.postgresql.Driver", "extra__jdbc__drv_path":"/usr/local/airflow/postgresql-42.3.0.jar"}'
   ```
   6. Copy the following DAG and run it. No data will be passed to XCOM
   ```python
   from datetime import datetime, timedelta
   
   from airflow import DAG
   from airflow.operators.dummy import DummyOperator
   from airflow.providers.jdbc.operators.jdbc import JdbcOperator
   
   with DAG(
       dag_id='example_jdbc_operator',
       schedule_interval=None,
       start_date=datetime(2021, 1, 1),
       dagrun_timeout=timedelta(minutes=60),
       tags=['example'],
       catchup=False,
   ) as dag:
   
       run_this_last = DummyOperator(task_id='run_this_last')
   
       query_dags_data = JdbcOperator(
           task_id='query_dags_data',
           sql='SELECT dag_id, is_active FROM dag',
           jdbc_conn_id='my_jdbc_connection',
           autocommit=True,
       )
   
       query_dags_data >> run_this_last 
   ```
   7. Copy the following DAG and run it. All DAGs and their active status will be put onto XCOM
   ```python
   from datetime import datetime, timedelta
   
   from airflow import DAG
   from airflow.operators.dummy import DummyOperator
   from airflow.providers.jdbc.operators.jdbc import JdbcOperator
   from airflow.providers.jdbc.hooks.jdbc import JdbcHook
   
   
   class JdbcHandlerOperator(JdbcOperator):
       def execute(self, context) -> None:
           self.log.info('Executing: %s', self.sql)
           hook = JdbcHook(jdbc_conn_id=self.jdbc_conn_id)
           return hook.run(
               self.sql,
               self.autocommit,
               parameters=self.parameters,
               # Defined by how JayDeBeApi operates 
               handler=lambda x: x.fetchall()
           )
   
   
   with DAG(
       dag_id='example_jdbc_operator',
       schedule_interval=None,
       start_date=datetime(2021, 1, 1),
       dagrun_timeout=timedelta(minutes=60),
       tags=['example'],
       catchup=False,
   ) as dag:
   
       run_this_last = DummyOperator(task_id='run_this_last')
   
       query_dags_data = JdbcHandlerOperator(
           task_id='query_dags_data',
           sql='SELECT dag_id, is_active FROM dag',
           jdbc_conn_id='my_jdbc_connection',
           autocommit=True,
       )
   
       query_dags_data >> run_this_last 
   ```
   
   ### Anything else
   
   _No response_
   
   ### Are you willing to submit PR?
   
   - [X] Yes I am willing to submit a PR!
   
   ### Code of Conduct
   
   - [X] I agree to follow this project's [Code of Conduct](https://github.com/apache/airflow/blob/main/CODE_OF_CONDUCT.md)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] cdabella commented on issue #19313: JdbcOperator should pass handler parameter to JdbcHook.run

Posted by GitBox <gi...@apache.org>.
cdabella commented on issue #19313:
URL: https://github.com/apache/airflow/issues/19313#issuecomment-1014930590


   Bit of life got in the way and I have not implemented the fix. Please feel free @nkyuray


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] nkyuray commented on issue #19313: JdbcOperator should pass handler parameter to JdbcHook.run

Posted by GitBox <gi...@apache.org>.
nkyuray commented on issue #19313:
URL: https://github.com/apache/airflow/issues/19313#issuecomment-1015875206


   Hi @cdabella,
   Thanks. 
   
   Hi @eladkal I am going to work on this at the weekend. 
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] eladkal commented on issue #19313: JdbcOperator should pass handler parameter to JdbcHook.run

Posted by GitBox <gi...@apache.org>.
eladkal commented on issue #19313:
URL: https://github.com/apache/airflow/issues/19313#issuecomment-1007421088


   @cdabella are you still working on this issue?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] eladkal commented on issue #19313: JdbcOperator should pass handler parameter to JdbcHook.run

Posted by GitBox <gi...@apache.org>.
eladkal commented on issue #19313:
URL: https://github.com/apache/airflow/issues/19313#issuecomment-961873992


   @cdabella assigned the issue to you, feel free to open PR


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] eladkal commented on issue #19313: JdbcOperator should pass handler parameter to JdbcHook.run

Posted by GitBox <gi...@apache.org>.
eladkal commented on issue #19313:
URL: https://github.com/apache/airflow/issues/19313#issuecomment-1007421088


   @cdabella are you still working on this issue?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] eladkal commented on issue #19313: JdbcOperator should pass handler parameter to JdbcHook.run

Posted by GitBox <gi...@apache.org>.
eladkal commented on issue #19313:
URL: https://github.com/apache/airflow/issues/19313#issuecomment-961873992


   @cdabella assigned the issue to you, feel free to open PR


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] eladkal commented on issue #19313: JdbcOperator should pass handler parameter to JdbcHook.run

Posted by GitBox <gi...@apache.org>.
eladkal commented on issue #19313:
URL: https://github.com/apache/airflow/issues/19313#issuecomment-961873992


   @cdabella assigned the issue to you, feel free to open PR


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] nkyuray edited a comment on issue #19313: JdbcOperator should pass handler parameter to JdbcHook.run

Posted by GitBox <gi...@apache.org>.
nkyuray edited a comment on issue #19313:
URL: https://github.com/apache/airflow/issues/19313#issuecomment-1015875206


   Hi @cdabella,
   Thanks. 
   
   Hi @eladkal I am going to work on this at the weekend just wanted to let you know.
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] nkyuray commented on issue #19313: JdbcOperator should pass handler parameter to JdbcHook.run

Posted by GitBox <gi...@apache.org>.
nkyuray commented on issue #19313:
URL: https://github.com/apache/airflow/issues/19313#issuecomment-1014929673


   Hi @eladkal,
   
   If @cdabella will not take this, I would like to give a try. 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org