在Hive中执行任何查询时,是否有办法在输出结果中获取列名?

79
在Hive中,当我们执行查询(例如:select * from employee)时,不会在输出结果中得到任何列名(例如在RDBMS SQL中获取的名称、年龄、工资),我们只能得到值。
有没有办法在执行任何查询时显示列名?
7个回答

153
如果我们想在HiveQl中查看表的列名,应将以下Hive配置属性设置为true。
hive> set hive.cli.print.header=true;
如果您希望始终查看列名称,请在$HOME/.hiverc文件的第一行中更新以上设置。
- Hive会自动查找名为.hiverc的文件,并在其中包含的命令(如果有)运行。

如果文件.hiverc不存在,您可以创建一个并按照上述描述添加行。 - Jignesh Rawal
1
echo "SET hive.cli.print.header=true;" >> ~/.hiverc - Rodrigo Hjort
1
如果你已经配置了安装环境,你也可以在hive-site.xml中将属性"hive.cli.print.header"设置为true的值。 - miniscem

16

要在输出中打印标题,必须在执行查询之前将以下Hive配置属性设置为true。

hive> set hive.cli.print.header=true;
hive> select * from table_name;

如果我们想要在文件中获取结果,我们也可以像这样使用查询。

hive -e 'set hive.cli.print.header=true;select * from table_name;' > result.xls

请填入您的表名称 table_name


5
所有上面的答案已经回答了这个问题。但是如果有人想要永久开启此属性,则可以在hive-default.xmlhive-site.xml中使用此属性:hive.cli.print.header
它的默认值为false。将其值设置为true并保存即可。
完成。

4
1)Permenant solution
change this property in hive-site.xml file under $HIVE_HOME/conf folder
    <property>
    <name>hive.cli.print.header</name>
    <value>true</value>
    <description>Whether to print the names of the columns in query output.
    </description>
    </property>
2)Temporary solution:
go to hive prompt execute this comman
   hive>set hive.cli.print.header=True

3
大多数解决方案都是准确的。
将属性hive.cli.print.header = true设置为真即可。
但如果您使用的是Cloudera、HDP或其他发行版,则这些值将被重置。因此,请在Hive配置中更新这些值并重新启动服务。
这将是一个永久性的修复。希望这可以帮助到您。

2
在执行查询之前设置此属性:
hive> set hive.cli.print.header=true;

0

使用 set hive.cli.print.header=true;

hive> set hive.cli.print.header=true;      
hive> select * from tblemployee;
OK
id      name    gender  salary  departmentid
1       tomr    male    40000   1
2       cats    female  30000   2
3       john    male    50000   1
4       james   male    35000   3
5       sara    female  29000   2
6       bens    male    35000   1
7       saman   female  30000   NULL
8       russel  male    40000   2
9       valar   female  30000   1
10      todd    male    95000   NULL
Time taken: 9.892 seconds

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接