转:https://blog.csdn.net/wulantian/article/details/53064123

用python把mysql数据库的数据导入到hive中,该过程主要是通过python语言操作sqoop.

#! /usr/bin/env python
# coding:utf-8
# --------------------------------
# Created by coco on 16/2/23
# ---------------------------------
# Comment: 主要功能说明 :初始化业务数据库 import os
import pyhs2 conn=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_info
def 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")

最新文章

  1. BZOJ 3639: Query on a tree VII
  2. 动画的使用—Drawable Animation
  3. 强大的打印功能jatoolsPrinter使用总结
  4. Tomcat:bio nio 的设计
  5. Power of Two
  6. POSTGRESQL 数据库导入导出
  7. spring源码 — 一、IoC容器初始化
  8. asp.net runat="server" && hiddenfield
  9. 海量数据的二度人脉挖掘算法(Hadoop 实现)
  10. BZOJ 2298 problem a(区间DP)
  11. C++的构造函数总结
  12. DenyHosts 安装及配置详解
  13. Java多线程-新特征-锁
  14. 上传文件块client实现
  15. 注意,WebDeploy服务会占用80端口。(Windows关闭了IIS,80端口任然被占用)
  16. 如何使用cygwin去编译cocos2dx项目中的C++文件
  17. 【English】十三、英语中的连词有哪些,都有什么作用
  18. 题解-PKUWC2018 随机游走
  19. Intellij idea 2017 图标含义
  20. C++中类的多继承

热门文章

  1. Asp.Net Core 实现服务的批量注册注入
  2. MongoDB在Linux系统下的安装与启动
  3. Linux 进入 5.0 时代!
  4. Filebeat原理与简单配置入门
  5. vuex的使用步骤
  6. .Net Core应用框架Util介绍(三)
  7. mybatis 使用缓存策略
  8. 【学习总结】GirlsInAI ML-diary day-11-while循环
  9. PHP整洁之道
  10. [官网]Using PuTTY