目录
准备Excel例如下:
如果是想生成Hive的建表语句则:
如果是想生成Mysql的建表语句则:
宏代码分享:
表结构-副本.xls(文章:《根据Excel记录生成Mysql和Hive建表语句》使用)-其它文档类资源-CSDN下载根据Excel记录生成Mysql和Hive建表语句更多下载资源、学习资料请访问CSDN下载频道.https://download.csdn.net/download/qq_30168227/75382077
D列的序号比较重要:
这一列都打上Y。
然后使用宏:
出现下图:
双击其中一个,生成结果如下:
打开文件如下:
将Hive的T列打的Y都给取消掉。
生成的文件如下:
Sub P01_Gen_DDL_mysql()
'lyg 2021-07-26
Application.ScreenUpdating = False
Dim FSO As Object
Dim TextFile As Object
Dim PrimaryKey As String '主键
Dim NotNull As String '分布键
Dim Orientation As String '分布键
Dim CompressMode As String '分布键
Dim DistributeKey As String '分布键
Dim ColumnList As String '字段列表
Dim CommentList As String '注释
'初始化变量值
PrimaryKey = ""
NotNull = ""
Orientation = ""
CompressMode = ""
DistributeKey = ""
ColumnList = ""
CommentList = ""
v_path = ActiveWorkbook.Path
v_filename = v_path + "\" + Left(ThisWorkbook.Name, Len(ThisWorkbook.Name) - 4) + "-mpp.sql"
Set FSO = CreateObject("Scripting.FileSystemObject")
Set TextFile = FSO.CreateTextFile(v_filename, True)
Set ws = ActiveWorkbook.ActiveSheet
For Each rw In ws.Cells(1, 2).CurrentRegion.Rows
If Len(ws.Cells(1, 1)) = 0 Then Exit For
n = rw.Row '行号
SchemaName = Cells(n, 1).Value
table_en = Cells(n, 2).Value '英文表名
table_cn = Cells(n, 3).Value '中文表名
field_en = Cells(n, 5).Value '字段名称英文
field_cn = Cells(n, 6).Value '字段名称中文
field_type = Cells(n, 7).Value '字段类型长度MPP
this_table = UCase(Trim(table_en)) '当前表名
next_table = UCase(Trim(Cells(n + 1, 2).Value)) '下一英文表名
If n > 1 Then
pre_table = UCase(Trim(Cells(n - 1, 2).Value)) '上一英文表名
Else
pre_table = ""
End If
If UCase(Trim(Cells(n, 11).Value)) = "Y" Then '设置主键, (行号,列号)
PrimaryKey = PrimaryKey + Cells(n, 5).Value + ","
End If
If UCase(Trim(Cells(n, 12).Value)) = "Y" Then '设置分布键
DistributeKey = DistributeKey + Cells(n, 5).Value + "," '字段名称英文
End If
If UCase(Trim(Cells(n, 13).Value)) = "NN" Then '非空标志
NotNull = " NOT NULL"
Else
NotNull = " NULL"
End If
If this_table <> pre_table Then
If UCase(Trim(Cells(n, 14).Value)) = "Y" Or IsEmpty(Trim(Cells(n, 14).Value)) Then '列式存储(默认column)
Orientation = "COLUMN"
Else
Orientation = "ROW" '指定N或其它值时为ROW
End If
End If
If this_table <> pre_table Then
If UCase(Cells(n, 15).Value) <> "" Then '压缩级别
CompressMode = Cells(n, 15).Value
Else
CompressMode = "LOW"
End If
End If
If UCase(Cells(n, 16).Value) = "Y" Then '复制表
DistributeKey = "REPLICATION"
End If
'生成字段列表, 例如: Wthd_Mod_Cd CHAR(3) NOT NULL, --支取方式代码
field_en_len = 32 '设置英文字段名的字符最大长度
field_type_len = 20 '设置字段类型的字符最大长度
If n > 1 Then '从第2行开始执行
ColumnList = ColumnList + " ," + field_en + Space(Abs(field_en_len - Len(field_en)) + 1) + field_type + Space(Abs(field_type_len - Len(field_type)) + 1) + Space(Abs(10 - Len(NotNull)) + 1) + "COMMENT '" + field_cn + "'" + vbLf
CommentString = "COMMENT ON COLUMN " + SchemaName + "." + table_en + "." + field_en + Space(Abs(field_en_len - Len(field_en)) + 1) + "IS '" + field_cn + "';"
If Len(field_cn) > 0 Then '如果有注释则执行,否则不执行COMMENT
CommentList = CommentList + CommentString + vbLf
Else
CommentList = CommentList + "--" + CommentString + vbLf
End If
End If
If n > 1 And this_table <> next_table Then '从第2行开始
TextFile.WriteLine ("-- --------------CREATE TABLE: " + table_cn + "----------------------------------")
DropSQL = "-- DROP TABLE IF EXISTS " + SchemaName + "." + table_en + " CASCADE;"
TextFile.WriteLine (DropSQL)
Create = "CREATE TABLE " + SchemaName + "." + table_en + vbLf + "("
TextFile.WriteLine (Create)
ColumnList = " " + Mid(ColumnList, 4, InStrRev(ColumnList, vbLf) - 4) '去除前面1个逗号和尾部换行符号
TextFile.WriteLine (ColumnList) '字段列表
' If Len(PrimaryKey) > 0 Then
' TextFile.WriteLine (" --,PRIMARY KEY (" + Mid(PrimaryKey, 1, Len(PrimaryKey) - 1) + ")") '设置主键,去除最后1个字符逗号
' End If
TextFile.WriteLine (")") '字段完成
TAB_COMMENT = "COMMENT ='" + table_cn + "';" '表注释
TextFile.WriteLine (TAB_COMMENT)
' TextFile.WriteLine ("WITH (ORIENTATION = " + Orientation + ", COMPRESSION = " + CompressMode + ")") '设置存储模式、压缩级别
' If DistributeKey = "" Then
'不设置分布键
' ElseIf DistributeKey = "REPLICATION" Then
' TextFile.WriteLine ("DISTRIBUTE BY REPLICATION") '设置分布键,去除最后1个字符逗号
' Else
' TextFile.WriteLine ("DISTRIBUTE BY HASH(" + Mid(DistributeKey, 1, Len(DistributeKey) - 1) + ")") '设置分布键,去除最后1个字符逗号
' End If
TextFile.WriteLine (";") '列出字段完成
TextFile.WriteLine ("")
' TAB_COMMENT = "COMMENT ON TABLE " + SchemaName + "." + table_en + " IS '" + table_cn + "';" '表注释
' TextFile.WriteLine (TAB_COMMENT)
' TextFile.WriteLine (CommentList) '设置字段注释,建表完成
'初始化变量值
PrimaryKey = ""
NotNull = ""
Orientation = ""
CompressMode = ""
DistributeKey = ""
ColumnList = ""
CommentList = ""
End If
Next
MsgBox "DDL成功生成在:" + vbLf + v_filename
End Sub
Sub P02_Gen_DDL_hive()
'wzm 2020-07-09
Application.ScreenUpdating = False
Dim FSO As Object
Dim TextFile As Object
Dim ColumnList As String
Dim PartitionKey As String
'初始化变量值
PrimaryKey = ""
NotNull = ""
Orientation = ""
CompressMode = ""
DistributeKey = ""
ColumnList = ""
PartitionKey = ""
v_path = ActiveWorkbook.Path
v_filename = v_path + "\" + Left(ThisWorkbook.Name, Len(ThisWorkbook.Name) - 4) + "-hive.sql"
Set FSO = CreateObject("Scripting.FileSystemObject")
Set TextFile = FSO.CreateTextFile(v_filename, True)
Set ws = ActiveWorkbook.ActiveSheet
For Each rw In ws.Cells(1, 2).CurrentRegion.Rows
If Len(ws.Cells(1, 1)) = 0 Then Exit For
n = rw.Row '行号
If UCase(Cells(n, 20).Value) = "Y" Then '判断是否生成Hive脚本
'生成字段列表, 例如: Wthd_Mod_Cd CHAR(3) NOT NULL, --支取方式代码
If n > 1 Then '从第2行开始
SchemaName = Cells(n, 1).Value
table_en = Cells(n, 2).Value '英文表名
table_cn = Cells(n, 3).Value '中文表名
field_en = Cells(n, 5).Value '字段名称英文
field_cn = Cells(n, 6).Value '字段名称中文
field_type = Cells(n, 7).Value '字段类型长度
field_type = Replace(UCase(field_type), "NVARCHAR2", "VARCHAR")
field_type = Replace(UCase(field_type), "VARCHAR2", "VARCHAR")
field_type = Replace(UCase(field_type), "CHARACTER", "CHAR")
field_type = Replace(UCase(field_type), "BYTEA", "STRING")
field_type = Replace(UCase(field_type), "TEXT", "STRING")
field_type = Replace(UCase(field_type), "CLOB", "STRING")
field_type = Replace(UCase(field_type), "NUMBER", "DOUBLE")
field_type = Replace(UCase(field_type), "NUMERIC", "DOUBLE")
field_type = Replace(UCase(field_type), "DECIMAL", "DOUBLE")
field_type = Replace(UCase(field_type), "TIMESTAMP(6)", "TIMESTAMP")
If Mid(UCase(field_type), 1, 6) = "DOUBLE" Then '将DECIMAL(x,x)替换为整型
field_type = "DOUBLE"
End If
If Mid(UCase(field_type), 1, 9) = "TIMESTAMP" Then 'TIMESTAMP(x)替换为整型
field_type = "TIMESTAMP"
End If
field_en_len = 32 '设置英文字段名的字符最大长度
field_type_len = 20 '设置字段类型的字符最大长度
ColumnList = ColumnList + " ," + field_en + Space(Abs(field_en_len - Len(field_en)) + 1) + field_type + Space(Abs(field_type_len - Len(field_type)) + 1) + "COMMENT " + """" + field_cn + """" + vbLf '设置字段及注释
If UCase(Cells(n, 21).Value) = "Y" Then '获取Hive分区键
PartitionKey = field_cn
End If
End If
this_table = UCase(Trim(table_en)) '当前表名
next_table = UCase(Trim(Cells(n + 1, 2).Value)) '下一表名
If n > 1 And this_table <> next_table Then '从第2行开始
TextFile.WriteLine ("----------------CREATE TABLE: " + table_cn + "----------------------------------")
DropSQL = "--DROP TABLE IF EXISTS " + SchemaName + "." + table_en + ";"
TextFile.WriteLine (DropSQL)
Create = "CREATE TABLE " + SchemaName + "." + table_en + vbLf + "("
TextFile.WriteLine (Create)
ColumnList = " " + Mid(ColumnList, 4, InStrRev(ColumnList, vbLf) - 4) '去除前面1个逗号和尾部换行符号
TextFile.WriteLine (ColumnList) '字段列表
TextFile.WriteLine (") ") '字段完成
TextFile.WriteLine ("COMMENT " + """" + table_cn) + """" '添加表注释
TextFile.WriteLine ("PARTITIONED BY (DYEAR STRING COMMENT """ + PartitionKey + "(年)""," + "DMONTH STRING COMMENT """ + PartitionKey + "(月)"") ") '设置分区
TextFile.WriteLine ("ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' ") '设置行格式
TextFile.WriteLine ("STORED AS ORC TBLPROPERTIES (""orc.compress""=""SNAPPY"");") '设置行格式
TextFile.WriteLine ("")
'初始化变量值
PrimaryKey = ""
NotNull = ""
Orientation = ""
CompressMode = ""
DistributeKey = ""
ColumnList = ""
PartitionKey = ""
End If
End If
Next
MsgBox "DDL成功生成在:" + vbLf + v_filename
End Sub