Apache Airflow: print query success on logging info, query failure on logging.error











up vote
0
down vote

favorite












My question is regarding the logging of query successes or failure, done by the BigQueryOperator of Apache Airflow 1.10.0 I am wondering if it is possible to print query success on logging.info, and if it is a failure to print on logging.error?



from airflow.contrib.operators import bigquery_operator
# Query recent StackOverflow questions.
bq_recent_questions_query = bigquery_operator.BigQueryOperator(
task_id='bq_recent_questions_query',
bql="""
SELECT owner_display_name, title, view_count
FROM `bigquery-public-data.stackoverflow.posts_questions`
WHERE creation_date < CAST('{max_date}' AS TIMESTAMP)
AND creation_date >= CAST('{min_date}' AS TIMESTAMP)
ORDER BY view_count DESC
LIMIT 100
""".format(max_date=max_query_date, min_date=min_query_date),
use_legacy_sql=False,
destination_dataset_table=bq_recent_questions_table_id)


https://cloud.google.com/composer/docs/how-to/using/writing-dags










share|improve this question




























    up vote
    0
    down vote

    favorite












    My question is regarding the logging of query successes or failure, done by the BigQueryOperator of Apache Airflow 1.10.0 I am wondering if it is possible to print query success on logging.info, and if it is a failure to print on logging.error?



    from airflow.contrib.operators import bigquery_operator
    # Query recent StackOverflow questions.
    bq_recent_questions_query = bigquery_operator.BigQueryOperator(
    task_id='bq_recent_questions_query',
    bql="""
    SELECT owner_display_name, title, view_count
    FROM `bigquery-public-data.stackoverflow.posts_questions`
    WHERE creation_date < CAST('{max_date}' AS TIMESTAMP)
    AND creation_date >= CAST('{min_date}' AS TIMESTAMP)
    ORDER BY view_count DESC
    LIMIT 100
    """.format(max_date=max_query_date, min_date=min_query_date),
    use_legacy_sql=False,
    destination_dataset_table=bq_recent_questions_table_id)


    https://cloud.google.com/composer/docs/how-to/using/writing-dags










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      My question is regarding the logging of query successes or failure, done by the BigQueryOperator of Apache Airflow 1.10.0 I am wondering if it is possible to print query success on logging.info, and if it is a failure to print on logging.error?



      from airflow.contrib.operators import bigquery_operator
      # Query recent StackOverflow questions.
      bq_recent_questions_query = bigquery_operator.BigQueryOperator(
      task_id='bq_recent_questions_query',
      bql="""
      SELECT owner_display_name, title, view_count
      FROM `bigquery-public-data.stackoverflow.posts_questions`
      WHERE creation_date < CAST('{max_date}' AS TIMESTAMP)
      AND creation_date >= CAST('{min_date}' AS TIMESTAMP)
      ORDER BY view_count DESC
      LIMIT 100
      """.format(max_date=max_query_date, min_date=min_query_date),
      use_legacy_sql=False,
      destination_dataset_table=bq_recent_questions_table_id)


      https://cloud.google.com/composer/docs/how-to/using/writing-dags










      share|improve this question















      My question is regarding the logging of query successes or failure, done by the BigQueryOperator of Apache Airflow 1.10.0 I am wondering if it is possible to print query success on logging.info, and if it is a failure to print on logging.error?



      from airflow.contrib.operators import bigquery_operator
      # Query recent StackOverflow questions.
      bq_recent_questions_query = bigquery_operator.BigQueryOperator(
      task_id='bq_recent_questions_query',
      bql="""
      SELECT owner_display_name, title, view_count
      FROM `bigquery-public-data.stackoverflow.posts_questions`
      WHERE creation_date < CAST('{max_date}' AS TIMESTAMP)
      AND creation_date >= CAST('{min_date}' AS TIMESTAMP)
      ORDER BY view_count DESC
      LIMIT 100
      """.format(max_date=max_query_date, min_date=min_query_date),
      use_legacy_sql=False,
      destination_dataset_table=bq_recent_questions_table_id)


      https://cloud.google.com/composer/docs/how-to/using/writing-dags







      python google-bigquery airflow






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 at 8:32









      Meghdeep Ray

      2,50321738




      2,50321738










      asked Nov 20 at 10:20









      Paul Velthuis

      130112




      130112
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          You could make your own Operator by just copying the BigQueryOperator and making the following changes to the execute and on_kill functions inside it, or you could override the existing BigQueryOperator as well.



          def execute(self, context):
          if self.bq_cursor is None:
          self.log.info( "Beginnging Execution." )
          hook = BigQueryHook(
          bigquery_conn_id=self.bigquery_conn_id,
          use_legacy_sql=self.use_legacy_sql,
          delegate_to=self.delegate_to)
          conn = hook.get_conn()
          self.bq_cursor = conn.cursor()
          self.bq_cursor.run_query(
          self.sql,
          destination_dataset_table=self.destination_dataset_table,
          write_disposition=self.write_disposition,
          allow_large_results=self.allow_large_results,
          flatten_results=self.flatten_results,
          udf_config=self.udf_config,
          maximum_billing_tier=self.maximum_billing_tier,
          maximum_bytes_billed=self.maximum_bytes_billed,
          create_disposition=self.create_disposition,
          query_params=self.query_params,
          labels=self.labels,
          schema_update_options=self.schema_update_options,
          priority=self.priority,
          time_partitioning=self.time_partitioning
          )
          self.log.info( "Executed: %s" % self.sql )

          def on_kill(self):
          super(BigQueryOperator, self).on_kill()
          self.log.error( "Failed to Execute: %s" % self.sql )
          if self.bq_cursor is not None:
          self.log.info('Canceling running query due to execution timeout')
          self.bq_cursor.cancel_query()


          You have to put in custom operators into the plugins directory.






          share|improve this answer




























            up vote
            1
            down vote













            Looks like the code logs the query prior to execution, so the outcome is not known at the time the log is written.






            share|improve this answer





















              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53390834%2fapache-airflow-print-query-success-on-logging-info-query-failure-on-logging-er%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              0
              down vote



              accepted










              You could make your own Operator by just copying the BigQueryOperator and making the following changes to the execute and on_kill functions inside it, or you could override the existing BigQueryOperator as well.



              def execute(self, context):
              if self.bq_cursor is None:
              self.log.info( "Beginnging Execution." )
              hook = BigQueryHook(
              bigquery_conn_id=self.bigquery_conn_id,
              use_legacy_sql=self.use_legacy_sql,
              delegate_to=self.delegate_to)
              conn = hook.get_conn()
              self.bq_cursor = conn.cursor()
              self.bq_cursor.run_query(
              self.sql,
              destination_dataset_table=self.destination_dataset_table,
              write_disposition=self.write_disposition,
              allow_large_results=self.allow_large_results,
              flatten_results=self.flatten_results,
              udf_config=self.udf_config,
              maximum_billing_tier=self.maximum_billing_tier,
              maximum_bytes_billed=self.maximum_bytes_billed,
              create_disposition=self.create_disposition,
              query_params=self.query_params,
              labels=self.labels,
              schema_update_options=self.schema_update_options,
              priority=self.priority,
              time_partitioning=self.time_partitioning
              )
              self.log.info( "Executed: %s" % self.sql )

              def on_kill(self):
              super(BigQueryOperator, self).on_kill()
              self.log.error( "Failed to Execute: %s" % self.sql )
              if self.bq_cursor is not None:
              self.log.info('Canceling running query due to execution timeout')
              self.bq_cursor.cancel_query()


              You have to put in custom operators into the plugins directory.






              share|improve this answer

























                up vote
                0
                down vote



                accepted










                You could make your own Operator by just copying the BigQueryOperator and making the following changes to the execute and on_kill functions inside it, or you could override the existing BigQueryOperator as well.



                def execute(self, context):
                if self.bq_cursor is None:
                self.log.info( "Beginnging Execution." )
                hook = BigQueryHook(
                bigquery_conn_id=self.bigquery_conn_id,
                use_legacy_sql=self.use_legacy_sql,
                delegate_to=self.delegate_to)
                conn = hook.get_conn()
                self.bq_cursor = conn.cursor()
                self.bq_cursor.run_query(
                self.sql,
                destination_dataset_table=self.destination_dataset_table,
                write_disposition=self.write_disposition,
                allow_large_results=self.allow_large_results,
                flatten_results=self.flatten_results,
                udf_config=self.udf_config,
                maximum_billing_tier=self.maximum_billing_tier,
                maximum_bytes_billed=self.maximum_bytes_billed,
                create_disposition=self.create_disposition,
                query_params=self.query_params,
                labels=self.labels,
                schema_update_options=self.schema_update_options,
                priority=self.priority,
                time_partitioning=self.time_partitioning
                )
                self.log.info( "Executed: %s" % self.sql )

                def on_kill(self):
                super(BigQueryOperator, self).on_kill()
                self.log.error( "Failed to Execute: %s" % self.sql )
                if self.bq_cursor is not None:
                self.log.info('Canceling running query due to execution timeout')
                self.bq_cursor.cancel_query()


                You have to put in custom operators into the plugins directory.






                share|improve this answer























                  up vote
                  0
                  down vote



                  accepted







                  up vote
                  0
                  down vote



                  accepted






                  You could make your own Operator by just copying the BigQueryOperator and making the following changes to the execute and on_kill functions inside it, or you could override the existing BigQueryOperator as well.



                  def execute(self, context):
                  if self.bq_cursor is None:
                  self.log.info( "Beginnging Execution." )
                  hook = BigQueryHook(
                  bigquery_conn_id=self.bigquery_conn_id,
                  use_legacy_sql=self.use_legacy_sql,
                  delegate_to=self.delegate_to)
                  conn = hook.get_conn()
                  self.bq_cursor = conn.cursor()
                  self.bq_cursor.run_query(
                  self.sql,
                  destination_dataset_table=self.destination_dataset_table,
                  write_disposition=self.write_disposition,
                  allow_large_results=self.allow_large_results,
                  flatten_results=self.flatten_results,
                  udf_config=self.udf_config,
                  maximum_billing_tier=self.maximum_billing_tier,
                  maximum_bytes_billed=self.maximum_bytes_billed,
                  create_disposition=self.create_disposition,
                  query_params=self.query_params,
                  labels=self.labels,
                  schema_update_options=self.schema_update_options,
                  priority=self.priority,
                  time_partitioning=self.time_partitioning
                  )
                  self.log.info( "Executed: %s" % self.sql )

                  def on_kill(self):
                  super(BigQueryOperator, self).on_kill()
                  self.log.error( "Failed to Execute: %s" % self.sql )
                  if self.bq_cursor is not None:
                  self.log.info('Canceling running query due to execution timeout')
                  self.bq_cursor.cancel_query()


                  You have to put in custom operators into the plugins directory.






                  share|improve this answer












                  You could make your own Operator by just copying the BigQueryOperator and making the following changes to the execute and on_kill functions inside it, or you could override the existing BigQueryOperator as well.



                  def execute(self, context):
                  if self.bq_cursor is None:
                  self.log.info( "Beginnging Execution." )
                  hook = BigQueryHook(
                  bigquery_conn_id=self.bigquery_conn_id,
                  use_legacy_sql=self.use_legacy_sql,
                  delegate_to=self.delegate_to)
                  conn = hook.get_conn()
                  self.bq_cursor = conn.cursor()
                  self.bq_cursor.run_query(
                  self.sql,
                  destination_dataset_table=self.destination_dataset_table,
                  write_disposition=self.write_disposition,
                  allow_large_results=self.allow_large_results,
                  flatten_results=self.flatten_results,
                  udf_config=self.udf_config,
                  maximum_billing_tier=self.maximum_billing_tier,
                  maximum_bytes_billed=self.maximum_bytes_billed,
                  create_disposition=self.create_disposition,
                  query_params=self.query_params,
                  labels=self.labels,
                  schema_update_options=self.schema_update_options,
                  priority=self.priority,
                  time_partitioning=self.time_partitioning
                  )
                  self.log.info( "Executed: %s" % self.sql )

                  def on_kill(self):
                  super(BigQueryOperator, self).on_kill()
                  self.log.error( "Failed to Execute: %s" % self.sql )
                  if self.bq_cursor is not None:
                  self.log.info('Canceling running query due to execution timeout')
                  self.bq_cursor.cancel_query()


                  You have to put in custom operators into the plugins directory.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 21 at 8:27









                  Meghdeep Ray

                  2,50321738




                  2,50321738
























                      up vote
                      1
                      down vote













                      Looks like the code logs the query prior to execution, so the outcome is not known at the time the log is written.






                      share|improve this answer

























                        up vote
                        1
                        down vote













                        Looks like the code logs the query prior to execution, so the outcome is not known at the time the log is written.






                        share|improve this answer























                          up vote
                          1
                          down vote










                          up vote
                          1
                          down vote









                          Looks like the code logs the query prior to execution, so the outcome is not known at the time the log is written.






                          share|improve this answer












                          Looks like the code logs the query prior to execution, so the outcome is not known at the time the log is written.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 20 at 14:32









                          joeb

                          2,08611519




                          2,08611519






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53390834%2fapache-airflow-print-query-success-on-logging-info-query-failure-on-logging-er%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              404 Error Contact Form 7 ajax form submitting

                              How to know if a Active Directory user can login interactively

                              Refactoring coordinates for Minecraft Pi buildings written in Python