Use Tablestore SDK for Java to read a row from a Wide Column model table by its complete primary key.
Prerequisites
Install the Tablestore SDK for Java and initialize the client.
Description
Call getRow to read a row by its complete primary key. You can specify the returned columns, data version range, and filter conditions.
public GetRowResponse getRow(GetRowRequest getRowRequest) throws TableStoreException, ClientException
Set at least one of maxVersions or timeRange. Otherwise, the server returns a parameter error.
The following example reads the row with primary key row1 from the get_row_demo table and returns only the latest version of each column.
String tableName = "get_row_demo";
PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
primaryKeyBuilder.addPrimaryKeyColumn("id", PrimaryKeyValue.fromString("row1"));
PrimaryKey primaryKey = primaryKeyBuilder.build();
SingleRowQueryCriteria criteria = new SingleRowQueryCriteria(tableName, primaryKey);
criteria.setMaxVersions(1);
GetRowResponse response = client.getRow(new GetRowRequest(criteria));
System.out.println("Row: " + response.getRow());
Parameters
GetRowRequest contains the following parameters.
|
Name |
Type |
Description |
|
rowQueryCriteria (required) |
|
The criteria for reading a row. |
|
transactionId (optional) |
|
The local transaction ID. Set this parameter only when you read data in a local transaction. |
Single-row query criteria
rowQueryCriteria is of the SingleRowQueryCriteria type and contains the following parameters.
|
Name |
Type |
Description |
|
tableName (required) |
|
The name of the table. |
|
primaryKey (required) |
|
The primary key of the row. Specify all primary key columns in the same order and with the same types as the table schema. Primary key columns support the |
|
maxVersions (optional) |
|
The maximum number of data versions to return. If more versions match, Tablestore returns versions from newest to oldest. Set at least one of |
|
timeRange (optional) |
|
The data version range. Only versions in the range are returned. Set at least one of |
|
columnsToGet (optional) |
|
The columns to return. If you do not specify this parameter, the entire row is returned. If you specify it and the row contains none of the specified columns, |
|
filter (optional) |
|
The filter condition. If you specify both |
Response
GetRowResponse contains the following operation-specific field.
|
Field |
Type |
Description |
|
|
|
The queried row, obtained by calling |
Examples
Read specific attribute columns
Use addColumnsToGet to specify the attribute column names to read. Only those columns are returned.
String tableName = "get_row_demo";
PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
primaryKeyBuilder.addPrimaryKeyColumn("id", PrimaryKeyValue.fromString("row1"));
PrimaryKey primaryKey = primaryKeyBuilder.build();
SingleRowQueryCriteria criteria = new SingleRowQueryCriteria(tableName, primaryKey);
criteria.setMaxVersions(1);
// Read only col1 and col2
criteria.addColumnsToGet("col1");
criteria.addColumnsToGet("col2");
GetRowResponse response = client.getRow(new GetRowRequest(criteria));
System.out.println("Row (selected columns): " + response.getRow());
Read by version range
Use setTimeRange to specify a version time range. Only versions within the range are returned.
String tableName = "get_row_demo";
PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
primaryKeyBuilder.addPrimaryKeyColumn("id", PrimaryKeyValue.fromString("row1"));
PrimaryKey primaryKey = primaryKeyBuilder.build();
SingleRowQueryCriteria criteria = new SingleRowQueryCriteria(tableName, primaryKey);
// Read all versions from the past day
long now = System.currentTimeMillis();
criteria.setTimeRange(new TimeRange(now - 86400 * 1000L, now));
criteria.setMaxVersions(Integer.MAX_VALUE);
GetRowResponse response = client.getRow(new GetRowRequest(criteria));
System.out.println("Row (time range): " + response.getRow());
Read with a filter
Use setFilter to set a column value filter. Tablestore returns the row only when it matches the filter condition.
String tableName = "get_row_demo";
PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
primaryKeyBuilder.addPrimaryKeyColumn("id", PrimaryKeyValue.fromString("row1"));
PrimaryKey primaryKey = primaryKeyBuilder.build();
SingleRowQueryCriteria criteria = new SingleRowQueryCriteria(tableName, primaryKey);
criteria.setMaxVersions(1);
// Return the row only when col1 equals "val1"
SingleColumnValueFilter filter = new SingleColumnValueFilter(
"col1",
SingleColumnValueFilter.CompareOperator.EQUAL,
ColumnValue.fromString("val1"));
// Skip the row when the column is missing
filter.setPassIfMissing(false);
criteria.setFilter(filter);
GetRowResponse response = client.getRow(new GetRowRequest(criteria));
System.out.println("Row (filtered): " + response.getRow());