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)