首页 > 数据库技术 > 详细

MySQL to Hbase 数据的抽取

时间:2017-07-22 22:13:38      阅读:340      评论:0      收藏:0      [点我收藏+]

一个简单粗暴的方法从MySQL数据库抽取数据到Hbase实现的过程:

rowKey利用MySQL表的主键ID特性作为HBASE的id

code:

####

#!/usr/bin/env python

#coding=utf-8

import sys

reload(sys)

sys.setdefaultencoding(‘utf-8‘)

import MySQLdb

import datetime,time

sys.path.append(‘/usr/lib/python2.6/site-packages/hbase‘)

from thrift import Thrift

from thrift.transport import TSocket

from thrift.transport import TTransport

from thrift.protocol import TBinaryProtocol

from hbase import Hbase

from hbase.ttypes import *

import csv

from hbase.ttypes import ColumnDescriptor, Mutation, BatchMutation, TRegionInfo

from hbase.ttypes import IOError, AlreadyExists


def client_conn():

    transport=TSocket.TSocket("172.16.10.87",9090)

    transport=TTransport.TBufferedTransport(transport)

    protocol=TBinaryProtocol.TBinaryProtocol(transport)

    client=Hbase.Client(protocol)

    transport.open()

    return client

if __name__=="__main__":

client=client_conn()

conn = MySQLdb.connect(host="172.161.110.10", user="dlan", passwd="root123", port=5029, db=‘coolqi‘, charset=‘utf8‘)

cur = conn.cursor()

sql="select * from ca_record where ca_time>=STR_TO_DATE(‘20170720‘,‘%Y%m%d‘)"

print sql

cur.execute(sql)

data=cur.fetchall()

for k in xrange(len(data)):

            datalist2=[]

            rowKey=data[k][0]

   print rowKey

            user_id=data[k][1]

            ca_result=data[k][2]

            ca_time=data[k][3]

            real_name=data[k][4]

            id_card=data[k][5]

            sex=data[k][6]

            datalist=[user_id,ca_result,ca_time,real_name,id_card,sex]

   datalist1=["user_id","ca_result","ca_time","real_name","id_card","sex"]

   for j in range(len(datalist)):

                args=str(datalist[j])

#print args

                if isinstance(args,str):

                    mutations="[Mutation(column="+"‘"+datalist1[j]+‘:‘+str(j)+"‘"+","+"value="+"‘"+str(args)+"‘)"+"]"

   datalist2.append(mutations)

   #print datalist2

client.mutateRow(‘ca_record‘,str(rowKey),[Mutation(column=datalist1[j]+‘:‘+str(j),value=str(args))])

   #client.mutateRows(‘ca_record‘,datalist2)

###在for k in xrange(len(data)): 可以利用enumerate()函数减少上面的循环 .

本文出自 “DBAspace” 博客,请务必保留此出处http://dbaspace.blog.51cto.com/6873717/1950007

MySQL to Hbase 数据的抽取

原文:http://dbaspace.blog.51cto.com/6873717/1950007

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!