Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- app.py +72 -0
- hive_copyfields_extract.py +34 -0
- json_2_incr_ddl.py +188 -0
app.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from json_2_incr_ddl import get_ddl_results
|
| 3 |
+
from hive_copyfields_extract import get_extract_fields
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def point_2_ddl(topic_name, biz_name, json_example):
|
| 8 |
+
return get_ddl_results(topic_name, biz_name, json_example)
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def hive_copy_fields_2_str(hive_copy_str):
|
| 12 |
+
return get_extract_fields(hive_copy_str)
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
with gr.Blocks() as demo:
|
| 16 |
+
json_str = '''
|
| 17 |
+
{
|
| 18 |
+
"Email": "835610010@qq.com",
|
| 19 |
+
"UseType": 946,
|
| 20 |
+
"Lang": 2,
|
| 21 |
+
"Sub": "22",
|
| 22 |
+
"HtmlBody": "22",
|
| 23 |
+
"HasBody": true
|
| 24 |
+
}
|
| 25 |
+
'''
|
| 26 |
+
hue_fields = '''
|
| 27 |
+
fid (bigint)
|
| 28 |
+
fuserid (bigint)
|
| 29 |
+
froomid (bigint)
|
| 30 |
+
ftype (int)
|
| 31 |
+
fcount (int)
|
| 32 |
+
frecordtime (timestamp)
|
| 33 |
+
fcreatetime (timestamp)
|
| 34 |
+
fmodifytime (timestamp)
|
| 35 |
+
'''
|
| 36 |
+
|
| 37 |
+
with gr.Tab("根据json生成ddl"):
|
| 38 |
+
with gr.Row():
|
| 39 |
+
with gr.Column():
|
| 40 |
+
json_input = [
|
| 41 |
+
gr.Text(label="topic名称", placeholder="Email_Send_Topic"),
|
| 42 |
+
gr.Text(label="业务线名称", placeholder="ludo"),
|
| 43 |
+
gr.Text(label="Json类埋点单条示例", lines=6, placeholder=json_str)
|
| 44 |
+
]
|
| 45 |
+
json_button = gr.Button("Click")
|
| 46 |
+
with gr.Column():
|
| 47 |
+
ddl_output = [
|
| 48 |
+
gr.Text(label="incr表", show_copy_button=True, text_align="left"),
|
| 49 |
+
gr.Text(label="incr_text表", show_copy_button=True, text_align="left"),
|
| 50 |
+
gr.Text(label="incr_explode表", show_copy_button=True, text_align="left"),
|
| 51 |
+
gr.Text(label="explode sql", show_copy_button=True, text_align="left"),
|
| 52 |
+
gr.Text(label="azkaban job", show_copy_button=True, text_align="left")
|
| 53 |
+
]
|
| 54 |
+
|
| 55 |
+
json_button.click(point_2_ddl, inputs=json_input, outputs=ddl_output)
|
| 56 |
+
|
| 57 |
+
with gr.Tab("根据hue复制字段生成特定字串"):
|
| 58 |
+
with gr.Row():
|
| 59 |
+
with gr.Column():
|
| 60 |
+
hue_input = gr.Text(label="从hue中复制的字段内容", placeholder=hue_fields, lines=10)
|
| 61 |
+
hue_button = gr.Button("Click")
|
| 62 |
+
with gr.Column():
|
| 63 |
+
fields_output = [
|
| 64 |
+
gr.Text(label="不带“,”的字段", show_copy_button=True, text_align="left"),
|
| 65 |
+
gr.Text(label="带“,”的字段", show_copy_button=True, text_align="left"),
|
| 66 |
+
gr.Text(label="没有换行符的字段", show_copy_button=True, text_align="left"),
|
| 67 |
+
gr.Text(label="特殊字段", show_copy_button=True, text_align="left")
|
| 68 |
+
]
|
| 69 |
+
|
| 70 |
+
hue_button.click(hive_copy_fields_2_str, inputs=hue_input, outputs=fields_output)
|
| 71 |
+
|
| 72 |
+
demo.launch(debug=True)
|
hive_copyfields_extract.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
# 将从hue中copy过来的fields转换为固定格式的字符串
|
| 5 |
+
def get_extract_fields(copy_str):
|
| 6 |
+
filed_arr = copy_str.split("\n")
|
| 7 |
+
with_comma = ""
|
| 8 |
+
no_comma = ""
|
| 9 |
+
with_comma_no_backspace = ""
|
| 10 |
+
special = ""
|
| 11 |
+
for k in range(len(filed_arr)):
|
| 12 |
+
fa = filed_arr[k]
|
| 13 |
+
field = fa.split(" ")[0]
|
| 14 |
+
no_comma += "%s\r\n" % field
|
| 15 |
+
with_comma += "%s,\r\n" % field
|
| 16 |
+
with_comma_no_backspace += "%s," % field
|
| 17 |
+
special += "coalesce(t2.%s,t1.%s) %s,\r\n" % (field, field, field)
|
| 18 |
+
|
| 19 |
+
return [no_comma, with_comma, with_comma_no_backspace, special]
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
# Press the green button in the gutter to run the script.
|
| 23 |
+
if __name__ == '__main__':
|
| 24 |
+
# 直接复制json数据赋值json_str
|
| 25 |
+
filed_str = """fid (bigint)
|
| 26 |
+
fuserid (bigint)
|
| 27 |
+
froomid (bigint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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
def get_ddl_results(topic, biz, json_str):
|
| 5 |
+
topic = topic.lower()
|
| 6 |
+
biz = biz.lower()
|
| 7 |
+
error_str = f"ERROR: not a valid json"
|
| 8 |
+
try:
|
| 9 |
+
json_obj = json.loads(json_str)
|
| 10 |
+
except Exception as e:
|
| 11 |
+
print("ERROR,",e)
|
| 12 |
+
return [error_str, error_str, error_str, error_str, error_str]
|
| 13 |
+
|
| 14 |
+
# 生成incr_text表
|
| 15 |
+
incr_txt_ddl = """CREATE TABLE `ods_%s.ods_kafka_%s_incr_text`(
|
| 16 |
+
`data` string COMMENT '原始数据',
|
| 17 |
+
`kafka_topic` string COMMENT 'topic名称',
|
| 18 |
+
`kafka_partition` bigint COMMENT 'kafka分区',
|
| 19 |
+
`kafka_offset` bigint COMMENT 'kafka位移',
|
| 20 |
+
`kafka_time` timestamp COMMENT 'kafka消费时间',
|
| 21 |
+
`processtime` string COMMENT '消费时间')
|
| 22 |
+
COMMENT ''
|
| 23 |
+
PARTITIONED BY (
|
| 24 |
+
`dt` string,
|
| 25 |
+
`load_time` string)
|
| 26 |
+
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\\001' STORED AS TEXTFILE;
|
| 27 |
+
""" % (biz, topic)
|
| 28 |
+
|
| 29 |
+
# 生成incr表
|
| 30 |
+
incr_ddl = """CREATE TABLE `ods_%s.ods_kafka_%s_incr`(
|
| 31 |
+
`data` string COMMENT '原始数据',
|
| 32 |
+
`kafka_topic` string COMMENT 'topic名称',
|
| 33 |
+
`kafka_partition` bigint COMMENT 'kafka分区',
|
| 34 |
+
`kafka_offset` bigint COMMENT 'kafka位移',
|
| 35 |
+
`kafka_time` timestamp COMMENT 'kafka消费时间',
|
| 36 |
+
`processtime` string COMMENT '消费时间')
|
| 37 |
+
COMMENT ''
|
| 38 |
+
PARTITIONED BY (
|
| 39 |
+
`dt` string,
|
| 40 |
+
`load_time` string)
|
| 41 |
+
stored as orc tblproperties ("orc.compress"="snappy");
|
| 42 |
+
""" % (biz, topic)
|
| 43 |
+
|
| 44 |
+
# 拼field
|
| 45 |
+
field_str = ""
|
| 46 |
+
sql_field_str = ""
|
| 47 |
+
field_str_with_single_quote = ""
|
| 48 |
+
field_str_with_back_quote = ""
|
| 49 |
+
for key in json_obj.keys():
|
| 50 |
+
field_str_with_single_quote += f",'{key}'"
|
| 51 |
+
field_str_with_back_quote += f"`{key}`,"
|
| 52 |
+
if type(json_obj.get(key)) == bool:
|
| 53 |
+
sql_field_str += f"if(t2.{key} == 'true',true,false) {key},\r\n"
|
| 54 |
+
else:
|
| 55 |
+
sql_field_str += f"t2.{key},\r\n"
|
| 56 |
+
|
| 57 |
+
if type(json_obj.get(key)) == int:
|
| 58 |
+
field_str += "`%s` bigint comment '',\r\n" % key
|
| 59 |
+
elif type(json_obj.get(key)) == float:
|
| 60 |
+
field_str += "`%s` decimal(18,4) comment '',\r\n" % key
|
| 61 |
+
elif type(json_obj.get(key)) == bool:
|
| 62 |
+
field_str += "`%s` boolean comment '',\r\n" % key
|
| 63 |
+
elif 'time' in key.lower():
|
| 64 |
+
field_str += "`%s` timestamp comment '',\r\n" % key
|
| 65 |
+
else:
|
| 66 |
+
field_str += "`%s` string comment '',\r\n" % key
|
| 67 |
+
# 生辰incr_explode表
|
| 68 |
+
incr_explode_ddl = """CREATE TABLE `ods_%s.ods_kafka_%s_incr_explode`(
|
| 69 |
+
%s`kafka_topic` string COMMENT 'topic名称',
|
| 70 |
+
`kafka_partition` bigint COMMENT 'kafka分区',
|
| 71 |
+
`kafka_offset` bigint COMMENT 'kafka位移',
|
| 72 |
+
`kafka_time` timestamp COMMENT 'kafka消费时间',
|
| 73 |
+
`processtime` string COMMENT '消费时间')
|
| 74 |
+
COMMENT ''
|
| 75 |
+
PARTITIONED BY (
|
| 76 |
+
`dt` string)
|
| 77 |
+
stored as orc tblproperties ("orc.compress"="snappy");
|
| 78 |
+
""" % (biz, topic, field_str)
|
| 79 |
+
|
| 80 |
+
# 生成一般的explode sql
|
| 81 |
+
explode_sql = f"""set hive.exec.dynamic.partition.mode=nonstrict;
|
| 82 |
+
set mapred.min.split.size.per.node=100000000;
|
| 83 |
+
set mapred.min.split.size.per.rack=100000000;
|
| 84 |
+
set hive.execution.engine=mr;
|
| 85 |
+
|
| 86 |
+
insert into table ods_{biz}.ods_kafka_{topic}_incr_explode partition(dt='${{etl_date}}')
|
| 87 |
+
select
|
| 88 |
+
{sql_field_str}
|
| 89 |
+
kafka_topic,
|
| 90 |
+
kafka_partition,
|
| 91 |
+
kafka_offset,
|
| 92 |
+
kafka_time,
|
| 93 |
+
processtime
|
| 94 |
+
from ods_{biz}.ods_kafka_{topic}_incr t1
|
| 95 |
+
lateral view json_tuple(t1.data{field_str_with_single_quote}) t2 as {field_str_with_back_quote[:-1]}
|
| 96 |
+
where dt = '${{etl_date}}'
|
| 97 |
+
and load_time = '${{load_time}}';
|
| 98 |
+
"""
|
| 99 |
+
|
| 100 |
+
explode_job = f"""type=command
|
| 101 |
+
command=sh ${{SHELL_PATH}}/load_into_hive_table_explode_point.sh /data/ludo_json/{topic} {topic} ods_{biz} ods_kafka_{topic}_incr ods_{biz} ods_kafka_{topic}_incr_explode ${{dt}} mr
|
| 102 |
+
retries=2
|
| 103 |
+
retry.backoff=5000
|
| 104 |
+
"""
|
| 105 |
+
|
| 106 |
+
return [incr_ddl, incr_txt_ddl, incr_explode_ddl, explode_sql, explode_job]
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
# Press the green button in the gutter to run the script.
|
| 110 |
+
if __name__ == '__main__':
|
| 111 |
+
get_ddl_results("")
|
| 112 |
+
quit()
|
| 113 |
+
|
| 114 |
+
# 预制信息
|
| 115 |
+
topic = "Email_Send_Topic"
|
| 116 |
+
biz = "ludo"
|
| 117 |
+
# 直接复制json数据赋值json_str
|
| 118 |
+
json_obj = """
|
| 119 |
+
{
|
| 120 |
+
"Email": "835610010@qq.com",
|
| 121 |
+
"UseType": 946,
|
| 122 |
+
"Lang": 2,
|
| 123 |
+
"Sub": "22",
|
| 124 |
+
"HtmlBody": "22",
|
| 125 |
+
"HasBody": True
|
| 126 |
+
}
|
| 127 |
+
"""
|
| 128 |
+
# 预制信息 END
|
| 129 |
+
|
| 130 |
+
# 生成incr_text表
|
| 131 |
+
incr_txt_ddl = """CREATE TABLE `ods_%s.ods_kafka_%s_incr_text`(
|
| 132 |
+
`data` string COMMENT '原始数据',
|
| 133 |
+
`kafka_topic` string COMMENT 'topic名称',
|
| 134 |
+
`kafka_partition` bigint COMMENT 'kafka分区',
|
| 135 |
+
`kafka_offset` bigint COMMENT 'kafka位移',
|
| 136 |
+
`kafka_time` timestamp COMMENT 'kafka消费时间',
|
| 137 |
+
`processtime` string COMMENT '消费时间')
|
| 138 |
+
COMMENT ''
|
| 139 |
+
PARTITIONED BY (
|
| 140 |
+
`dt` string,
|
| 141 |
+
`load_time` string)
|
| 142 |
+
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\\001' STORED AS TEXTFILE;
|
| 143 |
+
""" % (biz, topic)
|
| 144 |
+
|
| 145 |
+
# 生成incr表
|
| 146 |
+
incr_ddl = """CREATE TABLE `ods_%s.ods_kafka_%s_incr`(
|
| 147 |
+
`data` string COMMENT '���始数据',
|
| 148 |
+
`kafka_topic` string COMMENT 'topic名称',
|
| 149 |
+
`kafka_partition` bigint COMMENT 'kafka分区',
|
| 150 |
+
`kafka_offset` bigint COMMENT 'kafka位移',
|
| 151 |
+
`kafka_time` timestamp COMMENT 'kafka消费时间',
|
| 152 |
+
`processtime` string COMMENT '消费时间')
|
| 153 |
+
COMMENT ''
|
| 154 |
+
PARTITIONED BY (
|
| 155 |
+
`dt` string,
|
| 156 |
+
`load_time` string)
|
| 157 |
+
stored as orc tblproperties ("orc.compress"="snappy");
|
| 158 |
+
""" % (biz, topic)
|
| 159 |
+
|
| 160 |
+
# 拼field
|
| 161 |
+
field_str = ""
|
| 162 |
+
for key in json_obj.keys():
|
| 163 |
+
if type(json_obj.get(key)) == int:
|
| 164 |
+
field_str += "`%s` bigint comment '',\r\n" % key
|
| 165 |
+
elif type(json_obj.get(key)) == float:
|
| 166 |
+
field_str += "`%s` decimal(18,4) comment '',\r\n" % key
|
| 167 |
+
elif type(json_obj.get(key)) == bool:
|
| 168 |
+
field_str += "`%s` boolean comment '',\r\n" % key
|
| 169 |
+
elif 'time' in key.lower():
|
| 170 |
+
field_str += "`%s` timestamp comment '',\r\n" % key
|
| 171 |
+
else:
|
| 172 |
+
field_str += "`%s` string comment '',\r\n" % key
|
| 173 |
+
# 生辰incr_explode表
|
| 174 |
+
incr_explode_ddl = """CREATE TABLE `ods_%s.ods_kafka_%s_incr_explode`(
|
| 175 |
+
%s`kafka_topic` string COMMENT 'topic名称',
|
| 176 |
+
`kafka_partition` bigint COMMENT 'kafka分区',
|
| 177 |
+
`kafka_offset` bigint COMMENT 'kafka位移',
|
| 178 |
+
`kafka_time` timestamp COMMENT 'kafka消费时间',
|
| 179 |
+
`processtime` string COMMENT '消费时间')
|
| 180 |
+
COMMENT ''
|
| 181 |
+
PARTITIONED BY (
|
| 182 |
+
`dt` string)
|
| 183 |
+
stored as orc tblproperties ("orc.compress"="snappy");
|
| 184 |
+
""" % (biz, topic, field_str)
|
| 185 |
+
|
| 186 |
+
print(incr_txt_ddl)
|
| 187 |
+
print(incr_ddl)
|
| 188 |
+
print(incr_explode_ddl)
|