python hbase讀取數(shù)據(jù)發(fā)送kafka的方法
更新時(shí)間:2018年12月27日 11:02:08 作者:meiguopai1
今天小編就為大家分享一篇python hbase讀取數(shù)據(jù)發(fā)送kafka的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
本例子實(shí)現(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')"
#過濾條件,當(dāng)前為 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)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- python每5分鐘從kafka中提取數(shù)據(jù)的例子
- python 消費(fèi) kafka 數(shù)據(jù)教程
- python kafka 多線程消費(fèi)者&手動(dòng)提交實(shí)例
- python操作kafka實(shí)踐的示例代碼
- 在python環(huán)境下運(yùn)用kafka對(duì)數(shù)據(jù)進(jìn)行實(shí)時(shí)傳輸?shù)姆椒?/a>
- kafka-python批量發(fā)送數(shù)據(jù)的實(shí)例
- 對(duì)python操作kafka寫入json數(shù)據(jù)的簡單demo分享
- python消費(fèi)kafka數(shù)據(jù)批量插入到es的方法
- 深入了解如何基于Python讀寫Kafka
相關(guān)文章
Python函數(shù)之zip函數(shù)的介紹與實(shí)際應(yīng)用
zip() 函數(shù)用于將可迭代的對(duì)象作為參數(shù),將對(duì)象中對(duì)應(yīng)的元素打包成一個(gè)個(gè)元組,然后返回由這些元組組成的對(duì)象(python2 返回的是這些元組組成的列表 ),下面這篇文章主要給大家介紹了關(guān)于Python函數(shù)之zip函數(shù)實(shí)際應(yīng)用的相關(guān)資料,需要的朋友可以參考下2022-03-03
Python參數(shù)解析器configparser簡介
configparser是python自帶的配置參數(shù)解析器,可以用于解析.config文件中的配置參數(shù),ini文件中由sections(節(jié)點(diǎn))-key-value組成,這篇文章主要介紹了Python參數(shù)解析器configparser,需要的朋友可以參考下2022-12-12
用Python從0開始實(shí)現(xiàn)一個(gè)中文拼音輸入法的思路詳解
中文輸入法是一個(gè)歷史悠久的問題,但也實(shí)在是個(gè)繁瑣的活,不知道這是不是網(wǎng)上很少有人分享中文拼音輸入法的原因,接下來通過本文給大家分享使用Python從0開始實(shí)現(xiàn)一個(gè)中文拼音輸入法,需要的朋友可以參考下2019-07-07
pytorch fine-tune 預(yù)訓(xùn)練的模型操作
這篇文章主要介紹了pytorch fine-tune 預(yù)訓(xùn)練的模型操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
Python人工智能學(xué)習(xí)PyTorch實(shí)現(xiàn)WGAN示例詳解
這篇文章主要為大家介紹了人工智能學(xué)習(xí)PyTorch實(shí)現(xiàn)WGAN的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-11-11

