公众号平台登陆入口,seo在线教程,google优化排名,网页设计和网站开发本人因为工作原因#xff0c;经常使用hive以及presto#xff0c;一般是编写hive完成工作#xff0c;服务器原因#xff0c;presto会跑的更快一些#xff0c;所以工作的时候会使用presto验证结果#xff0c;所以就要频繁hive转presto#xff0c;为了方便#xff0c;我用… 本人因为工作原因经常使用hive以及presto一般是编写hive完成工作服务器原因presto会跑的更快一些所以工作的时候会使用presto验证结果所以就要频繁hive转presto为了方便我用Python编写了一个转换代码的小程序工作繁忙我一点点更新吧。
一、厘清需求 首先思考具体的需求
识别加减乘除等基本运算presto对于字段属性有严格要求所以为了一劳永逸将涉及到计算的字段全部cast为double但是要注意字符串中出现运算符的这是不能改的。识别,,,!,,,这些判断符一般用在条件语句wherejoin中。以ab为例1a是字段b是一个字符串那么a就要cast为varchar并且b不需要任何cast2如果b是数值那么a要cast为double并且b不需要任何cas3如果b和时间有关那么a就要case为date4如果两边都是字段那就都cast为varchar。常用的一些函数比如substrdatediffconcatsum等等都要转换成对应的presto函数用法。使用tkinter创造一个简单的交互界面方便实时的输入输出如果有错误就简单的反应错误。
二、代码思路 先处理加减乘除运算判断符然后再处理函数然后添加ui交互界面最后将代码简单包装为类条件允许就包装为应用程序但是公司电脑权限高所以不太可行。
三、Python代码
import re
import os
from tkinter import *class Hive2Presto:def __int__(self):self.t_funcs [substr, nvl, substring, unix_timestamp] \[to_date, concat, sum, avg, abs, year, month, ceiling, floor]self.time_funcs [date_add, datediff, add_months]self.funcs self.t_funcs self.time_funcsself.current_path os.path.abspath(__file__)self.dir os.path.dirname(self.current_path)self.result []self.error []self.filename def main(self):self.root Tk()self.root.config(bg#ff741d) # 背景颜色设置为公司主题色^_^self.root.title(Hive转Presto)self.win_width 550self.win_height 500self.screen_width self.root.winfo_screenwidth()self.screen_height self.root.winfo_screenheight()self.x (self.screen_width - self.win_width) // 2self.y (self.screen_height - self.win_height) // 2self.root.geometry(f{self.win_width}x{self.win_height}{self.x}{self.y})font (楷体, 11)self.button Button(self.root, text转换, commandself.trans, bg#ffcc8c, fontfont, anchore)self.button.grid(row0, column0, padx100, pady10, stickyW)self.file_button Button(self.root, text选择文件, commandself.choose_file, bg#ffcc8c, fontfont,anchore)self.file_button.grid(row0, column1, padx0, pady10, stickyW)self.entry Entry(self.root, width65, fontfont)self.entry.insert(0, 输入Hive代码)self.entry.grid(row1, column0, padx10, pady10, columnspan2)self.entry.bind(Button-1, self.delete_text)self.text Text(self.root, width75, height20)self.text.grid(row2, column0, padx10, pady10, columnspan2)self.des_label Label(self.root, text可以复制结果也有生成的文件与选取的文件同文件夹, bg#ffcc8c,font(楷体, 10))self.des_label.grid(row3, column0, padx10, pady10, columnspan2)s for i in range(0, (n : len(self.funcs)), 4):if i 4 n:s ,.join(self.funcs[i:i 4]) \nelse:s ,.join(self.funcs[i:]) \ns s[:-1]self.des_label1 Label(self.root, texts, bg#ffcc8c,font(楷体, 10))self.des_label1.grid(row4, column0, padx10, pady10, columnspan2)self.root.columnconfigure(0, minsize10)self.root.columnconfigure(1, minsize10)self.root.columnconfigure(0, pad5)self.root.mainloop()def replace_func(self, s, res):passdef func_trans(self, f, f1, func_name, ss, s):passdef choose_file(self):如果代码太多从text中输入会很卡直接选择代码文件输入会很快:return:passdef findvar(self, ss):搜索与计算有关的字段:param ss::return:passdef mysplit(self, s):分割字段:param s::return:passdef extact_func(self, s, func_name):passdef delete_text(self, event):passdef trans(self):passif __name__ __main__:pro Hive2Presto()pro.__int__()pro.main()先把大致框架写出来其实我已经完成了但是代码都在公司电脑传不出来需要我重新再打一遍顺便看看能不能再优化优化。UI界面大致如下