如何使用Unix Shell脚本将Impala查询输出日志获取到变量中? [英] How to get the impala query output log into a variable using unix shell script?

查看:827
本文介绍了如何使用Unix Shell脚本将Impala查询输出日志获取到变量中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建unix shell脚本来执行impala查询.我需要获取impala查询的输出日志.例如,我尝试了以下方法.

I'm creating unix shell script to execute the impala query. I need to get the output log of impala query. For example I tried the below.

output_log = echo $(impala-shell -i $node -q "select name from impaladb.impalatbl" -o output_file)

输出:

+--------+
| name   |
+--------+
| tom    |
| mike   |
+--------+

Fetched 2 row(s) in 0.83s

在这里,我在output_file和output_log中都得到了两个名称的输出.但是我需要在output_log变量中登录在0.83s内提取2行".我怎么能得到它?

Here I'm getting the two name output in both output_file and output_log. But I need the "Fetched 2 row(s) in 0.83s" log in output_log variable. How can I get it?

推荐答案

我解决了这个问题. 它的工作方式是Impala在不同的流中发送查询输出,而其他信息在不同的流中发送查询.

I solved the problem. The way it works is that impala sends the query output in a different stream and the other information w.r.t the query in a different stream.

因此,您所要做的就是

impala-shell -i $node -q "select name from impaladb.impalatbl" 2>output_file

2> 将在输出文件中发送包含" 10秒内提取到1行"的输出.现在,您可以grep它或做任何您想做的事情.

the 2> will send the output containing the "Fetched 1 row in 10seconds" in the output file. Now you can grep it or do whatever you want .

假设您要将相同的输出存储在 output_log 变量中,然后使用

Suppose you want to store the same output in output_log variable , then use

 output_log=$(impala-shell -i $node -q "select name from impaladb.impalatbl" 2>&1)

此处 2>& 1 会将输出发送到stdout,它将用于将值与变量相关联.

here 2>&1 will send the output to stdout which will be used for assinging the value to variable.

有关此的更多信息,只需在Google搜索中放入 2>& 1 并了解更多信息.

For more information on this , just put 2>&1 in google search and learn more about it.

希望它对您有帮助!

一些其他观察结果

2>& 1将输出从stderr重定向到stdout,但stdout也会获取查询输出,因此,将其存储在变量中时,它将获取查询输出以及诸如在3秒内获取1行"之类的额外信息.

2>&1 redirect output from stderr to stdout but stdout also gets the query output, so when you store it in a variable , it will get query output as well as the extra information like"fetched 1 row in 3seconds"

但是

当我们使用

2> a.txt,则仅stderr输出被重定向.因此a.txt将仅包含诸如启动impala .....在2秒内获取1行"之类的信息.然后您可以从文件中grep并将其放入变量中.

2>a.txt then only the stderr output is getting redirected. so a.txt will contain only information like "starting impala.....fetched 1 row in 2 seconds" . and then you can grep from file and put that in a variable.

只是想强调我观察到的存储在文件和存储在变量之间的细微差别.

just wanted to highlight this slight difference I observed between storing in a file and storing in a variable.

这篇关于如何使用Unix Shell脚本将Impala查询输出日志获取到变量中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆