gpt4 book ai didi

Hadoop hive : Generate Table Name and Attribute Name using Bash script

转载 作者:可可西里 更新时间:2023-11-01 15:51:21 25 4
gpt4 key购买 nike

在我们的环境中,我们无法访问 Hive 元存储来直接查询。

我需要为一组表动态生成表名、列名对。

我试图通过对所有表的文件运行“describe extended $tablename”并从文件中选取表名和列名对来实现这一点。

除了这种方式,还有其他更简单的方法吗?

想要的输出是这样的

table1|col1
table1|col2
table1|col3
table2|col1
table2|col2
table3|col1

最佳答案

此脚本将为单个表以所需格式打印列。 AWK 从 describe 命令解析字符串,只接受 column_name,用“|”连接和 table_name 变量,每个字符串以\n 作为它们之间的分隔符打印。

#!/bin/bash

#Set table name here
TABLE_NAME=your_schema.your_table

TABLE_COLUMNS=$(hive -S -e "set hive.cli.print.header=false; describe ${TABLE_NAME};" | awk -v table_name="${TABLE_NAME}" -F " " 'f&&!NF{exit}{f=1}f{printf c table_name "|" toupper($1)}{c="\n"}')

例如,您可以使用 show tables 命令轻松修改它以生成所有表的输出。

更简单的方法是直接访问元数据数据库。

关于Hadoop hive : Generate Table Name and Attribute Name using Bash script,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49025395/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com