自己python实现mysql2hive 的表结构功能,在脚本后输入mysql_db,mysql_table,hive_db,hive_table和任意字符
来源:互联网 发布:java aop编程 编辑:程序博客网 时间:2024/05/16 07:59
#!/usr/bin/python# -*- coding: UTF-8 -*-import pymysqlimport sysdef get_table_info(mysql_db,mysql_table,hive_db,hive_table,ispartition = True): cols = [] create_head = '''create external table if not exists {0}.{1}('''.format(hive_db,hive_table) if ispartition: create_tail = r'''partitioned by(dt string)row format delimited fields terminated by '\001'location '/hivetable/{0}';'''.format(hive_table) else: create_tail = r'''row format delimited fields terminated by '\001'location '/hivetable/{0}';'''.format(hive_table) connection=pymysql.connect(host='localhost', user='root', password='56789', db=mysql_db, port=3306, charset='utf8' ) try: #获取一个游标 with connection.cursor(cursor=pymysql.cursors.DictCursor) as cursor: sql='SHOW FULL FIELDS FROM {0}'.format(mysql_table) cout=cursor.execute(sql) #返回记录条数 try: for row in cursor:#cursor.fetchall() # print(row) # print (row['Type']) cols.append(row['Field']) if 'bigint' in row['Type']: row['Type'] = "bigint" elif 'int' in row['Type'] or 'tinyint' in row['Type'] or 'smallint' in row['Type'] or 'mediumint' in row['Type'] or 'integer' in row['Type']: row['Type'] = "int" elif 'double' in row['Type'] or 'float' in row['Type'] or 'decimal' in row['Type']: row['Type'] = "double" else: row['Type'] = "string" create_head += row['Field'] + ' '+ row['Type'] +' comment \'' + row['Comment'] + '\' ,\n' except: print('程序异常!') finally: connection.close() # print (create_head[:-2]) create_str = create_head[:-2] + '\n' + ')'+ create_tail return cols,create_str # 返回字段列表与你建表语句if __name__ == '__main__': mysql_db=sys.argv[1] mysql_table= sys.argv[2] hive_db = sys.argv[3] hive_table = sys.argv[4] if (sys.argv[5]=='false') or (sys.argv[5]=='0'): ispartition = False else: ispartition = True cols, create_str = get_table_info(mysql_db,mysql_table,hive_db,hive_table,ispartition) # cols, create_str = get_table_info("date_dimension", "ods") # print(cols) print(create_str) create_str=create_str.encode("utf-8") document = open("/Users/wanghaiyang/Documents/testfile.txt", "w"); document.write(create_str); document.close();
阅读全文
0 0
- 自己python实现mysql2hive 的表结构功能,在脚本后输入mysql_db,mysql_table,hive_db,hive_table和任意字符
- c++ 自己实现getLine函数,可以接受任意的字符长度的输入
- 统计你输入的任意字符
- Leetcode712. 计算两个字符串删除任意字符后使二者相等的最小删除字符和
- 在数据库查询任意字符所在表和字段
- OS X 系统下实现python脚本工具在任意目录下直接使用
- 输入密码后自动登录功能的实现
- Shell中实现类似于结构体/数组功能的脚本
- 用C语言实现一个revert函数,它的功能是将输入的字符串在原串上倒序后返回
- 用C语言实现一个revert函数,它的功能是将输入的字符串在原串上倒序后返回...
- 用C语言实现一个revert函数,它的功能是将输入的字符串在原串上倒序后返回
- 用C语言实现一个revert函数,它的功能是将输入的字符串在原串上倒序后返回。
- 用C语言实现一个revert函数,它的功能是将输入的字符串在原串上倒序后返回
- 用C语言实现一个revert函数,它的功能是将输入的字符串在原串上倒序后返回
- 在Jenkins的Python脚本中模拟用户输入
- 如何在Python的.py文件里输入中文字符
- window自带超级终端如何显示自己输入的字符及其呼叫功能
- 自己写的链表的基本结构和其他功能
- python 显示 gif 动图
- jQuery点击小图展示大图
- 有用的链接
- 如何获取taskflow的id值,用于控制当前页面的控件显示或隐藏
- git的使用
- 自己python实现mysql2hive 的表结构功能,在脚本后输入mysql_db,mysql_table,hive_db,hive_table和任意字符
- HLS自适应测试环境搭建
- RecyclerView
- JDBC技术
- 使用静态busybox 性能提升, 确一直coredump 的问题
- nginx统计访问量最高的ip
- cmd命令详情
- Okhttp 需要的依赖
- 移植openssl到32位的mips核心板上的详细步骤及遇到的问题解决