How can I get the hive column count names using HQL? I know we can use the describe.tablename to get the names of columns. How do we get the count?
Asked
Active
Viewed 1,338 times
1
-
Possible duplicate of [Hive, how do I retrieve all the database's tables columns](http://stackoverflow.com/questions/29239565/hive-how-do-i-retrieve-all-the-databases-tables-columns) – Gordon Linoff Mar 04 '17 at 19:56
-
@GordonLinoff - definitly a different question – David דודו Markovitz Mar 04 '17 at 20:08
1 Answers
0
create table mytable(i int,str string,dt date, ai array<int>,strct struct<k:int,j:int>);
select count(*)
from (select transform ('')
using 'hive -e "desc mytable"'
as col_name,data_type,comment
) t
;
5
Some additional playing around:
create table mytable (id int,first_name string,last_name string);
insert into mytable values (1,'Dudu',null);
select size(array(*)) from mytable limit 1;
This is not bulletproof since not all combinations of columns types can be combined into an array.
It also requires that the table will contain at least 1 row.
Here is a more complex but also stronger solution (types versa), but also requires that the table will contain at least 1 row
select size(str_to_map(val)) from (select transform (struct(*)) using 'sed -r "s/.(.*)./\1/' as val from mytable) t;

David דודו Markovitz
- 42,900
- 6
- 64
- 88
-
I'll edit it later to support partitions. I also might be able to overcome the 1 row limitation with the STRUCT solution. – David דודו Markovitz Mar 05 '17 at 05:25