import tablestore
from tablestore.metadata import *
class otsclient():
def __init__(self,end_point=None,access_key_id=None,access_key_secret=None,instance_name=None):
'''
连接写进初始化方法
'''
self.conn_ost = tablestore.OTSClient(
end_point=end_point
,access_key_id=access_key_id
,access_key_secret=access_key_secret
,instance_name=instance_name
)
def ots_getrow(self,table_name=None,primary_key=None,columns_to_get=None):
'''
单行取
:return: primary_key attribute_columns
'''
consumed, return_row, next_token = self.conn_ost.get_row(
table_name=table_name
,primary_key=primary_key
,columns_to_get=columns_to_get
)
print(return_row.attribute_columns)
return return_row
def ots_getrange(self,table_name=None,inclusive_start_primary_key=None,columns_to_get=None,
exclusive_end_primary_key=None,column_filter=None):
'''
范围取
:return:
'''
consumed,next_start_primary_key, row_list, next_token = self.conn_ost.get_range(
table_name=table_name
,direction="FORWARD"
,columns_to_get=columns_to_get
, inclusive_start_primary_key=inclusive_start_primary_key
, exclusive_end_primary_key=exclusive_end_primary_key
, max_version = 1
, column_filter =column_filter
)
all_rows = []
all_rows.extend(row_list)
# 删除
for row in all_rows:
print(row.primary_key, row.attribute_columns)#查看打印主键和属性列
# row = Row(row.primary_key)
# self.conn_ost.delete_row("guild_recruit_check_infos", row, None) #删除操作
print((len(row_list))) #打印满足条件检索到的条数
return row_list
def main():
'''实例化对象'''
connect = otsclient(
#实例地址,公网私网都可
end_point="https:/********.cn-hangzhou.ots.aliyuncs.com"
, access_key_id="***************************"
, access_key_secret="**************************************"
#实例名称
, instance_name="qa-high"
)
'''
单行取
'''
primary_key = [
('pkGuildIdRegion', '785')
, ('pkGuildId', 785)
, ('sequenceId', 1586772578008000)
]
# 要获取的字段,不写为所有字段值
columns_to_get = []
connect.ots_getrow(
table_name="guild_recruit_check_infos"
, primary_key=primary_key
, columns_to_get=columns_to_get
)
'''
范围取
'''
# 要获取的字段,不写为所有字段值
columns_to_get = []
# 主键范围
inclusive_start_primary_key = [('pkGuildIdRegion', '000'), ('pkGuildId', 000), ('sequenceId', 0)]
exclusive_end_primary_key = [('pkGuildIdRegion', '999'), ('pkGuildId', 999), ('sequenceId', 9904260867611000)]
# 多列条件 设置条件为(checkStatus == 2)AND(modifiedUserId == 7816)。
column_filter = CompositeColumnCondition(LogicalOperator.AND)
column_filter.add_sub_condition(SingleColumnCondition("checkStatus", 2, ComparatorType.EQUAL))
column_filter.add_sub_condition(SingleColumnCondition("modifiedUserId", 7816, ComparatorType.EQUAL))
#调用方法
connect.ots_getrange(
#表名
table_name="guild_recruit_check_infos"
,inclusive_start_primary_key=inclusive_start_primary_key
,exclusive_end_primary_key=exclusive_end_primary_key
,columns_to_get=columns_to_get
# 条件单列 当checkStatus的值为'2'时,返回该行。
# ,column_filter = SingleColumnCondition("checkStatus", 0, ComparatorType.EQUAL, pass_if_missing = True)
# 条件多列
,column_filter = column_filter
)
if __name__ == '__main__':
main()
python 操作tablestore查询与删除
最新推荐文章于 2024-04-24 17:21:36 发布