hive创建与mysql表结构一致的表

hive创建与mysql表结构一致的表,第1张

hive创建与mysql表结构一致的表

代码出自导师之手,特此记录,方便下次使用。

SET SESSION group_concat_max_len = 10240;

select
    concat_ws
    ('n'
     ,concat('create table ods.ods_{库名}_','{表名}','_dt')
     ,'('
     ,group_concat(
        concat_ws
        (' '
        ,case when t.ORDINAL_POSITION = 1 then ' ' else ',' end
        ,t.column_name
        ,t.hive_data_type
        ,'COMMENT'
        ,concat('''',t.COLUMN_COMMENT,'''')
        )
        order by t.ORDINAL_POSITION asc
        separator 'n'
      )
     ,concat(') comment ''',m2.table_comment,'''')
     ,'partitioned by (statdate string COMMENT ''数据日期(yyyyMMdd)'')'
     ,'stored as parquet'
     ,'TBLPROPERTIES (''parquet.compression''=''SNAPPY'')'
     )
     as hive_sql
    ,group_concat(t.column_name order by t.ORDINAL_POSITION asc separator ',') as sqoop_column_list
from
(
select
     t.table_schema
    ,t.table_name
    ,t.column_name
    ,CASE WHEN t.DATA_TYPE IN ('int','integer','smallint','tinyint','bit')
          THEN case when t.ORDINAL_POSITION = 1 and t.COLUMN_NAME like '%id' then 'bigint' else 'int' end
          WHEN t.DATA_TYPE IN ('bigint')
          THEN 'bigint'
          WHEN t.DATA_TYPE IN ('double','decimal')
          THEN concat('decimal(',IFNULL(t.NUMERIC_PRECISION,10),',',IFNULL(t.NUMERIC_SCALE,0),')')
          WHEN t.DATA_TYPE IN ('varchar','char')
          THEN 'string'
          WHEN t.DATA_TYPE IN ('timestamp','date','datetime','year')
          THEN 'string'
          ELSE '??????'
      END as hive_data_type
    ,t.DATA_TYPE as mysql_data_type
    ,t.COLUMN_COMMENT
    ,t.ORDINAL_POSITION
from information_schema.COLUMNS t
where t.table_schema='{库名}'
  and t.table_name='{表名}'
) t
join (
    select table_schema,table_name,table_comment from information_schema.TABLES t2
    where table_schema='{库名}'
      and table_name='{表名}'
) m2
on m2.table_schema = t.table_schema
and m2.table_name = t.table_name

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5705669.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存