All Products
Search
Document Center

Tablestore:Batch read data

更新时间:Mar 11, 2026

Use the Tablestore PHP SDK to read data from multiple tables in a single batch request.

Usage notes

A single batch read operation can retrieve up to 100 rows of data.

Prerequisites

Initialize a Tablestore client

Method

public function batchGetRow(array $request)

The $request parameter contains the following fields.

  • tables (Required) array: The information for batch reading data. This parameter includes the following fields.

    Name

    Type

    Description

    table_name (Required)

    string

    The name of the data table.

    primary_keys (Required)

    array

    The primary key information, including the primary key column names and values.

    • The data types of primary key columns can be STRING, INTEGER, or BINARY.

    • The number and types of primary keys must be the same as those of the data table.

    max_versions (Optional)

    int

    The maximum number of versions to return.

    • Set either max_versions or time_range.

    • If more versions exist than the specified maximum, the system returns the latest versions.

    time_range (Optional)

    array

    The time range of data versions.

    • Set either max_versions or time_range.

    • Each attribute column in a Tablestore table can have multiple data versions. Only data within the specified version range is returned.

    columns_to_get (Optional)

    array

    The data columns to read. These can be primary key columns or attribute columns.

    • If you do not set columns_to_get, the entire row is returned.

    • If you set columns_to_get and the row to read does not contain any of the specified data columns, the system returns null.

    column_filter (Optional)

    array

    The filter condition. For more information, see Filters.

    • If you set both columns_to_get and column_filter, the system first selects rows based on columns_to_get and then filters the data based on column_filter.

Examples

Read two rows with primary key values row1 and row2 from the test_table table:

$table = array (
    'table_name' => 'test_table',
    'max_versions' => 1,
    'primary_keys' => array (
        array ( array('id', 'row1') ),
        array ( array('id', 'row2') )
    )
);

$request = array (
    'tables' => array (
        $table
    )
);

try {
    // Call the batchGetRow method to read row data.
    $response = $client->batchGetRow ($request);

    // Process the response.
    foreach ($response['tables'] as $tableGroup) {
        echo "* Table: " . $tableGroup['table_name'] . "\n";
        foreach ($tableGroup['rows'] as $row) {
            if ($row['is_ok']) {
                if ($row['primary_key'] != null) {
                    echo "Succeeded Row: " . json_encode($row['primary_key']) . json_encode($row['attribute_columns']) . "\n";
                } else {
                    echo "Succeeded Row: This row is not exist." . "\n";
                }
            } else {
                echo "Failed Row: " . $row['error']['code'] . '. ' . $row['error']['message'] . "\n";
            }
        }
    }
} catch (Exception $e){
    echo "Batch get row failed.";
}

Configure batch read parameters as needed:

  • Read data from multiple tables. Batch read operations support retrieving data from multiple tables simultaneously. Specify a MultiRowQueryCriteria for each table.

    $table1 = array (
        'table_name' => 'orders_small',
        'max_versions' => 1,
        'primary_keys' => array (
            array (
                array('order_id', '90fb478c-1360-11f0-a34d-00163e30a2a9')
            )
        )
    );
    
    $request = array (
        'tables' => array (
            $table,
            $table1
        )
    );
  • Set a time range for the data versions to retrieve. Only data within the specified time range is returned.

    // Set the time range for the query to the last 24 hours.
    $table['time_range'] = array (
        'start_time' => intval(microtime(true) * 1000) - 86400 * 1000,
        'end_time' => intval(microtime(true) * 1000)
    );
  • Specify the attribute columns to retrieve.

    $table['columns_to_get'] = array('col2');

References

Read data in a range