python hive 插入mysql_Python操作HIve,将数据插入到Mysql

该博客展示了如何使用Python连接Hive服务器,执行查询并将数据插入到MySQL数据库中。通过Thrift库建立Hive连接,执行SQL添加jar包,查询Hive表,并将查询结果写入MySQL。
摘要由CSDN通过智能技术生成

Python操作HIve,将数据插入到Mysql

import sys

from hive_service import ThriftHive

from hive_service.ttypes import HiveServerException

from thrift import Thrift

from thrift.transport import TSocket

from thrift.transport import TTransport

from thrift.protocol import TBinaryProtocol

from python6 import mysqlExe

from python6 import hiveExeUpdate

try:

transport = TSocket.TSocket('dell3', 10000)

transport = TTransport.TBufferedTransport(transport)

protocol = TBinaryProtocol.TBinaryProtocol(transport)

client = ThriftHive.Client(protocol)

transport.open()

client.execute('ADD jar /home/hduser/hive-0.12.0/lib/hive-contrib-0.12.0.jar')

query = '''select count(*) from  smit_test limit 10'''

client.execute(query)

row = client.fetchOne()

print row

print mysqlExe("insert into first(counttime) values ("+"'"+row+"')")

transport.close()

except Thrift.TException, tx:

print '%s' % (tx.message)

===============================================

import MySQLdb

def mysqlExe(sql):

conn = MySQLdb.connect (host = "192.168.60.29",

user = "hive",

passwd = "111111",

db = "test")

cursor = conn.cursor ()

cursor.execute (sql)

cursor.close ()

conn.close ()

def hiveExeUpdate(sql,db,tableName,column,date):

try:

transport = TSocket.TSocket('192.168.60.29', 10000)

transport = TTransport.TBufferedTransport(transport)

protocol = TBinaryProtocol.TBinaryProtocol(transport)

client = ThriftHive.Client(protocol)

transport.open()

client.execute(sql)

update_sql=  " update  " + tableName + " set " + column + " = " + client.fetchOne() + " where id = '" + date + "'"

mysqlExe(update_sql)

transport.close()

except Thrift.TException, tx:

print '%s' % (tx.message)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值