激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - python3.7通過thrift操作hbase的示例代碼

python3.7通過thrift操作hbase的示例代碼

2020-04-25 13:48CPP.LA Python

HBase是一個分布式的、面向列的開源數據庫,其是Apache的Hadoop項目的子項目。這篇文章主要介紹了python3.7通過thrift操作hbase的示例代碼,需要的朋友可以參考下

HBase是一個分布式的、面向列的開源數據庫,其是Apache的Hadoop項目的子項目。HBase不同于一般的關系數據庫,它是一個適合于非結構化數據存儲的數據庫。另一個不同的是HBase基于列的而不是基于行的模式。其數據結構類似與Redis的key-value模式。

python3.7通過thrift操作hbase的示例代碼

python3.7 通過 thrift , rpc 接口操作 hbase ,指定依賴庫為: thrift 和 hbase-thrift 。 然而我們 在 python3.7 環境中發現 hbase-thrift-0.20.4 無法被支持, hbase-thrift 官方僅推薦用于 python2.x 。 于是有了下邊的 patch 版本 和 patch 版本寫法的客戶端。

patch 版本下載,適用于 python 3.x : http://dl.cpp.la/Archive/hbase-thrift-0.20.4.patch.tgz

卸載 hbase-thrift-0.20.4 版本

?
1
2
3
4
# pip3 list | grep hbase-thrift
>> hbase-thrift    0.20.4
# pip3 uninstall hbase-thrift -y
>> Successfully uninstalled hbase-thrift-0.20.4

安裝 hbase-thrift-0.20.4.patch 版本(支持 python3.x )

?
1
2
3
4
wget http://dl.cpp.la/Archive/hbase-thrift-0.20.4.patch.tgz
tar -zxvf hbase-thrift-0.20.4.patch.tgz
cd hbase-thrift-0.20.4.patch
python3 setup.py install

檢測安裝是否成功

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# pip3 list | grep hbase-thrift
>> hbase-thrift    0.20.4.patch
Python3.7 操作 hbase-thrift-patch 客戶端代碼示例
from thrift.transport import TSocket
from thrift.transport.TTransport import TBufferedTransport
from thrift.protocol import TBinaryProtocol
 
from hbase import Hbase
from hbase.ttypes import ColumnDescriptor
from hbase.ttypes import Mutation
 
class HBaseClient(object):
 
  def __init__(self):
    self.__ip = HBASE_URI.get("HOST")
    self.__port = HBASE_URI.get("PORT")
    self.__transport = self.createSocket
    protocol = TBinaryProtocol.TBinaryProtocol(self.__transport)
    self.__client = Hbase.Client(protocol)
    self.__transport.open()
 
  @property
  def createSocket(self):
    CS = TSocket.TSocket(self.__ip, self.__port)
    CS.setTimeout(60*1000)
    return TBufferedTransport(CS)
 
  def __del__(self):
    self.__transport.close()
 
  def get_tables(self):
    """
    get all table name
    :return: table name list
    """
    return self.__client.getTableNames()
 
  def create_table(self, table, *columns):
    """
    create table
    :param table: table name
    :param columns: columns name , variable parameter
    """
    func = lambda col: ColumnDescriptor(col)
    column_families = list(map(func, columns))
    self.__client.createTable(table, column_families)
 
  def delete_table(self, table):
    '''
    delete table in hbase
    :param table: tableName
    :return:
    '''
    if self.__client.isTableEnabled(table):
      self.__client.disableTable(table)
    self.__client.deleteTable(table)
 
  def put(self, table, row, columns):
    """
    add record
    :param table: table name
    :param row:
    :param columns:
    :return:
    """
    self.__client.mutateRow(table, row, [Mutation(column=k, value=v) for k, v in columns.items()])
 
  def delete(self, table, row, column):
    """
    delete record
    :param table: table name
    :param row:
    """
    self.__client.deleteAll(table, row, column)
 
  def scan(self, table, start_row="", columns=None):
    """
    get record
    :param table: table name
    :param start_row:
    :param columns:
    """
    scanner = self.__client.scannerOpen(table, start_row, columns)
    while True:
      r = self.__client.scannerGet(scanner)
      if not r:
        break
      yield dict([(k, v.value) for k, v in r[0].columns.items()])
if __name__ == "__main__":
  client = HBaseClient()
  for v in client.scan('studentd', columns={"cpp.la":"https://cpp.la"}):
    print(v)
by:cpp.la

ps:python3.7連接hbase

pip安裝thrift 和hbase 包

?
1
2
3
4
5
6
7
8
9
10
11
12
from thrift.transport import TSocket,TTransport
from thrift.protocol import TBinaryProtocol,TCompactProtocol
from hbase import Hbase
socket = TSocket.TSocket('10.1.21.35',port=9090)
socket.setTimeout(5000)
transport = TTransport.TFramedTransport(socket)
protocol = TBinaryProtocol.TBinaryProtocol(transport) //不使用這個協議
protocol = TCompactProtocol.TCompactProtocol(transport)
client = Hbase.Client(protocol)
socket.open()
table = client.getTableNames()
print(table)

總結

以上所述是小編給大家介紹的python3.7通過thrift操作hbase的示例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
如果你覺得本文對你有幫助,歡迎轉載,煩請注明出處,謝謝!

原文鏈接:https://cpp.la/369.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: av影院在线播放 | 欧美成年人视频 | 精品一区二区三区网站 | 一本色道精品久久一区二区三区 | 国产韩国精品一区二区三区久久 | 日韩大片在线永久观看视频网站免费 | 亚洲网站在线 | 国产成人高清在线观看 | 黄色成人短视频 | 久久亚洲线观看视频 | 视频一区二区三区在线播放 | 久久久精品福利 | 五月天堂婷婷 | 一本色道久久99精品综合蜜臀 | 国产一级毛片视频在线! | 日韩精品二区 | 国产五区 | 99久久精品免费看国产小宝寻花 | 中文字幕偷拍 | 国产成人羞羞视频在线 | 中国女警察一级毛片视频 | 国产91精品欧美 | 国产精品久久久久久婷婷天堂 | 成人一区二区在线观看视频 | 色视频在线观看 | 欧美aaa| 91成人免费在线视频 | 欧美成年人在线视频 | 成年免费在线视频 | 亚洲第一黄色网 | 国产成人在线播放视频 | 久操免费在线视频 | 蜜桃网站免费 | 日韩在线黄 | 在线成人免费av | 国产精品久久久毛片 | 手机黄色小视频 | 在线成人av | 欧美一级久久 | 欧美人与zoxxxx另类9 | 日本a∨精品中文字幕在线 狠狠干精品视频 |