python指令碼 用sqoop把mysql資料匯入hive

來源:互聯網
上載者:User

標籤:資料匯入   說明   過程   hal   host   ons   pac   mysq   python指令碼   

轉:53064123

用python把mysql資料庫的資料匯入到hive中,該過程主要是通過python語言操作sqoop.

#! /usr/bin/env python# coding:utf-8# --------------------------------# Created by coco  on 16/2/23# ---------------------------------# Comment: 主要功能說明 :初始化業務資料庫import osimport pyhs2conn=pyhs2.connect(host="192.168.8.94",port=10000,authMechanism="PLAIN",user="hdfs")mysql_info={"host":"192.168.8.94","port":3306,"user":"root","passwd":"gc895316"}print mysql_infodef run_hive_query(sql):    with conn.cursor()  as cursor:        cursor.execute(sql)        return cursor.fetchall()def mysql_to_hive(host,port,user,passwd,database,table):    #os.system("hadoop fs -rm    -r /user/task/%s"%table)    if [database] not in run_hive_query("show databases"):        with conn.cursor() as cursor:            cursor.execute("create database " +database)    with conn.cursor() as cursor:        cursor.execute("use  "+database)    if [table] not in run_hive_query("show tables"):        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 "%(            host,port,database,user,passwd,table,database))    else:        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 "%(            host,port,database,user,passwd,table,database))mysql_to_hive(mysql_info["host"],mysql_info["port"],mysql_info["user"],mysql_info["passwd"].replace("(","\("),"wwn","cm_vip")

 

python指令碼 用sqoop把mysql資料匯入hive

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.