Hive - 如何在命令行中显示 Hive 查询结果以及列名称

JKC*_*JKC 3 hadoop hive

我在 Hive 工作了一段时间。请注意,我根本不使用 Hue。我一直使用 Hive shell,现在我遇到了一个奇怪但有用的问题。

每当我们在 Hive shell 中执行查询时,我们都可以在屏幕上看到相关结果,但我们无法识别与数据对应的列名,除非我们执行“desc formatted table_name”或任何其他类似命令并向上/向下滚动屏幕将结果与表结构进行匹配。我们很可能一直这样做。

出于好奇,我想知道当我们执行诸如“select * from table_name”之类的基本查询时,是否有任何方法至少可以打印列名和数据?

Shu*_*Shu 7

打开 Hive 会话后设置此属性

hive> set hive.cli.print.header=true;
Run Code Online (Sandbox Code Playgroud)

这样它就会显示您的列名称。

例子:

hive> desc sales;
OK
col_name        data_type       comment
year                    string
month                   string
customer                string
stateid                 string
productid               string
qty                     string
billed                  string

hive> select * from sales;
OK
2011    1.2     A       2       1       2       8
2011    5.2     C       3       1       1       8
2011    2       B       1       2       1       2
2011    3       B       1       2       2       2
Run Code Online (Sandbox Code Playgroud)

一旦我设置了上述属性

hive> set hive.cli.print.header=true;
hive> select * from sales;
OK
sales.year      sales.month     sales.customer  sales.stateid   sales.productid sales.qty       sales.billed
2011    1.2     A       2       1       2       8
2011    5.2     C       3       1       1       8
2011    2       B       1       2       1       2
Run Code Online (Sandbox Code Playgroud)

如果你想摆脱表名,即销售。每个列名称之前设置以下属性

hive> set hive.resultset.use.unique.column.names=false;
hive> select * from sales;
OK
year    month   customer        stateid productid       qty     billed
2011    1.2     A       2       1       2       8
2011    5.2     C       3       1       1       8
2011    2       B       1       2       1       2
Run Code Online (Sandbox Code Playgroud)

(或者)

作为永久解决方案,您可以在hive-site.xml中找到此属性值并将其更改为true