Just get column names from hive table

SqlHadoopHive

Sql Problem Overview


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

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

Is it also possible to just get 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.

Sql Solutions


Solution 1 - Sql

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:

use database;
describe tablename;

Solution 2 - Sql

Solution 3 - Sql

use desc tablename from 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.

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

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

$cd ~
$ls

Solution 4 - Sql

The solution is

show columns in table_name;

This is simpler than use

describe tablename;

Thanks a lot.

Solution 5 - Sql

Best way to do this is setting the below property:

set hive.cli.print.header=true;
set hive.resultset.use.unique.column.names=false;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncantdutchthisView Question on Stackoverflow
Solution 1 - SqlJJFord3View Answer on Stackoverflow
Solution 2 - SqlAngelo Di DonatoView Answer on Stackoverflow
Solution 3 - SqlAlex Raj KaliamoorthyView Answer on Stackoverflow
Solution 4 - SqlLuis Fernando Ramos CastilloView Answer on Stackoverflow
Solution 5 - SqlMahesh KumarView Answer on Stackoverflow