Read data from a Lastpoint index

Updated at:

Use Tablestore SDK for Java to read the latest data point of each time series from a Lastpoint index by primary key range.

Prerequisites

Install the Tablestore SDK for Java and initialize a client. Lastpoint indexes require version 5.17.1 or later. We recommend that you use the latest version.

Description

A Lastpoint index can be read as a data table. Call getRange and specify a primary key range to read the latest data points in the range. For a time series table that uses the default time series identifiers, the Lastpoint index contains the _#h, _m_name, _data_source, and _tags primary key columns in this order.

public GetRangeResponse getRange(GetRangeRequest getRangeRequest) throws TableStoreException, ClientException

The following example sets the start values of the four primary key columns to INF_MIN and the end values to INF_MAX. The example then reads all data from example_lastpoint_index across multiple pages.

RangeRowQueryCriteria criteria =
        new RangeRowQueryCriteria("example_lastpoint_index");

PrimaryKey startPrimaryKey = PrimaryKeyBuilder.createPrimaryKeyBuilder()
        .addPrimaryKeyColumn("_#h", PrimaryKeyValue.INF_MIN)
        .addPrimaryKeyColumn("_m_name", PrimaryKeyValue.INF_MIN)
        .addPrimaryKeyColumn("_data_source", PrimaryKeyValue.INF_MIN)
        .addPrimaryKeyColumn("_tags", PrimaryKeyValue.INF_MIN)
        .build();
PrimaryKey endPrimaryKey = PrimaryKeyBuilder.createPrimaryKeyBuilder()
        .addPrimaryKeyColumn("_#h", PrimaryKeyValue.INF_MAX)
        .addPrimaryKeyColumn("_m_name", PrimaryKeyValue.INF_MAX)
        .addPrimaryKeyColumn("_data_source", PrimaryKeyValue.INF_MAX)
        .addPrimaryKeyColumn("_tags", PrimaryKeyValue.INF_MAX)
        .build();

criteria.setInclusiveStartPrimaryKey(startPrimaryKey);
criteria.setExclusiveEndPrimaryKey(endPrimaryKey);
criteria.setMaxVersions(1);

while (true) {
    GetRangeResponse response =
            client.getRange(new GetRangeRequest(criteria));
    response.getRows().forEach(System.out::println);

    if (response.getNextStartPrimaryKey() == null) {
        break;
    }
    criteria.setInclusiveStartPrimaryKey(
            response.getNextStartPrimaryKey());
}

Parameters

GetRangeRequest contains the following parameter.

Name

Type

Description

rangeRowQueryCriteria (required)

RangeRowQueryCriteria

The range read criteria.

Range read criteria

rangeRowQueryCriteria contains the following parameters.

Name

Type

Description

tableName (required)

String

The Lastpoint index name.

inclusiveStartPrimaryKey (required)

PrimaryKey

The inclusive start primary key. Specify all primary key columns in the order defined in the index.

exclusiveEndPrimaryKey (required)

PrimaryKey

The exclusive end primary key. Specify all primary key columns in the order defined in the index.

maxVersions (required)

int

The maximum number of versions to return for each column. A Lastpoint index retains only the latest value of each field. Set this parameter to 1.

columnsToGet (optional)

Set<String>

The attribute columns to return. If this parameter is not specified, all attribute columns are returned.

direction (optional)

Direction

The read direction. Valid values: FORWARD and BACKWARD. Default value: FORWARD.

limit (optional)

int

The maximum number of rows to return in a request. The value must be greater than 0. If this parameter is not specified, the SDK default of -1 is used.

Response

GetRangeResponse contains the following operation-specific fields.

Field

Type

Description

rows

List<Row>

Call getRows() to obtain the Lastpoint rows returned by the request.

nextStartPrimaryKey

PrimaryKey

Call getNextStartPrimaryKey() to obtain the start primary key for the next request. A value of null indicates that all data in the specified range is returned.

Lastpoint row schema

For a time series table that uses the default time series identifiers, each Lastpoint row contains the following fields.

Field

Type

Description

_#h

String

The system-generated partition key for the time series.

_m_name

String

The measurement name.

_data_source

String

The data source.

_tags

String

The time series tags.

_time

long

The timestamp of the latest data point, in microseconds.

Data fields

ColumnValue

The data fields and values in the latest data point.