-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathTwitBase.py
executable file
·58 lines (44 loc) · 1.56 KB
/
TwitBase.py
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
#! /usr/bin/env python
import sys
from thrift.transport import TSocket, TTransport
from thrift.protocol import TBinaryProtocol
from hbase import Hbase
from hbase.ttypes import *
usage = """TwitBase.py action ...
help - print this messsage and exit
list - list all installed users."""
class TwitBaseConn(object):
def __init__(self, host="localhost", port=9090):
transport = TSocket.TSocket(host, port)
self.transport = TTransport.TBufferedTransport(transport)
self.protocol = TBinaryProtocol.TBinaryProtocol(self.transport)
self.client = Hbase.Client(self.protocol)
self.transport.open()
def close(self):
self.transport.close()
def _user_from_row(self, row):
user = {}
for col,cell in row.columns.items():
user[col[5:]] = cell.value
return "<User: {user}, {name}, {email}>".format(**user)
def scan_users(self):
columns = ['info:user','info:name','info:email']
scanner = self.client.scannerOpen('users', '', columns)
row = self.client.scannerGet(scanner)
while row:
yield self._user_from_row(row[0])
row = self.client.scannerGet(scanner)
self.client.scannerClose(scanner)
def main(args=None):
if args is None:
args = sys.argv[1:]
if len(args) == 0 or 'help' == args[0]:
print usage
raise SystemExit()
twitBase = TwitBaseConn()
if args[0] == 'list':
for user in twitBase.scan_users():
print user
twitBase.close()
if __name__ == '__main__':
main()