最近在将数据从mysql 等其他关系型数据库 抽取到hive 表中时,需要同步mysql表中的注释,以下脚本可以生成hive表字段注释修改语句。主要给大家介绍了关于mysql元数据如何生成hive建表语句注释脚本的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面跟着小编来一起学习学习吧。
注:其他关系型数据库如:oracle 可以通过相同的思路,读取元数据,修改脚本语法实现。
使用:
在mysql元数据库:information_schema 中执行以下语句
select concat('alter table ', table_name, ' change column ', column_name, ' ', column_name, ' ', data_type, ' comment ', '"', column_comment, '"', ';')
from (select table_name, column_name, case when data_type = 'varchar' then 'string' when data_type = 'int' then 'int' when data_type = 'tinyint' then 'tinyint' when data_type = 'decimal' then 'double' when data_type = 'datetime' then 'string' when data_type = 'timestamp' then 'string' when data_type = 'float' then 'double' when data_type = 'double' then 'double' when data_type = 'bigint' then 'bigint' end as data_type, column_comment
from columns
where table_name = 'o_oms_statistic_profit'
) t;
在将数据从mysql 等其他关系型数据库 抽取到hive 表中时,需要同步mysql表中的注释,以下脚本可以生成hive创建表语句。只是生成了hive表主要的字段信息,其他信息需要手工添加。
在mysql元数据库:information_schema 中执行以下语句
select concat('create table ', table_name, '(', substring(column_info, 1, length(column_info) - 1), ')', ' comment ', '"', table_comment, '"', ';')
from (select table_name, table_comment, group_concat(concat(column_name, ' ', data_type, ' comment ', '"', column_comment, '"')) as column_info
from (select t1.table_name, case when t2.table_comment = null then t1.table_name else t2.table_comment end as table_comment, column_name, case when data_type = 'varchar' then 'string' when data_type = 'int' then 'int' when data_type = 'tinyint' then 'tinyint' when data_type = 'decimal' then 'double' when data_type = 'datetime' then 'string' when data_type = 'timestamp' then 'string' when data_type = 'float' then 'double' when data_type = 'double' then 'double' when data_type = 'bigint' then 'bigint' end as data_type, case when column_comment = null then column_name else column_comment end as column_comment
from columns t1 join tables t2 on t1.table_name = t2.table_name
where t1.table_name = 'o_oms_statistic_profit'
) t3
group by table_name, table_comment
) t4;
相关推荐:
什么叫mysql元数据?元数据的介绍及实例代码
基于mysql元数据的hive的安装和简单测试
[mysql] 获取元数据的步骤
以上就是关于mysql元数据如何生成hive建表语句注释脚本的详细内容。
