1. 范围读
# 设置范围读的起始主键。
inclusive_start_primary_key = [('uid', INF_MIN), ('gid', INF_MIN)]
# 设置范围读的结束主键。
exclusive_end_primary_key = [('uid', INF_MAX), ('gid', INF_MAX)]
# 查询所有列。
columns_to_get = []
# 每次最多返回90行,如果总共有100个结果,首次查询时指定limit=90,则第一次最多返回90,最少可能返回0个结果,但是next_start_primary_key不为None。
limit = 90
# 设置过滤器。
cond = CompositeColumnCondition(LogicalOperator.AND)
cond.add_sub_condition(SingleColumnCondition("address", 'China', ComparatorType.EQUAL))
cond.add_sub_condition(SingleColumnCondition("age", 50, ComparatorType.LESS_THAN))
try:
# 调用get_range接口。
consumed, next_start_primary_key, row_list, next_token = client.get_range(
table_name, Direction.FORWARD,
inclusive_start_primary_key, exclusive_end_primary_key,
columns_to_get,
limit,
column_filter=cond,
max_version=1
time_range = (1557125059000, 1557129059000) # start_time大于等于1557125059000,end_time小于1557129059000。
)
all_rows = []
all_rows.extend(row_list)
# 当next_start_primary_key不为空时,则继续读取。
while next_start_primary_key is not None:
inclusive_start_primary_key = next_start_primary_key
consumed, next_start_primary_key, row_list, next_token = client.get_range(
table_name, Direction.FORWARD,
inclusive_start_primary_key, exclusive_end_primary_key,
columns_to_get, limit,
column_filter=cond,
max_version=1
)
all_rows.extend(row_list)
# 打印主键和属性列。
for row in all_rows:
print(row.primary_key, row.attribute_columns)
print('Total rows: ', len(all_rows))
# 客户端异常,一般为参数错误或者网络异常。
except OTSClientError as e:
print
"get row failed, http_status:%d, error_message:%s" % (e.get_http_status(), e.get_error_message())
# 服务端异常,一般为参数错误或者流控错误。
except OTSServiceError as e:
print
"get row failed, http_status:%d, error_code:%s, error_message:%s, request_id:%s" % (e.get_http_status(), e.get_error_code(), e.get_error_message(), e.get_request_id())