简体   繁体   中英

Count Number of Columns In Hive

I am looking for a way to count the number of columns in a table in Hive.

I know the following code works in Microsoft SQL Server. Is there a Hive equivalent?

SELECT COUNT(*),
  FROM INFORMATION_SCHEMA.COLUMNS
 WHERE TABLE_CATALOG = 'database_name'
   AND TABLE_SCHEMA = 'schema_name'
   AND TABLE_NAME = 'table_name'

try this

SHOW COLUMNS (FROM|IN) table_name [(FROM|IN) db_name]

试试这个,它会显示你的表的列:

DESCRIBE schemaName.tableName;

I do not know of a way to count the columns directly, however, I solved the problem for my needs indirectly via:

echo 'table1name:, '`hive -e 'describe schemaname.table1name;' | grep -v 'col_name' | wc -l > num_columns.csv
echo 'table2name:, '`hive -e 'describe schemaname.table2name;' | grep -v 'col_name' | wc -l >> num_columns.csv
...

(I needed the grep -v bit because I have headers on by default; without it you get one too many lines counted in the wc -l step.)

您必须检查您的 HIVE 是否包含 HIVE-287,因为对于不包含 HIVE-287 的 HIVE 版本,您需要使用 COUNT(1) 代替 COUNT(*)。

只需做一个描述,它就会显示所有列,然后在底部然后您可以看到它获取的行数,即列数。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM