All Products
Search
Document Center

Tablestore:Read data in a range

Last Updated:Jun 02, 2026

Use the Python SDK to read data within a specified primary key range from a Tablestore data table.

Prerequisites

Initialize a Tablestore client

Method

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):

Parameters

Parameter

Type

Description

table_name (Required)

str

The name of the data table.

inclusive_start_primary_key (Required)

List[Tuple]

The start primary key. Includes primary key column names and values.

  • The result includes this row.

  • Column count and data types must match the table schema.

  • For forward reads, must be less than the end primary key.

  • For backward reads, must be greater than the end primary key.

  • INF_MIN represents negative infinity, and INF_MAX represents positive infinity.

exclusive_end_primary_key (Required)

List[Tuple]

The end primary key. Includes primary key column names and values.

  • The result excludes this row.

  • Column count and data types must match the table schema.

  • INF_MIN represents negative infinity, and INF_MAX represents positive infinity.

direction (Required)

Direction

The read direction.

  • FORWARD: Default. Reads data in ascending primary key order.

  • BACKWARD: Reads data in descending primary key order.

max_version (Optional)

int

Maximum versions to return per column. Default: 1.

  • You must specify either max_version or time_range.

  • If matching versions exceed this value, only the newest are returned.

time_range (Optional)

Tuple

The version range to read.

  • You must specify either max_version or time_range.

  • Attribute columns can have multiple versions. Only versions within the specified range are returned.

limit (Optional)

int

Maximum rows per request. Must be greater than 0. If more rows match, the response returns up to this many rows and a start primary key for retrieving the next batch.

columns_to_get (Optional)

List[str]

Columns to return. Accepts primary key or attribute column names.

  • If omitted, all columns are returned.

  • If a row lacks the specified columns, it is returned with an empty attribute column list.

column_filter (Optional)

ColumnCondition

The filter condition. Filter.

  • When both columns_to_get and column_filter are set, Tablestore retrieves the specified columns first, then applies the filter.

transaction_id (Optional)

str

The local transaction ID. Uniquely identifies a local transaction. Local transactions.

Examples

This example reads all rows from test_table with primary key values greater than row1.

try:
    # Set the start primary key for the query.
    inclusive_start_primary_key = [('id', 'row1')]
    # Set the end primary key for the query. The end primary key is exclusive.
    exclusive_end_primary_key = [('id', INF_MAX)]

    # Call the get_range method to query data.
    consumed, next_start_primary_key, row_list, next_token = client.get_range('test_table', Direction.FORWARD,
                                                                              inclusive_start_primary_key, exclusive_end_primary_key)

    # Process the results.
    print('* Read CU Cost: %s' % consumed.read)
    print('* Write CU Cost: %s' % consumed.write)
    print('* Rows Data:')
    for row in row_list:
        print(row.primary_key, row.attribute_columns)
except Exception as e:
    print("Range get failed with error: %s" % e)

A single call returns at most 5,000 rows or 4 MB. When results exceed this limit, use the returned next_start_primary_key to paginate.

while True:
    # Call the get_range method to query data.
    consumed, next_start_primary_key, row_list, next_token = client.get_range('test_table', Direction.FORWARD,
                                                                              inclusive_start_primary_key,
                                                                              exclusive_end_primary_key)

    # Process the results.
    print('* Read CU Cost: %s' % consumed.read)
    print('* Write CU Cost: %s' % consumed.write)
    print('* Rows Count: %s' % len(row_list))
    print('* Rows Data:')
    for row in row_list:
        print(row.primary_key, row.attribute_columns)

    # Set the start primary key for the next read.
    if next_start_primary_key:
        inclusive_start_primary_key = next_start_primary_key
    else:
        break

Additional query options:

  • Filter by version range.

    # Set the version range for the query to the last 24 hours.
    time_range = (int(time.time() * 1000 - 86400 * 1000), int(time.time() * 1000))
    
    consumed, next_start_primary_key, row_list, next_token = client.get_range('test_table', Direction.FORWARD,
                                                                              inclusive_start_primary_key, exclusive_end_primary_key,
                                                                              time_range= time_range)
  • Read specific columns.

    columns_to_get = ['col1']
    
    # Call the get_range method to query data.
    consumed, next_start_primary_key, row_list, next_token = client.get_range('test_table', Direction.FORWARD,
                                                                              inclusive_start_primary_key, exclusive_end_primary_key,
                                                                              columns_to_get)
  • Limit rows per request.

    limit = 10
    
    # Call the get_range method to query data.
    consumed, next_start_primary_key, row_list, next_token = client.get_range('test_table', Direction.FORWARD,
                                                                              inclusive_start_primary_key, exclusive_end_primary_key,
                                                                              limit=limit)

References

Batch read data