1

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?

David דודו Markovitz
  • 42,900
  • 6
  • 64
  • 88

1 Answers1

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