行操作
更新时间:2023-11-22
单条写入
描述
写入一行数据。
请求参数
参数名称 | 参数类型 | 说明 |
---|---|---|
instance_name | String | 实例名称 |
table_name | String | 表的名称 |
row | Row | 期望写入的行 |
+rowkey | String | 行主键 |
+cells | List | 每个column,value对的列表 |
++column | String | 列名,命名规则满足正则[a-zA-Z_][a-za-z0-9\_]{0,254} |
++value | String | 列值 |
返回参数
无
示例
# 示例1
if __name__ == "__main__":
client = BtsClient(bts_sample_conf.config)
instance_name = b'instance1'
table_name = b'tab01'
row = Row()
row.rowkey = "row1"
cell1 = Cell("c1", "v1_1")
cell2 = Cell("c2", "v2_1")
cells = [cell1.__dict__, cell2.__dict__]
row.cells = cells
try:
response = client.put_row(instance_name, table_name, row)
print(response)
except Exception as e:
print(e)
# 示例2
if __name__ == "__main__":
client = BtsClient(bts_sample_conf.config)
instance_name = b'instance1'
table_name = b'tab01'
row = Row()
row.rowkey = "row1"
cell1 = Cell("c1", "v1_1")
cell2 = Cell("c2", "v2_1")
cells = [cell1, cell2]
row.cells = cells
try:
response = client.put_row(instance_name, table_name, row)
print(response)
except Exception as e:
print(e)
批量写入
描述
批量写入多行数据。
请求参数
参数名称 | 参数类型 | 说明 |
---|---|---|
instance_name | String | 实例名称 |
table_name | String | 表的名称 |
batch_put_row | BatchPutRowArgs | 期望写入的行 |
+rows | List | 期望写入的行列表 |
++rowkey | String | 行主键 |
++cells | List |
每个column,value对的列表 |
+++column | String | 列名,命名规则满足正则[a-zA-Z_][a-za-z0-9\_]{0,254} |
+++value | String | 列值 |
返回参数
无
示例
# 示例1
if __name__ == "__main__":
client = BtsClient(bts_sample_conf.config)
instance_name = b'instance1'
table_name = b'tab01'
batch_put_row = BatchPutRowArgs()
for i in range(1, 15):
row = Row()
row.rowkey = "row" + str(i)
for j in range(3):
col = "c" + str(j)
val = "v" + str(j) + "_1"
cell = Cell(col, val)
row.cells.append(cell.__dict__)
batch_put_row.rows.append(row.__dict__)
try:
response = client.batch_put_row(instance_name, table_name, batch_put_row)
print(response)
except Exception as e:
print(e)
# 示例2
if __name__ == "__main__":
client = BtsClient(bts_sample_conf.config)
instance_name = b'instance1'
table_name = b'tab01'
batch_put_row = BatchPutRowArgs()
for i in range(1, 15):
row = Row()
row.rowkey = "row" + str(i)
for j in range(3):
col = "c" + str(j)
val = "v" + str(j) + "_1"
cell = Cell(col, val)
row.cells.append(cell)
batch_put_row.rows.append(row)
try:
response = client.batch_put_row(instance_name, table_name, batch_put_row)
print(response)
except Exception as e:
print(e)
单条删除
描述
删除一整行数据或该行数据的部分列。
请求参数
参数名称 | 参数类型 | 说明 |
---|---|---|
instance_name | String | 实例名称 |
table_name | String | 表的名称 |
query_row_args | QueryRowArgs | 指定要删除的行 |
+rowkey | String | 主键 |
+cells | List | 待删除column列表,不写默认返回全部列 |
++column | String | 待删除的列名称 |
返回参数
无
示例
# 示例1
if __name__ == "__main__":
client = BtsClient(bts_sample_conf.config)
instance_name = b'instance1'
table_name = b'tab01'
query_row_args = QueryRowArgs()
query_row_args.rowkey = "row1"
cell1 = QueryCell()
cell1.column = "c1"
query_row_args.cells.append(cell1.__dict__)
try:
response = client.delete_row(instance_name, table_name, query_row_args)
print(response)
except Exception as e:
print(e)
# 示例2
if __name__ == "__main__":
client = BtsClient(bts_sample_conf.config)
instance_name = b'instance1'
table_name = b'tab01'
query_row_args = QueryRowArgs()
query_row_args.rowkey = "row1"
cell1 = QueryCell()
cell1.column = "c1"
query_row_args.cells.append(cell1)
try:
response = client.delete_row(instance_name, table_name, query_row_args)
print(response)
except Exception as e:
print(e)
批量删除
描述
批量删除若干行数据。
请求参数
参数名称 | 参数类型 | 说明 |
---|---|---|
instance_name | String | 实例名称 |
table_name | String | 表的名称 |
batch_query_row_args | BatchQueryRowArgs | 指定要删除的所有行 |
+rows | List | 指定要删除的行列表 |
++rowkey | String | 主键 |
++cells | List |
待删除column列表,不写默认返回全部列 |
+++column | String | 待删除的列名称 |
返回参数
无
示例
# 示例1
if __name__ == "__main__":
client = BtsClient(bts_sample_conf.config)
instance_name = b'instance1'
table_name = b'tab01'
batch_query_row_args = BatchQueryRowArgs()
query_row_args1 = QueryRowArgs()
query_row_args1.rowkey = "row1"
query_row_args1.cells.append(QueryCell("c0").__dict__)
query_row_args1.cells.append(QueryCell("c1").__dict__)
batch_query_row_args.rows.append(query_row_args1.__dict__)
query_row_args2 = QueryRowArgs()
query_row_args2.rowkey = "row2"
query_row_args2.cells.append(QueryCell("c1").__dict__)
query_row_args2.cells.append(QueryCell("c2").__dict__)
batch_query_row_args.rows.append(query_row_args2.__dict__)
try:
response = client.batch_delete_row(instance_name, table_name, batch_query_row_args)
print(response)
except Exception as e:
print(e)
# 示例2
if __name__ == "__main__":
client = BtsClient(bts_sample_conf.config)
instance_name = b'instance1'
table_name = b'tab01'
batch_query_row_args = BatchQueryRowArgs()
query_row_args1 = QueryRowArgs()
query_row_args1.rowkey = "row1"
query_row_args1.cells.append(QueryCell("c0"))
query_row_args1.cells.append(QueryCell("c1"))
batch_query_row_args.rows.append(query_row_args1)
query_row_args2 = QueryRowArgs()
query_row_args2.rowkey = "row2"
query_row_args2.cells.append(QueryCell("c1"))
query_row_args2.cells.append(QueryCell("c2"))
batch_query_row_args.rows.append(query_row_args2)
try:
response = client.batch_delete_row(instance_name, table_name, batch_query_row_args)
print(response)
except Exception as e:
print(e)
单条随机读
描述
查询一行,或一行中的某些列。
请求参数
参数名称 | 参数类型 | 说明 |
---|---|---|
instance_name | String | 实例名称 |
table_name | String | 表的名称 |
query_row_args | QueryRowArg | 指定要查询的行 |
+rowkey | String | 主键 |
+max_versions | int | 指定读取的最大版本数。设置该值后,用户可读取之前的版本到最新版本之间共maxVersions个版本的数据 |
+cells | List |
待查询column列表,不写默认返回全部列 |
++column | String | 待查询的列名称 |
返回参数
参数名称 | 参数类型 | 说明 |
---|---|---|
rowkey | String | 主键 |
cells | List |
cell列表 |
+column | String | 列名 |
+value | String | 列值 |
+timestamp | long | 时间戳 |
示例
# 示例1
if __name__ == "__main__":
client = BtsClient(bts_sample_conf.config)
instance_name = b'instance1'
table_name = b'tab01'
query_row_args = QueryRowArgs()
query_row_args.rowkey = "row1"
query_row_args.cells.append(QueryCell("c1").__dict__)
query_row_args.cells.append(QueryCell("c2").__dict__)
query_row_args.max_versions = 2
try:
response = client.get_row(instance_name, table_name, query_row_args)
if response.result is not None:
print("rowkey: " + response.result[0].rowkey)
for i in range(len(response.result[0].cells)):
print(" column: " + response.result[0].cells[i].column)
print(" value: " + response.result[0].cells[i].value)
print(" timestamp: " + str(response.result[0].cells[i].timestamp))
except Exception as e:
print(e)
# 示例2
if __name__ == "__main__":
client = BtsClient(bts_sample_conf.config)
instance_name = b'instance1'
table_name = b'tab01'
query_row_args = QueryRowArgs()
query_row_args.rowkey = "row1"
query_row_args.cells.append(QueryCell("c1"))
query_row_args.cells.append(QueryCell("c2"))
query_row_args.max_versions = 2
try:
response = client.get_row(instance_name, table_name, query_row_args)
if response.result is not None:
print("rowkey: " + response.result[0].rowkey)
for i in range(len(response.result[0].cells)):
print(" column: " + response.result[0].cells[i].column)
print(" value: " + response.result[0].cells[i].value)
print(" timestamp: " + str(response.result[0].cells[i].timestamp))
except Exception as e:
print(e)
批量随机读
描述
批量查询一行,或一行中的某些列。
请求参数
参数名称 | 参数类型 | 说明 |
---|---|---|
instance_name | String | 实例名称 |
table_name | String | 表的名称 |
batch_query_row_args | BatchQueryRowArgs | 指定要查询的所有行 |
+rows | List | 指定要查询的行列表 |
++rowkey | String | 主键 |
++max_versions | int | 指定读取的最大版本数。设置该值后,用户可读取之前的版本到最新版本之间共maxVersions个版本的数据 |
++cells | List |
待查询column列表,不写默认返回全部列 |
+++column | String | 待查询的列名称 |
返回参数
参数名称 | 参数类型 | 说明 |
---|---|---|
results | List | 结果列表 |
+rowkey | String | 主键 |
+cells | List | cell列表 |
++column | String | 列名 |
++value | String | 列值 |
++timestamp | long | 时间戳 |
示例
# 示例1
if __name__ == "__main__":
client = BtsClient(bts_sample_conf.config)
instance_name = b'instance1'
table_name = b'tab01'
batch_query_row_args = BatchQueryRowArgs()
batch_query_row_args.max_versions = 2
query_row_args1 = QueryRowArgs()
query_row_args1.rowkey = "row12"
query_row_args1.cells.append(QueryCell("c0").__dict__)
query_row_args1.cells.append(QueryCell("c1").__dict__)
batch_query_row_args.rows.append(query_row_args1.__dict__)
query_row_args2 = QueryRowArgs()
query_row_args2.rowkey = "row13"
query_row_args2.cells.append(QueryCell("c1").__dict__)
query_row_args2.cells.append(QueryCell("c2").__dict__)
batch_query_row_args.rows.append(query_row_args2.__dict__)
try:
response = client.batch_get_row(instance_name, table_name, batch_query_row_args)
if response.result is not None:
for i in range(len(response.result)):
print("rowkey: " + response.result[i].rowkey)
for j in range(len(response.result[i].cells)):
print(" column: " + response.result[i].cells[j].column)
print(" value: " + response.result[i].cells[j].value)
print(" timestamp: " + str(response.result[i].cells[j].timestamp))
except Exception as e:
print(e)
# 示例2
if __name__ == "__main__":
client = BtsClient(bts_sample_conf.config)
instance_name = b'instance1'
table_name = b'tab01'
batchQueryRowArgs1 = BatchQueryRowArgs()
batchQueryRowArgs1.max_versions = 2
queryRowArgs1 = QueryRowArgs()
queryRowArgs1.rowkey = "row2"
queryRowArgs1.cells.append(QueryCell("p1"))
batchQueryRowArgs1.rows.append(queryRowArgs1)
queryRowArgs2 = QueryRowArgs()
queryRowArgs2.rowkey = "row3"
queryRowArgs2.cells.append(QueryCell("c2"))
batchQueryRowArgs1.rows.append(queryRowArgs2)
try:
response = client.batch_get_row(instance_name, table_name, batchQueryRowArgs1)
if response.result is not None:
for i in range(len(response.result)):
print("rowkey: " + response.result[i].rowkey)
for j in range(len(response.result[i].cells)):
print(" column: " + response.result[i].cells[j].column)
print(" value: " + response.result[i].cells[j].value)
print(" timestamp: " + str(response.result[i].cells[j].timestamp))
except exception.BceError as e:
__logger.debug(e)
区间读
描述
扫描若干行数据。
请求参数
参数名称 | 参数类型 | 说明 |
---|---|---|
instance_name | String | 实例名称 |
table_name | String | 表的名称 |
scan_args | ScanArgs | 查询参数 |
+start_rowkey | String | scan的起始rowkey,默认表的第一个rowkey |
+include_start | boolean | 是否包含起始rowkey,默认包含 |
+stop_rowkey | String | scan的终止rowkey,默认表的最后一个rowkey |
+include_stop | boolean | 是否包含终止rowkey,默认不包含 |
+selector | List | 待查询的column列表,不写默认返回全部列 |
++column | String | 待查询的列名称 |
+limit | int | 限定查询行数,其值必须为整型,设为其他类型无效 |
+max_versions | int | 指定读取的最大版本数。设置该值后,用户可读取之前的版本到最新版本之间共maxVersions个版本的数据 |
返回参数
参数名称 | 参数类型 | 说明 |
---|---|---|
results | List |
结果列表 |
+rowkey | String | 主键 |
+cells | List |
cell列表 |
++column | String | 列名 |
++value | String | 列值 |
++timestamp | long | 时间戳 |
next_start_key | String | 若本次扫描未结束,返回下一次扫描的起始key,用户应用此值发起下一次scan操作 |
示例
# 示例1
if __name__ == "__main__":
client = BtsClient(bts_sample_conf.config)
instance_name = b'instance1'
table_name = b'tab01'
scan_args = ScanArgs()
scan_args.start_rowkey = "row10"
scan_args.include_start = "true"
scan_args.stop_rowkey = "row14"
scan_args.include_stop = "false"
scan_args.selector.append(QueryCell("c0").__dict__)
scan_args.selector.append(QueryCell("c1").__dict__)
scan_args.selector.append(QueryCell("c2").__dict__)
scan_args.max_versions = 2
scan_args.limit = 20
try:
response = client.scan(instance_name, table_name, scan_args)
if response.result is not None:
for i in range(len(response.result)):
print("rowkey: " + response.result[i].rowkey)
for j in range(len(response.result[i].cells)):
print(" column: " + response.result[i].cells[j].column)
print(" value: " + response.result[i].cells[j].value)
print(" timestamp: " + str(response.result[i].cells[j].timestamp))
except Exception as e:
print(e)
# 示例2
if __name__ == "__main__":
client = BtsClient(bts_sample_conf.config)
instance_name = b'instance1'
table_name = b'tab01'
scan_args = ScanArgs()
scan_args.start_rowkey = "row1"
scan_args.include_start = "true"
scan_args.stop_rowkey = "row5"
scan_args.include_stop = "false"
scan_args.selector.append(QueryCell("c0"))
scan_args.selector.append(QueryCell("c1"))
scan_args.max_versions = 2
try:
response = client.scan(instance_name, table_name, scan_args)
if response.result is not None:
for i in range(len(response.result)):
print("rowkey: " + response.result[i].rowkey)
for j in range(len(response.result[i].cells)):
print(" column: " + response.result[i].cells[j].column)
print(" value: " + response.result[i].cells[j].value)
print(" timestamp: " + str(response.result[i].cells[j].timestamp))
except Exception as e:
print(e)