1- #!/usr/bin/python
1+ #!/usr/bin/env python
22'''
33 Licensed to the Apache Software Foundation (ASF) under one
44 or more contributor license agreements. See the NOTICE file
2020import sys
2121import time
2222import os
23-
24- # Modify this import path to point to the correct location to thrift.
25- thrift_path = os .path .abspath ('/Users/sergey/Downloads/thrift/lib/py/build/lib.macosx-10.8-intel-2.7' )
26- sys .path .append (thrift_path )
27- gen_py_path = os .path .abspath ('gen-py' )
28- sys .path .append (gen_py_path )
29-
30- from thrift import Thrift
3123from thrift .transport import TSocket , TTransport
3224from thrift .protocol import TBinaryProtocol
33- from hbase . ttypes import TThriftServerType
34- from hbase .Hbase import Client , ColumnDescriptor , Mutation
25+ from gen_py . hbase import ttypes
26+ from gen_py . hbase .Hbase import Client , ColumnDescriptor , Mutation
3527
3628def printVersions (row , versions ):
37- print "row: " + row + ", values: " ,
29+ print ( "row: " + row + ", values: " , end = ' ' )
3830 for cell in versions :
39- print cell .value + "; " ,
40- print
31+ print ( cell .value + "; " , end = ' ' )
32+ print ()
4133
4234def printRow (entry ):
43- print "row: " + entry .row + ", cols:" ,
35+ print ( "row: " + entry .row + ", cols:" , end = ' ' )
4436 for k in sorted (entry .columns ):
45- print k + " => " + entry .columns [k ].value ,
46- print
37+ print ( k + " => " + entry .columns [k ].value , end = ' ' )
38+ print ()
4739
4840
4941def demo_client (host , port , is_framed_transport ):
@@ -68,22 +60,22 @@ def demo_client(host, port, is_framed_transport):
6860
6961 # Check Thrift Server Type
7062 serverType = client .getThriftServerType ()
71- if serverType != TThriftServerType .ONE :
72- raise Exception ( "Mismatch between client and server, server type is %s" % serverType )
63+ if serverType != ttypes . TThriftServerType .ONE :
64+ raise RuntimeError ( f "Mismatch between client and server, server type is { serverType } " )
7365
7466 t = "demo_table"
7567
7668 #
7769 # Scan all tables, look for the demo table and delete it.
7870 #
79- print "scanning tables..."
71+ print ( "scanning tables..." )
8072 for table in client .getTableNames ():
81- print " found: %s" % ( table )
73+ print ( f " found: { table } " )
8274 if table == t :
8375 if client .isTableEnabled (table ):
84- print " disabling table: %s" % ( t )
76+ print ( f " disabling table: { t } " )
8577 client .disableTable (table )
86- print " deleting table: %s" % ( t )
78+ print ( f " deleting table: { t } " )
8779 client .deleteTable (table )
8880
8981 columns = []
@@ -96,16 +88,16 @@ def demo_client(host, port, is_framed_transport):
9688 columns .append (col )
9789
9890 try :
99- print "creating table: %s" % ( t )
91+ print ( f "creating table: { t } " )
10092 client .createTable (t , columns )
101- except AlreadyExists , ae :
102- print "WARN: " + ae .message
93+ except ttypes . AlreadyExists as ae :
94+ print ( "WARN: " + ae .message )
10395
10496 cols = client .getColumnDescriptors (t )
105- print "column families in %s" % ( t )
97+ print ( f "column families in { t } " )
10698 for col_name in cols .keys ():
10799 col = cols [col_name ]
108- print " column: %s , maxVer: %d" % ( col .name , col . maxVersions )
100+ print ( f " column: { col . name } , maxVer: { col .maxVersions } " )
109101
110102 dummy_attributes = {}
111103 #
@@ -116,7 +108,7 @@ def demo_client(host, port, is_framed_transport):
116108
117109 # non-utf8 is fine for data
118110 mutations = [Mutation (column = "entry:foo" ,value = invalid )]
119- print str (mutations )
111+ print ( str (mutations ) )
120112 client .mutateRow (t , "foo" , mutations , dummy_attributes )
121113
122114 # try empty strings
@@ -131,25 +123,25 @@ def demo_client(host, port, is_framed_transport):
131123 try :
132124 mutations = [Mutation (column = "entry:foo" , value = invalid )]
133125 client .mutateRow (t , invalid , mutations , dummy_attributes )
134- except ttypes .IOError , e :
135- print 'expected exception: %s' % ( e .message )
126+ except ttypes .IOError as e :
127+ print ( f 'expected exception: { e .message } ' )
136128
137129 # Run a scanner on the rows we just created
138- print "Starting scanner..."
130+ print ( "Starting scanner..." )
139131 scanner = client .scannerOpen (t , "" , ["entry:" ], dummy_attributes )
140132
141133 r = client .scannerGet (scanner )
142134 while r :
143135 printRow (r [0 ])
144136 r = client .scannerGet (scanner )
145- print "Scanner finished"
137+ print ( "Scanner finished" )
146138
147139 #
148140 # Run some operations on a bunch of rows.
149141 #
150142 for e in range (100 , 0 , - 1 ):
151143 # format row keys as "00000" to "00100"
152- row = "%0.5d" % ( e )
144+ row = f" { row :05 } "
153145
154146 mutations = [Mutation (column = "unused:" , value = "DELETE_ME" )]
155147 client .mutateRow (t , row , mutations , dummy_attributes )
@@ -187,15 +179,15 @@ def demo_client(host, port, is_framed_transport):
187179 r = client .get (t , row , "entry:foo" , dummy_attributes )
188180 # just to be explicit, we get lists back, if it's empty there was no matching row.
189181 if len (r ) > 0 :
190- raise "shouldn't get here!"
182+ raise RuntimeError ( "shouldn't get here!" )
191183
192184 columnNames = []
193185 for (col , desc ) in client .getColumnDescriptors (t ).items ():
194- print "column with name: " + desc .name
195- print desc
186+ print ( "column with name: " + desc .name )
187+ print ( desc )
196188 columnNames .append (desc .name + ":" )
197189
198- print "Starting scanner..."
190+ print ( "Starting scanner..." )
199191 scanner = client .scannerOpenWithStop (t , "00020" , "00040" , columnNames , dummy_attributes )
200192
201193 r = client .scannerGet (scanner )
@@ -204,16 +196,14 @@ def demo_client(host, port, is_framed_transport):
204196 r = client .scannerGet (scanner )
205197
206198 client .scannerClose (scanner )
207- print "Scanner finished"
199+ print ( "Scanner finished" )
208200
209201 transport .close ()
210202
211203
212204if __name__ == '__main__' :
213-
214- import sys
215205 if len (sys .argv ) < 3 :
216- print 'usage: %s <host> <port>' % __file__
206+ print ( f 'usage: { __file__ } <host> <port>')
217207 sys .exit (1 )
218208
219209 host = sys .argv [1 ]
0 commit comments