我正在使用 MySqlHook 建立与 airflow_db 的连接,并且正在执行一些查询,但是我需要在某处查看查询结果(假设为日志) ,怎么看?
这是示例代码
t1 = MySqlOperator(
task_id='basic_mysql',
mysql_conn_id='airflow_db',
sql="select * from xcom",
dag=dag)
答案 0 :(得分:2)
当前,MySQL运算符(在编写本文时为airflow 1.10.1)不支持在XCom中返回任何内容,因此,目前针对您的解决方法是自己编写一个小运算符。您可以直接在DAG文件中执行此操作:
from airflow.operators.python_operator import PythonOperator
from airflow.operators.mysql_operator import MySqlOperator
from airflow.hooks.mysql_hook import MySqlHook
class ReturningMySqlOperator(MySqlOperator):
def execute(self, context):
self.log.info('Executing: %s', self.sql)
hook = MySqlHook(mysql_conn_id=self.mysql_conn_id,
schema=self.database)
return hook.get_records(
self.sql,
parameters=self.parameters)
t1 = ReturningMySqlOperator(
task_id='basic_mysql',
mysql_conn_id='airflow_db',
sql="select * from xcom",
dag=dag)
def get_records(**kwargs):
ti = kwargs['ti']
xcom = ti.xcom_pull(task_ids='basic_mysql')
string_to_print = 'Value in xcom is: {}'.format(xcom)
# Get data in your logs
logging.info(string_to_print)
t2 = PythonOperator(
task_id='records',
provide_context=True,
python_callable=get_records,
dag=dag)
t1 >> t2
答案 1 :(得分:1)
AFAIK,MySqlOperator
用于运行UPDATE
/ DELETE
等查询的目的;换句话说查询:
为了掌握实际结果,您必须利用MySqlHook
。这是一个入门的小代码段(Python 3.6+
)(未经测试,仅用于提示)
from typing import List, Optional, Any
from airflow.hooks.mysql_hook import MySqlHook
# instantiate a MySqlHook
mysql_hook: MySqlHook = MySqlHook(mysql_conn_id="airflow_db")
# get records (this method comes from airflow.hooks.db_api_hook.DbApiHook)
records: List[List[Optional[Any]]] = mysql_hook.get_records(sql="select * from xcom")
# print records
print(records)
# alternatively, you can write records to task's logger
# note that here 'operator' = reference to your Operator
# operator.log.info("\n".join(records))
print()
/ log.info()
的输出将显示在用户界面的任务日志中
答案 2 :(得分:0)
通常,在使用Airflow时,应编写查询,以使结果进入临时表(可能包括results_name_{{ds_nodash}}
)。然后,您可以使用MySqlTo
SomethingElse Operator
移动临时表的结果。然后放下桌子进行清理。
我看不出任何理由将结果记录在Airflow日志中足以满足DAG的工作。