61

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.

chanchal1987
  • 2,320
  • 7
  • 31
  • 64
cantdutchthis
  • 31,949
  • 17
  • 74
  • 114

5 Answers5

90

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;
JJFord3
  • 1,976
  • 1
  • 25
  • 40
71

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

Community
  • 1
  • 1
Angelo Di Donato
  • 1,093
  • 9
  • 11
  • 12
    This should be the accepted answer to the question "is it possible to **just** get the column names", as `describe` provides extra information. – Matthew Moisen Sep 20 '16 at 22:30
  • This answer is more appropriate. Because it doesn't return extra information. For example, if your table has partitions, DESCRIBE command will return some extra rows, including empty ones. – Pastor Sep 10 '19 at 17:45
  • This answer requires an edit since Hive 3.0! Now ``show columns in $table`` orders the column names, possibly braking other code when the order is important. – runr Dec 08 '20 at 12:41
1

The solution is

show columns in table_name;

This is simpler than use

describe tablename;

Thanks a lot.

Amira Bedhiafi
  • 8,088
  • 6
  • 24
  • 60
0

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
Alex Raj Kaliamoorthy
  • 2,035
  • 3
  • 29
  • 46
-2

Best way to do this is setting the below property:

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