python hbase讀取數(shù)據(jù)發(fā)送kafka的方法
更新時間:2018年12月27日 11:02:08 作者:meiguopai1
今天小編就為大家分享一篇python hbase讀取數(shù)據(jù)發(fā)送kafka的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
本例子實現(xiàn)從hbase獲取數(shù)據(jù),并發(fā)送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 #調(diào)用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讀取數(shù)據(jù)發(fā)送kafka的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Python函數(shù)之zip函數(shù)的介紹與實際應用
zip() 函數(shù)用于將可迭代的對象作為參數(shù),將對象中對應的元素打包成一個個元組,然后返回由這些元組組成的對象(python2 返回的是這些元組組成的列表 ),下面這篇文章主要給大家介紹了關于Python函數(shù)之zip函數(shù)實際應用的相關資料,需要的朋友可以參考下2022-03-03
Python參數(shù)解析器configparser簡介
configparser是python自帶的配置參數(shù)解析器,可以用于解析.config文件中的配置參數(shù),ini文件中由sections(節(jié)點)-key-value組成,這篇文章主要介紹了Python參數(shù)解析器configparser,需要的朋友可以參考下2022-12-12
用Python從0開始實現(xiàn)一個中文拼音輸入法的思路詳解
中文輸入法是一個歷史悠久的問題,但也實在是個繁瑣的活,不知道這是不是網(wǎng)上很少有人分享中文拼音輸入法的原因,接下來通過本文給大家分享使用Python從0開始實現(xiàn)一個中文拼音輸入法,需要的朋友可以參考下2019-07-07
Python人工智能學習PyTorch實現(xiàn)WGAN示例詳解
這篇文章主要為大家介紹了人工智能學習PyTorch實現(xiàn)WGAN的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2021-11-11

