-
Notifications
You must be signed in to change notification settings - Fork 439
/
sample.py
267 lines (223 loc) · 7.06 KB
/
sample.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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#!/usr/bin/env python
# encoding: utf-8
"""
sample of using Tera Python SDK
"""
from TeraSdk import Client, RowMutation, RowReader, TeraSdkException
from TeraSdk import MUTATION_CALLBACK, READER_CALLBACK, Status
import time
def mutation_callback(raw_mu):
""" mutation callback """
mu = RowMutation(raw_mu)
status = mu.GetStatus()
if status.GetReasonNumber() != Status.OK:
print(status.GetReasonString())
print "callback of rowkey:", mu.RowKey()
mu.Destroy()
""" 用户需要确保回调执行时,write_callback仍然有效(例如没有因为过作用域被gc掉)"""
write_callback = MUTATION_CALLBACK(mutation_callback)
def reader_callback(raw_reader):
""" reader callback """
reader = RowReader(raw_reader)
status = reader.GetStatus()
if status.GetReasonNumber() != Status.OK:
print(status.GetReasonString())
while not reader.Done():
row = reader.RowKey()
column = reader.Family() + ":" + reader.Qualifier()
timestamp = str(reader.Timestamp())
val = reader.Value()
print row + ":" + column + ":" + timestamp + ":" + val
reader.Next()
reader.Destroy()
""" 用户需要确保回调执行时,read_callback仍然有效(例如没有因为过作用域被gc掉)"""
read_callback = READER_CALLBACK(reader_callback)
def main():
""" REQUIRES:
tera.flag in current work directory;
table `oops' was created
"""
try:
client = Client("./tera.flag", "pysdk")
except TeraSdkException as e:
print(e.reason)
return
try:
table = client.OpenTable("oops")
except TeraSdkException as e:
print(e.reason)
return
# sync put
sync_put(table)
# sync get
sync_get(table)
# sync put batch
sync_put_batch(table)
# sync get batch
sync_get_batch(table)
# scan (stream)
scan(table)
# async put
async_put(table)
# async get
async_get(table)
put_get_with_timestamp(table)
table.Close()
client.Close()
print("main() done\n")
def sync_put(table):
""" sync put """
print("\nsync put")
try:
table.Put("sync", "cf0", "qu0", "value")
except TeraSdkException as e:
print(e.reason)
def sync_get(table):
""" sync get """
print("\nsync get")
try:
print(table.Get("sync", "cf0", "qu0", 0))
except TeraSdkException as e:
print(e.reason)
if "not found" in e.reason:
pass
else:
return
def sync_put_batch(table):
""" sync_put_batch """
print("\nsync put batch")
mutation_list = list()
for i in range(1, 1001):
if i == 3:
# valid rowkey should < 64K, otherwise an invalid argument
# mock error for batch put
mutation = table.NewRowMutation("k" * 64 * 1024 + "k")
else:
mutation = table.NewRowMutation(str(i))
mutation.Put("cf0", "qu0", "value" + str(i))
mutation_list.append(mutation)
table.BatchPut(mutation_list)
for m in mutation_list:
status = m.GetStatus()
if status.GetReasonNumber() != Status.OK:
print("put/write failed for key<" + m.RowKey()
+ "> due to:" + status.GetReasonString())
m.Destroy()
def sync_get_batch(table):
""" sync_get_batch """
print("\nsync get batch")
s1 = long(time.time() * 1000)
reader_list = list()
for i in range(1, 1001):
reader = table.NewRowReader(str(i))
# read column cf0:qu0
reader.AddColumn("cf0", "qu0")
reader_list.append(reader)
table.BatchGet(reader_list)
for r in reader_list:
status = r.GetStatus()
if status.GetReasonNumber() != Status.OK:
print("get/read failed for key<" + r.RowKey()
+ "> due to:" + status.GetReasonString())
else:
# print(r.Value())
pass
r.Destroy()
e1 = long(time.time() * 1000)
print(str(e1 - s1) + " ms")
def async_put(table):
""" async put """
print("\nasync put")
rowkey_list = ["async"]
for key in rowkey_list:
mu = table.NewRowMutation(key)
mu.Put("cf0", "qu0", "value")
mu.SetCallback(write_callback)
table.ApplyMutation(mu)
while not table.IsPutFinished():
time.sleep(0.01)
def async_get(table):
""" async get """
print("\nasync get")
rowkey_list = ["async", "async_not_found"]
for key in rowkey_list:
reader = table.NewRowReader(key)
reader.SetCallback(read_callback)
table.ApplyReader(reader)
while not table.IsGetFinished():
time.sleep(0.01)
def put_get_with_timestamp(table):
""" put get with timestamp """
print("\nput_get_with_timestamp")
key = "nput_get_with_timestamp"
mu = table.NewRowMutation(key)
mu.PutWithTimestamp("cf0", "qu0", 42, "value")
table.ApplyMutation(mu)
while not table.IsPutFinished():
time.sleep(0.01)
reader = table.NewRowReader(key)
reader.AddColumn("cf0", "qu0")
table.ApplyReader(reader)
while not table.IsGetFinished():
time.sleep(0.01)
status = reader.GetStatus()
if status.GetReasonNumber() != Status.OK:
print(status.GetReasonString())
return
while not reader.Done():
row = reader.RowKey()
column = reader.Family() + ":" + reader.Qualifier()
timestamp = str(reader.Timestamp())
assert reader.Timestamp() == 42
val = reader.Value()
print row + ":" + column + ":" + timestamp + ":" + val
reader.Next()
def put_get_int64(table, rowkey, cf, qu, value):
""" put/get int64 """
try:
table.PutInt64(rowkey, cf, qu, value)
print("i got:" + str(table.GetInt64(rowkey, cf, qu, 0)))
except TeraSdkException as e:
print(e.reason)
def scan_with_filter(table):
""" scan with filter """
from TeraSdk import ScanDescriptor
scan_desc = ScanDescriptor("")
scan_desc.SetBufferSize(1024 * 1024) # 1MB
if not scan_desc.SetFilter("SELECT * WHERE int64 cf0 >= 0"):
print("invalid filter")
return
try:
stream = table.Scan(scan_desc)
except TeraSdkException as e:
print(e.reason)
return
while not stream.Done():
row = stream.RowName()
column = stream.ColumnName()
timestamp = str(stream.Timestamp())
val = stream.ValueInt64()
print row + ":" + column + ":" + timestamp + ":" + str(val)
stream.Next()
def scan(table):
""" scan """
print("\nscan")
from TeraSdk import ScanDescriptor
scan_desc = ScanDescriptor("")
scan_desc.SetBufferSize(1024 * 1024) # 1MB
try:
stream = table.Scan(scan_desc)
except TeraSdkException as e:
print(e.reason)
return
while not stream.Done():
row = stream.RowName()
column = stream.ColumnName()
timestamp = str(stream.Timestamp())
val = stream.Value()
print row + ":" + column + ":" + timestamp + ":" + val
stream.Next()
scan_desc.Destroy()
stream.Destroy()
if __name__ == '__main__':
main()