SQL 只需从 hive 表中获取列名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26181454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 02:44:26  来源:igfitidea点击:

Just get column names from hive table

sqlhadoophive

提问by cantdutchthis

I know that you can get column names from a table via the following trick in hive:

我知道您可以通过 hive 中的以下技巧从表中获取列名:

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

Is it also possible to justget the column names from the table?

是否也可以从表中获取列名?

I dislike having to change a setting for something I only need once.

我不喜欢为我只需要一次的东西更改设置。

My current solution is the following:

我目前的解决方案如下:

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

This seems too verbose and against the DRY-principle.

这似乎过于冗长并且违反了 DRY 原则。

回答by JJFord3

If you simply want to see the column names this one line should provide it without changing any settings:

如果您只是想查看列名,这一行应该提供它而无需更改任何设置:

describe database.tablename;

However, if that doesn't work for your version of hive this code will provide it, but your default database will now be the database you are using:

但是,如果这不适用于您的 hive 版本,此代码将提供它,但您的默认数据库现在将是您正在使用的数据库:

use database;
describe tablename;

回答by Angelo Di Donato

you could also do show columns in $tableor see Hive, how do I retrieve all the database's tables columnsfor access to hive metadata

您还可以执行show columns in $table或查看Hive,如何检索所有数据库的表列以访问Hive元数据

回答by Alex Raj Kaliamoorthy

use desc tablenamefrom Hive CLI or beeline to get all the column names. If you want the column names in a file then run the below command from the shell.

使用desc tablenameHive CLI 或 beeline 获取所有列名称。如果您想要文件中的列名,请从 shell 运行以下命令。

$ hive -e 'desc dbname.tablename;' > ~/columnnames.txt

where dbnameis the name of the Hive database where your table is residing You can find the file columnnames.txt in your root directory.

这里dbname是你的表是居住,您可以找到您的根目录下的文件columnnames.txt蜂房数据库的名称。

$cd ~
$ls