python hbase读取数据发送kafka的方法

yipeiwu_com5年前Python基础

本例子实现从hbase获取数据,并发送kafka。

使用

#!/usr/bin/env python
#coding=utf-8
 
import sys
import time
import json
 
sys.path.append('/usr/local/lib/python3.5/site-packages')
from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from hbase1 import Hbase #调用hbase thrif1
from hbase1.ttypes import *
from kafka import KafkaConsumer
from kafka import KafkaProducer
from kafka.errors import KafkaError
import unittest
 
class HbaseOpreator:
 def __init__(self,host,port,table='test'):
  self.tableName=table
  self.transport=TTransport.TBufferedTransport(TSocket.TSocket(host,port))
  self.protocol=TBinaryProtocol.TBinaryProtocol(self.transport)
  self.client=Hbase.Client(self.protocol)
  self.transport.open()
 
 def __del__(self):
  self.transport.close()
 
 
 def scanTablefilter(self,table,*args):
  d=dict() 
  L=[]
  try:
   tableName=table
   # scan = Hbase.TScan(startRow, stopRow)
   scan=TScan()
   #主键首字母123
   # filter = "PrefixFilter('123_')"
   # filter = "RowFilter(=,'regexstring:.aaa')"
   #过滤条件,当前为 statis_date 字段,值为20170223
   # fitler = "SingleColumnValueFilter(tableName,'f','statis_date','20170223')"
   # filter="SingleColumnValueFilter('f','statis_date',=,'binary:20170223') AND SingleColumnValueFilter('f','name',=,'binary:LXS')"
   filter="SingleColumnValueFilter('info','name',=,'binary:lilei') OR SingleColumnValueFilter('info','name',=,'binary:lily')"
   scan.filterString=filter
   id=self.client.scannerOpenWithScan(tableName,scan,None)
   result=self.client.scannerGet(id)
   # result=self.client.scannerGetList(id,100)
   while result:
    for r in result:
     key=r.row
     name=r.columns.get('info:name').value
     age=r.columns.get('info:age').value
     phone=r.columns.get('info:phone').value
     d['key']=key
     d['name']=name
     d['age']=age
     d['phone']=phone
     # encode_result_json=json.dumps(d).encode(encoding="utf-8")
     # print(encode_result_json)
     L.append(d)         
    result=self.client.scannerGet(id)    
   return json.dumps(L).encode(encoding="utf-8")  
  finally:
   # self.client.scannerClose(scan)
   print("scan finish")
 
def sendKfafkaProduct(data):
 # self.host_port='localhost:9092'
 producer = KafkaProducer(bootstrap_servers=['localhost:9092'])
 for d in data:
  producer.send('test', key=b'lxs', value=d)
  time.sleep(5)
  print(d)
 
 while True:
  producer.send('test', key=b'lxs', value=data)
  time.sleep(5)
  print(data)
 
if __name__== '__main__':
 # unittest.main()
 
 B=HbaseOpreator('10.27.1.138',9090)
 value=B.scanTablefilter('ns_lbi:test_hbase_student')
 print(value)
 #sendKfafkaProduct(value)
 

以上这篇python hbase读取数据发送kafka的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python利用wx实现界面按钮和按钮监听和字体改变的方法

python利用wx实现界面按钮和按钮监听和字体改变的方法

wxPython是Python语言的一套优秀的GUI图形库。允许Python程序员很方便的创建完整的、功能键全的GUI用户界面。 wxPython是作为优秀的跨平台GUI库wxWidge...

详解Python中字符串前“b”,“r”,“u”,“f”的作用

1、字符串前加 u 例:u"我是含有中文字符组成的字符串。" 作用: 后面字符串以 Unicode 格式 进行编码,一般用在中文字符串前面,防止因为源码储存格式问题,导致再次使用时出现乱...

python使用线程封装的一个简单定时器类实例

本文实例讲述了python使用线程封装的一个简单定时器类。分享给大家供大家参考。具体实现方法如下: from threading import Timer class MyTimer...

python查询sqlite数据表的方法

本文实例讲述了python查询sqlite数据表的方法。分享给大家供大家参考。具体实现方法如下: import sqlite3 as db conn = db.connect('my...

python中如何使用分步式进程计算详解

python中如何使用分步式进程计算详解

前言 在python中使用多进程和多线程都能达到同时运行多个任务,和多进程和多线程的选择上,应该优先选择多进程的方式,因为多进程更加稳定,且对于进程的操作管理也更加方便,但有一点是多进程...