Read a range of rows
Use Tablestore SDK for Python to read contiguous rows in a primary key range and optionally specify the direction, pagination, data versions, or a filter.
Prerequisites
Install the Tablestore SDK for Python and initialize a client.
Function description
Call get_range to read rows in a primary key range. The start primary key is included, and the end primary key is excluded.
def get_range(
self,
table_name,
direction,
inclusive_start_primary_key,
exclusive_end_primary_key,
columns_to_get=None,
limit=None,
column_filter=None,
max_version=1,
time_range=None,
start_column=None,
end_column=None,
token=None,
transaction_id=None,
)
The following example reads all rows in the device partition of example_table in the forward direction. Only the latest version of each attribute column is returned.
start_primary_key = [("partition", "device"), ("id", INF_MIN)]
end_primary_key = [("partition", "device"), ("id", INF_MAX)]
consumed, next_start_primary_key, rows, next_token = client.get_range(
"example_table",
Direction.FORWARD,
start_primary_key,
end_primary_key,
)
for row in rows:
print(row.primary_key, row.attribute_columns)
A single range read returns up to 5,000 rows or 4 MB of data. If the limit is reached, use next_start_primary_key in the response to continue reading.
Parameters
The get_range method contains the following parameters.
|
Name |
Type |
Description |
|
table_name (required) |
|
The name of the table. |
|
direction (required) |
|
The read direction. |
|
inclusive_start_primary_key (required) |
|
The start primary key, which is included in the result. It must be less than the end primary key for a forward read and greater than the end primary key for a backward read. Its schema must match that of the table. |
|
exclusive_end_primary_key (required) |
|
The end primary key, which is excluded from the result. Its schema must match that of the table. |
|
columns_to_get (optional) |
|
The primary key or attribute columns to return. If this parameter is omitted, entire rows are returned. |
|
limit (optional) |
|
The maximum number of rows to return in one request. The value must be greater than |
|
column_filter (optional) |
|
The filter. For more information, see Use filters. |
|
max_version (optional) |
|
The maximum number of versions to return for each attribute column. Default value: |
|
time_range (optional) |
|
The data version range in milliseconds. The start is inclusive, and the end is exclusive. This parameter and |
|
start_column (optional) |
|
The start attribute column for reading a wide row. |
|
end_column (optional) |
|
The end attribute column for reading a wide row. |
|
token (optional) |
|
The start position for the next page of a wide row. Use the |
|
transaction_id (optional) |
|
The local transaction ID. Specify this parameter only for reads in a local transaction. For more information, see Use local transactions. |
Response
get_range returns the following values.
|
Field |
Type |
Description |
|
consumed |
|
The read and write CUs consumed by the operation. |
|
next_start_primary_key |
|
The start primary key for the next page. A value of |
|
rows |
|
The rows that meet the conditions. |
|
next_token |
|
The start position for the next page of a wide row. A value of |
Examples
Read all pages
The following example repeatedly uses next_start_primary_key until all rows in the primary key range are read.
start_primary_key = [("partition", "device"), ("id", INF_MIN)]
end_primary_key = [("partition", "device"), ("id", INF_MAX)]
while start_primary_key:
consumed, start_primary_key, rows, next_token = client.get_range(
"example_table",
Direction.FORWARD,
start_primary_key,
end_primary_key,
limit=100,
)
for row in rows:
print(row.primary_key, row.attribute_columns)
Read in the backward direction
For a backward read, the start primary key must be greater than the end primary key.
start_primary_key = [("partition", "device"), ("id", INF_MAX)]
end_primary_key = [("partition", "device"), ("id", INF_MIN)]
consumed, next_start_primary_key, rows, next_token = client.get_range(
"example_table",
Direction.BACKWARD,
start_primary_key,
end_primary_key,
)
Filter rows
The following example returns only rows whose latest value in the status attribute column is online.
column_filter = SingleColumnCondition(
"status",
"online",
ComparatorType.EQUAL,
pass_if_missing=False,
)
consumed, next_start_primary_key, rows, next_token = client.get_range(
"example_table",
Direction.FORWARD,
start_primary_key,
end_primary_key,
column_filter=column_filter,
)