Getting started with Tablestore - Data Management - GetRow

Posted by Dragoonus on Wed, 11 Dec 2019 03:11:46 +0100

GetRow Interface Overview

The GetRow interface is used to read a row of data. It is one of the most basic API s of Tablestore. Official Java, Go, Node.js, Python, PHP, C and C + + SDKs are provided.
This paper takes Java code as an example to explain GetRow interface in detail.

Basic instructions

Parameter description

Parameter name Is it necessary to fill in? Parameter description
PrimaryKey yes Primary key, all primary keys need to be filled in
ColumnsToGet no The collection of columns to be read. If not set, all columns will be read
MaxVersions MaxVersions and TimeRange must be set at least one How many versions can I read at most
TimeRange MaxVersions and TimeRange must be set at least one Version range to read
Filter no Filter, filter the read results on the server

Java code example

public void getRow() {
    //Construct primary key. All primary key columns must be specified
    PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
    primaryKeyBuilder.addPrimaryKeyColumn(PK1, PrimaryKeyValue.fromLong(1L));
    primaryKeyBuilder.addPrimaryKeyColumn(PK2, PrimaryKeyValue.fromString("string"));
    PrimaryKey primaryKey = primaryKeyBuilder.build();

    SingleRowQueryCriteria criteria = new SingleRowQueryCriteria(TABLE_NAME, primaryKey);
    //Set the number of versions read to 1, that is, read the latest version
    criteria.setMaxVersions(1);

    GetRowRequest getRowRequest = new GetRowRequest(criteria);
    GetRowResponse getRowResponse = syncClient.getRow(getRowRequest);
    Row row = getRowResponse.getRow();

    System.out.println("Finished reading lines, The result is: ");
    System.out.println(row);
}

Multi version data reading

Table storage supports multi version data storage, please refer to developer's guide.

1.MaxVersions

Specify maxVersions to return the latest versions. For example: a column in a row has 20 historical versions. Set maxVersions to 10, and the latest 10 versions are returned.

SingleRowQueryCriteria criteria = new SingleRowQueryCriteria(TABLE_NAME, primaryKey);
// Set read latest version
criteria.setMaxVersions(10);

2.TimeRange

Each version has a corresponding version number. This version number is a millisecond time stamp by default. You can also specify the version number yourself. When reading it out, you can take the value according to the time range and return the version column within the range.

SingleRowQueryCriteria criteria = new SingleRowQueryCriteria(TABLE_NAME, primaryKey);
TimeRange timeRange = new TimeRange(1418380771, 1418390771);
criteria.setTimeRange(timeRange);

Wide line read

There are many rows of data columns, which we call wide rows. Because there are so many data columns, we can't read them all at once because of the influence of network bandwidth, delay and other factors. Here are some reading methods of wide rows.

1. Use ColumsToGet to read the specified column

The ColumsToGet parameter, which allows the user to specify which columns to read.

SingleRowQueryCriteria criteria = new SingleRowQueryCriteria(TABLE_NAME, primaryKey);
// Set to read some columns
criteria.addColumnsToGet(new String[] {"Col1","Col2","Col3"});

2. Use startColumn and endColumn to read a certain range of property columns

Start column and end column specify a range of columns in a wide row, and sort and compare the columns according to dictionary order.

SingleRowQueryCriteria criteria = new SingleRowQueryCriteria(TABLE_NAME, primaryKey);
criteria.setStartColumn("col0");
criteria.setEndColumn("col3");

3. Use ColumnPaginationFilter with startColumn to page

ColumnPaginationFilter has two parameters, limit and offset. When reading with ColumnPaginationFilter, it will skip offset property columns, and then read limit property columns. When used with startColumn, it will start from startColumn, skip offset property columns, and read limit property columns. This method is suitable for pagination reading of attribute columns.

SingleRowQueryCriteria criteria = new SingleRowQueryCriteria(TABLE_NAME, primaryKey);
// Set read from Column0
criteria.setStartColumn("Column0");
// Use ColumnPaginationFilter to set the number of columns read at one time, limit=10, offset=0
criteria.setFilter(new ColumnPaginationFilter(10, 0));

Using filters

The filter conditions of the table storage filter support arithmetic operation (=,! =, >, > =, < AND < =) AND logical operation (NOT, AND, OR). The combination of up to 10 conditions is supported. Please refer to: Filter instructions.

1. Single condition filter: SingleColumnValueFilter

The data in the table storage is in the form of sparse matrix, and the columns of each row may be different. For rows without a column, you can use the passifmissing parameter to set the desired filtering form. If passifmissing is True, it means that if this column does not exist, it will return. If false, it means that this column does not exist, it will not return.

// Set the filter to return the row when Col0's value is 0.
SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter("Col0",
        SingleColumnValueFilter.CompareOperator.EQUAL, ColumnValue.fromLong(0));
// If Col0 does not exist, it will not return.
singleColumnValueFilter.setPassIfMissing(false);

2. Multi condition filter: CompositeColumnValueFilter

Multi condition filter can achieve the effect of multi condition combination filter by combining multiple arithmetic operation symbols.

// composite1 is (Col0 = = 0) and (col1 > 100)
CompositeColumnValueFilter composite1 = new CompositeColumnValueFilter(CompositeColumnValueFilter.LogicOperator.AND);
SingleColumnValueFilter single1 = new SingleColumnValueFilter("Col0",
      SingleColumnValueFilter.CompareOperator.EQUAL, ColumnValue.fromLong(0));
SingleColumnValueFilter single2 = new SingleColumnValueFilter("Col1",
      SingleColumnValueFilter.CompareOperator.GREATER_THAN, ColumnValue.fromLong(100));
composite1.addFilter(single1);
composite1.addFilter(single2);

// composite2 condition is ((Col0 = = 0) and (col1 > 100)) or (col2 < = 10)
CompositeColumnValueFilter composite2 = new CompositeColumnValueFilter(CompositeColumnValueFilter.LogicOperator.OR);
SingleColumnValueFilter single3 = new SingleColumnValueFilter("Col2",
      SingleColumnValueFilter.CompareOperator.LESS_EQUAL, ColumnValue.fromLong(10));
composite2.addFilter(composite1);
composite2.addFilter(single3);

Topics: Java Python PHP network