自己python实现mysql2hive 的表结构功能,在脚本后输入mysql_db,mysql_table,hive_db,hive_table和任意字符

#!/usr/bin/python
# -*- coding: UTF-8 -*-



import  pymysql
import  sys

def 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();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值