如何在Hive中的所有表中找到特定的列名。

时间:2018-01-18 12:46:32

标签: hadoop hive

如何在Hive中的所有表中找到特定的列名?

我在配置单元中运行了此查询:select table_name,column_name from retail.columns where column_name like '%emp%'; retail 是一个数据库)。

但它给予了:

  

错误FAILED:SemanticException第0行:-1未找到表'列'

我尝试过查询:select distinct table_name from default.columns where column_name = 'emp'默认是我的数据库)。但它也给出了错误。

我搜索了这些,我得到了我为SQL数据库编写的查询。

但我想在蜂巢数据库中搜索?如何进入蜂巢?

以前曾问过同样的问题,但我觉得事情可能已经改变,可能有直接的解决方案:

How can you search for all tables with a given column name and return which tables have this column name in Hadoop/Hive?

Searching Tables and Columns in Hive

4 个答案:

答案 0 :(得分:2)

下面的shell脚本会给你想要的结果:

hive -S -e 'show databases'|
while read database
do
   eval "hive -S -e 'show tables in $database'"|
   while read line
   do
if eval "hive -S -e 'describe $database.$line'"| grep -q "<column_name"; then
  output="Required table name: $database.$line"'\n';
else
output=""'\n';

fi
echo -e "$output"
 done
done

答案 1 :(得分:0)

如果您不了解列的一点名称,我相信此查询将为您提供帮助:

select table_name,column_name from information_schema.columns
where column_name like '%lead%'

答案 2 :(得分:0)

我在下面的简化的shell脚本中编写了此脚本以得到结果:

步骤1:在下面的脚本中替换并运行:

   while read line
   do
    if eval "hive -S -e 'describe <DB_NAME>.$line'"| grep -q "<COLUMN_NAME>"; then
        output="${output}  <DB_NAME>.$line"'\n';
    fi
   done < <(eval "hive -S -e 'show tables in <DB_NAME>'")

第2步:运行以下命令

echo -e "Required table name:\n\n $output"

注意:记住,如果您多次运行,请清除变量输出。

output=""

答案 3 :(得分:-1)

以下是您可以在Metastore上使用的查询:

从TBLS选择TBL_NAME,COLUMN_NAME,TYPE_NAME,在CD_ID = TBL_ID上加入COLUMNS_V2,其中COLUMN_NAME喜欢'列';

其中'column'是您要查找的列名。