使用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
val sqlStr: String = Source.fromFile("data/hive_ddl_sql.sql").getLines().toList.mkString("\n")
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)
val newDDL: String =
s"""
|CREATE external TABLE IF NOT EXISTS ods.${tableName.trim}_f (
|${mergeField.trim}
) ${info.trim}
|
""".stripMargin
newDDlSql.append(newDDL)
}
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/'
|