使用气流配置单元运算符并输出到文本文件 [英] Use airflow hive operator and output to a text file
问题描述
我想使用气流配置单元运算符执行配置单元查询并将结果输出到文件中。我不想在这里使用INSERT OVERWRITE。
Hi I want to execute hive query using airflow hive operator and output the result to a file. I don't want to use INSERT OVERWRITE here.
hive_ex = HiveOperator(
task_id='hive-ex',
hql='/sql/hive-ex.sql',
hiveconfs={
'DAY': '{{ ds }}',
'YESTERDAY': '{{ yesterday_ds }}',
'OUTPUT': '{{ file_path }}'+'csv',
},
dag=dag
)
最好的方法是什么?
我知道如何使用bash运算符执行此操作,但想知道我们是否可以使用hive运算符
I know how to do this using bash operator,but want to know if we can use hive operator
hive_ex = BashOperator(
task_id='hive-ex',
bash_command='hive -f hive.sql -DAY={{ ds }} >> {{ file_path }}
/file_{{ds}}.json',
dag=dag
)
推荐答案
因为这是一个非常自定义的用例,所以最好的方法是扩展Hive运算符(或创建自己的Hive2CSVOperator)。具体实施取决于您是否可以通过CLI或HiveServer2访问hive。
Since it is a pretty custom use-case the best way is to extend the Hive operator (or create your own Hive2CSVOperator). The implementation would depend on whether you have access to hive through CLI or HiveServer2.
Hive CLI
我首先尝试配置Hive CLI连接,然后根据
I would try first with configuring the Hive CLI connection and adding the hive_cli_params
, as per Hive CLI hook code, and if this doesn't work, extend the Hook (which would give you access to everything).
HiveServer2
这种情况有一个单独的钩子(链接)。因为它具有 get_results
方法(源)或 to_csv
方法(源)。
There is a separate hook for this case (link). It is a bit more convenient because it has a get_results
method (source) or to_csv
method (source).
操作符代码中的 execute
看起来类似于:
The execute
in the operator code could look then similar to this:
def execute():
...
self.hook = HiveServer2Hook(...)
self.conn = self.hook.get_conn()
self.conn.to_csv(hql=self.hql, csv_filepath=self.output_filepath, ...)
这篇关于使用气流配置单元运算符并输出到文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!