python脚本用sqoop把mysql数据导入hive数据仓库中

  1. #! /usr/bin/env python  
  2. # coding:utf-8  
  3. # --------------------------------  
  4. # Created by coco  on 16/2/23  
  5. # ---------------------------------  
  6. # Comment: 主要功能说明 :初始化业务数据库  
  7.   
  8. import os  
  9. import pyhs2  
  10.   
  11.   
  12. conn=pyhs2.connect(host="192.168.8.94",port=10000,authMechanism="PLAIN",user="hdfs")  
  13. mysql_info={"host":"192.168.1.200","port":3306,"user":"root","passwd":"123456"}  
  14. print mysql_info  
  15. def run_hive_query(sql):  
  16.     with conn.cursor()  as cursor:  
  17.         cursor.execute(sql)  
  18.         return cursor.fetchall()  
  19. def mysql_to_hive(host,port,user,passwd,database,table):  
  20.     #os.system("hadoop fs -rm    -r /user/task/%s"%table)  
  21.     if [database] not in run_hive_query("show databases"):  
  22.         with conn.cursor() as cursor:  
  23.             cursor.execute("create database " +database)  
  24.     with conn.cursor() as cursor:  
  25.         cursor.execute("use  "+database)  
  26.     if [table] not in run_hive_query("show tables"):  
  27.         os.system("sqoop   import --connect   jdbc:mysql://%s:%s/%s --username  %s   --password  %s --table %s  --hive-database  %s  -m 10 --create-hive-table --hive-import   --hive-overwrite "%(  
  28.             host,port,database,user,passwd,table,database))  
  29.     else:  
  30.         os.system("sqoop   import --connect   jdbc:mysql://%s:%s/%s --username  %s   --password  %s --table %s  --hive-database  %s  -m 10 --hive-import   --hive-overwrite "%(  
  31.             host,port,database,user,passwd,table,database))  
  32.   
  33.   
  34. mysql_to_hive(mysql_info["host"],mysql_info["port"],mysql_info["user"],mysql_info["passwd"].replace("(","\("),"wwn","cm_vip")  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值