IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 大数据 -> 使用scala语言编写代码,一键把hive中的DDLsql转化成MySql中的DDLsql -> 正文阅读

[大数据]使用scala语言编写代码,一键把hive中的DDLsql转化成MySql中的DDLsql

使用scala语言编写代码,一键把hive中的DDLsql转化成MySql中的DDLsql

编写hiveDDLsql转化成MySqlDDLsql的代码

注意
1、本代码只是转化了大概,如果在字段的数据类型上不一致的话,可以用VSCode打开生成的文件,替换掉你想替换的字段数据类型
2、在VSCode上也可以使用正则匹配进行替换,如下图,非常好用

请添加图片描述

3、这个代码会减少很多重复的工作量,一下转化几百张表,甚至几千张表,一起来提高效率,拒绝当工具人

下面的代码就是hiveDDLsql转化成MySqlDDLsql的代码

import java.io.FileWriter

import scala.collection.mutable.ListBuffer
import scala.io.Source

object GenSql {
  def main(args: Array[String]): Unit = {
    /**
      * 在原本的建表语句上增加预留字段
      * 去掉分区字段
      * 在表名上增加标识
      *
      */

    val newDDlSql = new ListBuffer[String]


    val yuField: String =
      """
        |,f1 string comment '预留字段',
        |f2 string comment '预留字段'
        |
        """.stripMargin


    //1、读取sql文件
    val sqlStr: String = Source.fromFile("data/hive_ddl_sql.sql").getLines().toList.mkString("\n")

    //2、按照分号拆分
    val list: List[String] = sqlStr.split(";").toList
    for (ddlSql: String <- list) {

      //查找括号开始和接收位置
      val fieldStart: Int = ddlSql.indexOf("(") + 1
      val fieldEnd: Int = ddlSql.indexOf(")")

      //截取列
      val fields: String = ddlSql.substring(fieldStart, fieldEnd)

      //增加预留字段
      val mergeField: String = fields + yuField


      //取出表除了分区后面所有的信息

      val rowStaart: Int = ddlSql.indexOf("ROW")
      val info: String = ddlSql.substring(rowStaart)


      //取出表名
      val tableName: String = ddlSql.substring(ddlSql.indexOf(".") + 1, ddlSql.indexOf("("))
      println(tableName)

      //拼接完整sql


      val newDDL: String =
        s"""
           |CREATE external TABLE IF NOT EXISTS  ods.${tableName.trim}_f (
           |${mergeField.trim}
           ) ${info.trim}
           |
        """.stripMargin


      newDDlSql.append(newDDL)
    }


    //将sql保存
    val writer = new FileWriter("data/mysql_ddl_sql.sql")
    writer.write(newDDlSql.mkString(";\n"))
    writer.close()
  }
}

hive中的DDLsql

hive_ddl_sql.sql

CREATE external TABLE IF NOT EXISTS  ods.ods_t_fcj_nwrs_sellbargain(
    id        STRING comment '身份证号码',
    r_fwzl    STRING comment '房产地址',
    htydjzmj   STRING comment '合同中约定房子面积',
    tntjzmj   STRING comment '房子内建筑面积',
    ftmj      STRING comment '房子分摊建筑面积',
    time_tjba STRING comment '商品房备案时间',
    htzj       STRING comment '合同总价'
)PARTITIONED BY
(
    ds   STRING
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
STORED AS textfile
location '/daas/motl/ods/ods_t_fcj_nwrs_sellbargain/';


CREATE external  TABLE IF NOT EXISTS ods.ods_t_ga_hjxx_czrkjbxx_delta(
GMSFHM         string comment'公民身份号码',
QFJG           string comment'签发机关',
YXQXQSRQ       string comment'有效期限起始日期',
YXQXJZRQ       string comment'有效期限截止日期',
XM             string comment'姓名',
CYM            string comment'曾用名',
XMPY           string comment'姓名拼音',
CYMPY          string comment'曾用名拼音',
XB             string comment'性别',
MZ             string comment'民族',
CSRQ           string comment'出生日期',
CSSJ           string comment'出生时间',
CSDGJDQ        string comment'出生地国家(地区)',
CSDSSXQ        string comment'出生地省市县(区)',
CSDXZ          string comment'出生地详址',
DHHM           string comment'电话号码',
JHRYXM         string comment'监护人一姓名',
JHRYGMSFHM     string comment'监护人一公民身份号码',
JHRYJHGX       string comment'监护人一监护关系',
JHREXM         string comment'监护人二姓名',
JHREGMSFHM     string comment'监护人二公民身份号码',
JHREJHGX       string comment'监护人二监护关系',
FQXM           string comment'父亲姓名',
FQGMSFHM       string comment'父亲公民身份号码',
MQXM           string comment'母亲姓名',
MQGMSFHM       string comment'母亲公民身份号码',
POXM           string comment'配偶姓名',
POGMSFHM       string comment'配偶公民身份号码',
JGGJDQ         string comment'籍贯国家(地区)',
JGSSXQ         string comment'籍贯省市县(区)',
ZJXY           string comment'宗教信仰',
WHCD           string comment'文化程度',
HYZK           string comment'婚姻状况',
BYZK           string comment'兵役状况',
SG             string comment'身高',
XX             string comment'血型',
ZY             string comment'职业',
ZYLB           string comment'职业类别',
FWCS           string comment'服务处所',
XXJB           string comment'信息级别',
HSQL           string comment'何时迁来',
HYQL           string comment'何因迁来',
HGJDQQL        string comment'何国家(地区)迁来',
HSSXQQL        string comment'何省市县(区)迁来',
HXZQL          string comment'何详址迁来',
HSLBZ          string comment'何时来本址',
HYLBZ          string comment'何因来本址',
HGJDQLBZ       string comment'何国家(地区)来本址',
HSSSQLBZ       string comment'何省市县(区)来本址',
HXZLBZ         string comment'何详址来本址',
SWRQ           string comment'死亡日期',
SWZXLB         string comment'死亡注销类别',
SWZXRQ         string comment'死亡注销日期',
QCRQ           string comment'迁出日期',
QCZXLB         string comment'迁出注销类别',
QWDGJDQ        string comment'迁往地国家(地区)',
QWDSSXQ        string comment'迁往地省市县(区)',
QWDXZ          string comment'迁往地详址',
CSZMBH         string comment'出生证明编号',
CSZQFRQ        string comment'出生证签发日期',
HYLB           string comment'行业类别',
QTSSXQ         string comment'其他省市县(区)',
QTZZ           string comment'其他住址',
RYLB           string comment'人员类别',
HB             string comment'户别',
YHZGX          string comment'与户主关系',
RYZT           string comment'人员状态',
RYSDZT         string comment'人员锁定状态',
LXDBID         string comment'离线DBID',
BZ             string comment'备注',
JLBZ           string comment'记录标志',
YWNR           string comment'业务内容',
CJHJYWID       string comment'创建户籍业务ID',
CCHJYWID       string comment'撤除户籍业务ID',
QYSJ           string comment'起用时间',
JSSJ           string comment'结束时间',
CXBZ           string comment'冲销标志',
JLX            string comment'街路巷',
MLPH           string comment'门(楼)牌号',
MLXZ           string comment'门(楼)详址',
PCS            string comment'派出所',
ZRQ            string comment'责任区',
XZJD           string comment'乡镇(街道)',
JCWH           string comment'居(村)委会',
PXH            string comment'排序号',
MLPID          string comment'门(楼)牌ID',
SSXQ           string comment'省市县(区)',
HH             string comment'户号',
HLX            string comment'户类型',
HHID           string comment'户号ID',
BDFW           string comment'变动范围',
XXQYSJ         string comment'信息启用时间',
DHHM2          string comment'电话号码2',
GXSJ           string comment'更新时间戳',
ZXSJ           string comment'死亡,注销的注销时间',
CRYNBID        string comment'人员nbidchar',
CGMSFHM        string comment'身份号码2',
GXSJD          string comment'日期格式更新时间',
ZJLB           string comment'证件类别',
last_mod       string comment'数据更新时间'
)PARTITIONED BY
(
    ds   STRING
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
STORED AS textfile
location '/daas/motl/ods/ods_t_ga_hjxx_czrkjbxx_delta/';


CREATE external TABLE IF NOT EXISTS  ods.ods_t_gjj_sspersons(
spcode string comment '职工内码',
hjstatus string comment '缴汇状态',
sncode string comment '单位代码',
spname string comment '个人姓名',
id string comment '身份证',
spcard string comment '卡号',
sppassword string comment '口令',
zjfdm string comment '助记符代码',
spkhrq string comment '开户日期',
spperm string comment '起缴年月',
spgz decimal comment '工资基数',
spsingl decimal  comment '职工缴交率',
spjcbl decimal  comment '单位缴交率',
spmfact decimal  comment '月缴额',
spmfactzg decimal  comment '职工月缴额',
spjym string comment '最后缴年月',
ncye  decimal comment '年初余额',
splast  decimal comment '定期余额',
dwbfye  decimal comment '余额单位部分',
grbfye  decimal comment '余额职工部分',
spmend  decimal comment '余额',
splastlx  decimal comment '年初利息',
spout  decimal comment '年内总支出',
spin  decimal comment '年内总收入',
bnlx decimal  comment '活期利息积数',
nclx decimal  comment '定期利息积数',
dwhjny string comment '单位汇缴年月',
zghjny string comment '职工汇缴年月',
btyje  decimal comment '补贴月缴额',
btye  decimal comment '余额补贴部分',
btbl decimal  comment '补贴计缴比例',
bthjny string comment '补贴汇缴年月',
spxh string comment '销户标志',
spzy string comment '转移标志',
spxhrq string comment '销户日期',
splr decimal  comment '录入员',
spoldbankno string comment '原帐号',
spdk string comment '是否贷款',
spdy string comment '是否抵押',
zhdj string comment '帐户冻结',
spnote string comment '备注',
modifytime string comment '最后修改时间',
status string comment '联名卡状态',
cbank string comment '联名卡发卡银行',
bcyje  decimal comment '补充月缴额',
bcye  decimal comment '余额补充',
bcbl decimal  comment '补充计缴比例',
bchjny string comment ' 补充最后汇款年月',
zjzl decimal  comment '缴存资金种类'
)PARTITIONED BY
(
    ds   STRING
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
STORED AS textfile
location '/daas/motl/ods/ods_t_gjj_sspersons/';


CREATE external TABLE IF NOT EXISTS  ods.ods_t_gsj_reg_investor(
    id STRING comment '身份证号码'
)PARTITIONED BY
(
    ds   STRING
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
STORED AS textfile
location '/daas/motl/ods/ods_t_gsj_reg_investor/';


CREATE external TABLE IF NOT EXISTS  ods.ods_t_gsj_reg_legrepre(
id string comment '证件编号',
position string comment '职务',
tel string comment '联系电话',
appounit string comment '任免机构',
accdside string comment '派出单位',
posbrmode string comment '产生方式',
offhfrom string comment '任职期限起',
offhto string comment '任职期限止',
stufftype string comment '资料类型代码'
)PARTITIONED BY
(
    ds   STRING
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
STORED AS textfile
location '/daas/motl/ods/ods_t_gsj_reg_legrepre/';


CREATE external TABLE IF NOT EXISTS  ods.ods_t_rs_zhcs_az01bsbkxx(
id string comment '身份证号',
citty_id string comment '城市代码',
ss_id string comment '社保卡号',
fkrq string comment '发卡日期',
yxqz string comment '有效期至',
aaz502 string comment '卡状态',
aae008 string comment '开户银行',
aae008b string comment '银行网点',
aae010 string comment '金融卡号',
aae010a string comment '金融帐户',
aae010b string comment '个人帐户'
)PARTITIONED BY
(
    ds   STRING
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
STORED AS textfile
location '/daas/motl/ods/ods_t_rs_zhcs_az01bsbkxx/';

MySql中的DDLsql

mysql_ddl_sql.sql


CREATE external TABLE IF NOT EXISTS ods.ods_t_fcj_nwrs_sellbargain (
id        STRING comment '身份证号码',
    r_fwzl    STRING comment '房产地址',
    htydjzmj   STRING comment '合同中约定房子面积',
    tntjzmj   STRING comment '房子内建筑面积',
    ftmj      STRING comment '房子分摊建筑面积',
    time_tjba STRING comment '商品房备案时间',
    htzj       STRING comment '合同总价'

,f1 String comment "预留字段1",
f2 String comment "预留字段2"
)ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
STORED AS textfile
location '/daas/motl/ods/ods_t_fcj_nwrs_sellbargain/'
;

CREATE external TABLE IF NOT EXISTS ods.ods_t_ga_hjxx_czrkjbxx_delta (
GMSFHM         string comment'公民身份号码',
QFJG           string comment'签发机关',
YXQXQSRQ       string comment'有效期限起始日期',
YXQXJZRQ       string comment'有效期限截止日期',
XM             string comment'姓名',
CYM            string comment'曾用名',
XMPY           string comment'姓名拼音',
CYMPY          string comment'曾用名拼音',
XB             string comment'性别',
MZ             string comment'民族',
CSRQ           string comment'出生日期',
CSSJ           string comment'出生时间',
CSDGJDQ        string comment'出生地国家(地区)',
CSDSSXQ        string comment'出生地省市县(区)',
CSDXZ          string comment'出生地详址',
DHHM           string comment'电话号码',
JHRYXM         string comment'监护人一姓名',
JHRYGMSFHM     string comment'监护人一公民身份号码',
JHRYJHGX       string comment'监护人一监护关系',
JHREXM         string comment'监护人二姓名',
JHREGMSFHM     string comment'监护人二公民身份号码',
JHREJHGX       string comment'监护人二监护关系',
FQXM           string comment'父亲姓名',
FQGMSFHM       string comment'父亲公民身份号码',
MQXM           string comment'母亲姓名',
MQGMSFHM       string comment'母亲公民身份号码',
POXM           string comment'配偶姓名',
POGMSFHM       string comment'配偶公民身份号码',
JGGJDQ         string comment'籍贯国家(地区)',
JGSSXQ         string comment'籍贯省市县(区)',
ZJXY           string comment'宗教信仰',
WHCD           string comment'文化程度',
HYZK           string comment'婚姻状况',
BYZK           string comment'兵役状况',
SG             string comment'身高',
XX             string comment'血型',
ZY             string comment'职业',
ZYLB           string comment'职业类别',
FWCS           string comment'服务处所',
XXJB           string comment'信息级别',
HSQL           string comment'何时迁来',
HYQL           string comment'何因迁来',
HGJDQQL        string comment'何国家(地区)迁来',
HSSXQQL        string comment'何省市县(区)迁来',
HXZQL          string comment'何详址迁来',
HSLBZ          string comment'何时来本址',
HYLBZ          string comment'何因来本址',
HGJDQLBZ       string comment'何国家(地区)来本址',
HSSSQLBZ       string comment'何省市县(区)来本址',
HXZLBZ         string comment'何详址来本址',
SWRQ           string comment'死亡日期',
SWZXLB         string comment'死亡注销类别',
SWZXRQ         string comment'死亡注销日期',
QCRQ           string comment'迁出日期',
QCZXLB         string comment'迁出注销类别',
QWDGJDQ        string comment'迁往地国家(地区)',
QWDSSXQ        string comment'迁往地省市县(区)',
QWDXZ          string comment'迁往地详址',
CSZMBH         string comment'出生证明编号',
CSZQFRQ        string comment'出生证签发日期',
HYLB           string comment'行业类别',
QTSSXQ         string comment'其他省市县(区)',
QTZZ           string comment'其他住址',
RYLB           string comment'人员类别',
HB             string comment'户别',
YHZGX          string comment'与户主关系',
RYZT           string comment'人员状态',
RYSDZT         string comment'人员锁定状态',
LXDBID         string comment'离线DBID',
BZ             string comment'备注',
JLBZ           string comment'记录标志',
YWNR           string comment'业务内容',
CJHJYWID       string comment'创建户籍业务ID',
CCHJYWID       string comment'撤除户籍业务ID',
QYSJ           string comment'起用时间',
JSSJ           string comment'结束时间',
CXBZ           string comment'冲销标志',
JLX            string comment'街路巷',
MLPH           string comment'门(楼)牌号',
MLXZ           string comment'门(楼)详址',
PCS            string comment'派出所',
ZRQ            string comment'责任区',
XZJD           string comment'乡镇(街道)',
JCWH           string comment'居(村)委会',
PXH            string comment'排序号',
MLPID          string comment'门(楼)牌ID',
SSXQ           string comment'省市县(区)',
HH             string comment'户号',
HLX            string comment'户类型',
HHID           string comment'户号ID',
BDFW           string comment'变动范围',
XXQYSJ         string comment'信息启用时间',
DHHM2          string comment'电话号码2',
GXSJ           string comment'更新时间戳',
ZXSJ           string comment'死亡,注销的注销时间',
CRYNBID        string comment'人员nbidchar',
CGMSFHM        string comment'身份号码2',
GXSJD          string comment'日期格式更新时间',
ZJLB           string comment'证件类别',
last_mod       string comment'数据更新时间'

,f1 String comment "预留字段1",
f2 String comment "预留字段2"
)ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
STORED AS textfile
location '/daas/motl/ods/ods_t_ga_hjxx_czrkjbxx_delta/'
;

CREATE external TABLE IF NOT EXISTS ods.ods_t_gjj_sspersons (
spcode string comment '职工内码',
hjstatus string comment '缴汇状态',
sncode string comment '单位代码',
spname string comment '个人姓名',
id string comment '身份证',
spcard string comment '卡号',
sppassword string comment '口令',
zjfdm string comment '助记符代码',
spkhrq string comment '开户日期',
spperm string comment '起缴年月',
spgz decimal comment '工资基数',
spsingl decimal  comment '职工缴交率',
spjcbl decimal  comment '单位缴交率',
spmfact decimal  comment '月缴额',
spmfactzg decimal  comment '职工月缴额',
spjym string comment '最后缴年月',
ncye  decimal comment '年初余额',
splast  decimal comment '定期余额',
dwbfye  decimal comment '余额单位部分',
grbfye  decimal comment '余额职工部分',
spmend  decimal comment '余额',
splastlx  decimal comment '年初利息',
spout  decimal comment '年内总支出',
spin  decimal comment '年内总收入',
bnlx decimal  comment '活期利息积数',
nclx decimal  comment '定期利息积数',
dwhjny string comment '单位汇缴年月',
zghjny string comment '职工汇缴年月',
btyje  decimal comment '补贴月缴额',
btye  decimal comment '余额补贴部分',
btbl decimal  comment '补贴计缴比例',
bthjny string comment '补贴汇缴年月',
spxh string comment '销户标志',
spzy string comment '转移标志',
spxhrq string comment '销户日期',
splr decimal  comment '录入员',
spoldbankno string comment '原帐号',
spdk string comment '是否贷款',
spdy string comment '是否抵押',
zhdj string comment '帐户冻结',
spnote string comment '备注',
modifytime string comment '最后修改时间',
status string comment '联名卡状态',
cbank string comment '联名卡发卡银行',
bcyje  decimal comment '补充月缴额',
bcye  decimal comment '余额补充',
bcbl decimal  comment '补充计缴比例',
bchjny string comment ' 补充最后汇款年月',
zjzl decimal  comment '缴存资金种类'

,f1 String comment "预留字段1",
f2 String comment "预留字段2"
)ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
STORED AS textfile
location '/daas/motl/ods/ods_t_gjj_sspersons/'
;

CREATE external TABLE IF NOT EXISTS ods.ods_t_gsj_reg_investor (
id STRING comment '身份证号码'

,f1 String comment "预留字段1",
f2 String comment "预留字段2"
)ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
STORED AS textfile
location '/daas/motl/ods/ods_t_gsj_reg_investor/'
;

CREATE external TABLE IF NOT EXISTS ods.ods_t_gsj_reg_legrepre (
id string comment '证件编号',
position string comment '职务',
tel string comment '联系电话',
appounit string comment '任免机构',
accdside string comment '派出单位',
posbrmode string comment '产生方式',
offhfrom string comment '任职期限起',
offhto string comment '任职期限止',
stufftype string comment '资料类型代码'

,f1 String comment "预留字段1",
f2 String comment "预留字段2"
)ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
STORED AS textfile
location '/daas/motl/ods/ods_t_gsj_reg_legrepre/'
;

CREATE external TABLE IF NOT EXISTS ods.ods_t_rs_zhcs_az01bsbkxx (
id string comment '身份证号',
citty_id string comment '城市代码',
ss_id string comment '社保卡号',
fkrq string comment '发卡日期',
yxqz string comment '有效期至',
aaz502 string comment '卡状态',
aae008 string comment '开户银行',
aae008b string comment '银行网点',
aae010 string comment '金融卡号',
aae010a string comment '金融帐户',
aae010b string comment '个人帐户'

,f1 String comment "预留字段1",
f2 String comment "预留字段2"
)ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
STORED AS textfile
location '/daas/motl/ods/ods_t_rs_zhcs_az01bsbkxx/'

  大数据 最新文章
实现Kafka至少消费一次
亚马逊云科技:还在苦于ETL?Zero ETL的时代
初探MapReduce
【SpringBoot框架篇】32.基于注解+redis实现
Elasticsearch:如何减少 Elasticsearch 集
Go redis操作
Redis面试题
专题五 Redis高并发场景
基于GBase8s和Calcite的多数据源查询
Redis——底层数据结构原理
上一篇文章      下一篇文章      查看所有文章
加:2022-07-04 22:59:50  更:2022-07-04 23:02:36 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/20 0:34:24-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码