8
8
9
9
from thrift .transport .TSocket import TSocket
10
10
from thrift .transport .TTransport import TBufferedTransport , TFramedTransport
11
- from thrift .protocol import TBinaryProtocol
11
+ from thrift .protocol import TBinaryProtocol , TCompactProtocol
12
12
13
13
from .hbase import Hbase
14
14
from .hbase .ttypes import ColumnDescriptor
22
22
buffered = TBufferedTransport ,
23
23
framed = TFramedTransport ,
24
24
)
25
+ THRIFT_PROTOCOLS = dict (
26
+ binary = TBinaryProtocol .TBinaryProtocolAccelerated ,
27
+ compact = TCompactProtocol .TCompactProtocol ,
28
+ )
25
29
26
30
DEFAULT_HOST = 'localhost'
27
31
DEFAULT_PORT = 9090
28
32
DEFAULT_TRANSPORT = 'buffered'
29
33
DEFAULT_COMPAT = '0.96'
34
+ DEFAULT_PROTOCOL = 'binary'
30
35
31
36
32
37
class Connection (object ):
@@ -67,6 +72,18 @@ class Connection(object):
67
72
``-hsha``, ``-nonblocking``, and ``-threadedselector`` modes use the
68
73
framed transport.
69
74
75
+ The optional `protocol` argument specifies the Thrift transport
76
+ protocol to use. Supported values for this argument are ``binary``
77
+ (the default) and ``compact``. Make sure to choose the right one,
78
+ since otherwise you might see non-obvious connection errors or
79
+ program hangs when making a connection. ``TCompactProtocol`` is
80
+ a more compact binary format that is typically more efficient to
81
+ process as well. ``TBinaryAccelerated`` is the default protocol that
82
+ happybase uses.
83
+
84
+ .. versionadded:: 0.9
85
+ `protocol` argument
86
+
70
87
.. versionadded:: 0.5
71
88
`timeout` argument
72
89
@@ -88,7 +105,7 @@ class Connection(object):
88
105
def __init__ (self , host = DEFAULT_HOST , port = DEFAULT_PORT , timeout = None ,
89
106
autoconnect = True , table_prefix = None ,
90
107
table_prefix_separator = '_' , compat = DEFAULT_COMPAT ,
91
- transport = DEFAULT_TRANSPORT ):
108
+ transport = DEFAULT_TRANSPORT , protocol = DEFAULT_PROTOCOL ):
92
109
93
110
if transport not in THRIFT_TRANSPORTS :
94
111
raise ValueError ("'transport' must be one of %s"
@@ -105,6 +122,10 @@ def __init__(self, host=DEFAULT_HOST, port=DEFAULT_PORT, timeout=None,
105
122
raise ValueError ("'compat' must be one of %s"
106
123
% ", " .join (COMPAT_MODES ))
107
124
125
+ if protocol not in THRIFT_PROTOCOLS :
126
+ raise ValueError ("'protocol' must be one of %s"
127
+ % ", " .join (THRIFT_PROTOCOLS ))
128
+
108
129
# Allow host and port to be None, which may be easier for
109
130
# applications wrapping a Connection instance.
110
131
self .host = host or DEFAULT_HOST
@@ -115,6 +136,7 @@ def __init__(self, host=DEFAULT_HOST, port=DEFAULT_PORT, timeout=None,
115
136
self .compat = compat
116
137
117
138
self ._transport_class = THRIFT_TRANSPORTS [transport ]
139
+ self ._protocol_class = THRIFT_PROTOCOLS [protocol ]
118
140
self ._refresh_thrift_client ()
119
141
120
142
if autoconnect :
@@ -129,7 +151,7 @@ def _refresh_thrift_client(self):
129
151
socket .setTimeout (self .timeout )
130
152
131
153
self .transport = self ._transport_class (socket )
132
- protocol = TBinaryProtocol . TBinaryProtocolAccelerated (self .transport )
154
+ protocol = self . _protocol_class (self .transport )
133
155
self .client = Hbase .Client (protocol )
134
156
135
157
def _table_name (self , name ):
0 commit comments