hive 通过mysql 生成建表语句

自行修改字段



SELECT
    a.TABLE_NAME ,
    b.TABLE_COMMENT ,
    concat('DROP TABLE IF EXISTS ', 'ods_', a.TABLE_NAME, ';', char(13),
'CREATE EXTERNAL TABLE IF NOT EXISTS ', 'ods_', a.TABLE_NAME , ' (', char(13),
group_concat(
concat("`",a.COLUMN_NAME,"`"," ",
(case when a.data_type = 'bigint' then 'bigint'
when a.data_type = 'binary' then 'binary'
when a.data_type = 'char' then 'string'
when a.data_type = 'date' then 'string'
when a.data_type = 'datetime' then 'string'
when a.data_type = 'decimal' then concat('decimal', '(', a.NUMERIC_PRECISION, ',', a.NUMERIC_SCALE, ')')
when a.data_type = 'double' then 'double'
when a.data_type = 'enum' then 'string'
when a.data_type = 'float' then 'double'
when a.data_type = 'int' then 'int'
when a.data_type = 'json' then 'map<string,string>'
when a.data_type = 'longtext' then 'string'
when a.data_type = 'mediumtext' then 'string'
when a.data_type = 'smallint' then 'int'
when a.data_type = 'text' then 'string'
when a.data_type = 'time' then 'string'
when a.data_type = 'timestamp' then 'string'
when a.data_type = 'tinyint' then 'int'
when a.data_type = 'varbinary' then 'binary'
when a.data_type = 'varchar' then 'string'
else '未知类型'
end), " COMMENT '", COLUMN_COMMENT, "'" ), char(13)
order by a.TABLE_NAME, a.ORDINAL_POSITION) ,
") COMMENT '", b.TABLE_COMMENT , "'", char(13), "PARTITIONED BY (`dt` STRING)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
NULL DEFINED AS ''  LOCATION '/warehouse/edu/ods/xxxxxxxxxx/';", char(13)) AS ods_ddl
FROM
    (
    SELECT
        TABLE_SCHEMA,
        TABLE_NAME,
        COLUMN_NAME,
        ORDINAL_POSITION,
        DATA_TYPE,
        NUMERIC_PRECISION,
        NUMERIC_SCALE,
        COLUMN_COMMENT
    FROM
        information_schema.COLUMNS
    WHERE
        TABLE_SCHEMA = 'edu22'
       -- AND TABLE_NAME = 'base_category_info'
        # 该条件限制单张张表的转换,可以注释掉,注释后为整个库的转换


) AS a
LEFT JOIN
information_schema.TABLES AS b
ON
    a.TABLE_NAME = b.TABLE_NAME
    AND a.TABLE_SCHEMA = b.TABLE_SCHEMA
where
    b.TABLE_TYPE = 'BASE TABLE'
GROUP BY
    a.TABLE_NAME,
    b.TABLE_COMMENT


;





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值