This topic describes how to perform API operations such as createTable and scanFromIndex in the Tablestore SDK for Java.

  • Create an index when you create a base table.
    private static void createTable(SyncClient client) {
        TableMeta tableMeta = new TableMeta(TABLE_NAME);
        tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema(PRIMARY_KEY_NAME_1, PrimaryKeyType.STRING)); // Specify a primary key column for the base table.
        tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema(PRIMARY_KEY_NAME_2, PrimaryKeyType.INTEGER)); // Specify a primary key column for the base table.
        tableMeta.addDefinedColumn(new DefinedColumnSchema(DEFINED_COL_NAME_1, DefinedColumnType.STRING)); // Specify a predefined column for the base table.
        tableMeta.addDefinedColumn(new DefinedColumnSchema(DEFINED_COL_NAME_2, DefinedColumnType.INTEGER)); // Specify a predefined column for the base table.
        tableMeta.addDefinedColumn(new DefinedColumnSchema(DEFINED_COL_NAME_3, DefinedColumnType.INTEGER)); // Specify a predefined column for the base table.
    
        int timeToLive = -1; // Specify the validity period of data. -1 indicates that the data never expires.
        int maxVersions = 1; // Specify the maximum number of versions that can be saved in each column. 1 indicates that only the latest version is saved in each column.
    
        TableOptions tableOptions = new TableOptions(timeToLive, maxVersions);
    
        ArrayList<IndexMeta> indexMetas = new ArrayList<IndexMeta>();
        IndexMeta indexMeta = new IndexMeta(INDEX_NAME); // Create an index named Meta.
        indexMeta.addPrimaryKeyColumn(DEFINED_COL_NAME_1); // Specify DEFINED_COL_NAME_1 of the base table as the primary key of the index.
        indexMeta.addDefinedColumn(DEFINED_COL_NAME_2); // Specify DEFINED_COL_NAME_2 of the base table as an attribute column of the index.
        indexMetas.add(indexMeta); // Add the index to the base table.
    
        CreateTableRequest request = new CreateTableRequest(tableMeta, tableOptions, indexMetas); // Create the base table.
    
        client.createTable(request);
    }
  • Create an index on a base table.
    private static void createIndex(SyncClient client) {
        IndexMeta indexMeta = new IndexMeta(INDEX_NAME); // Create an index named Meta.
        indexMeta.addPrimaryKeyColumn(DEFINED_COL_NAME_2); // Specify DEFINED_COL_NAME_2 as the first primary key column of the index.
        indexMeta.addPrimaryKeyColumn(DEFINED_COL_NAME_1); // Specify DEFINED_COL_NAME_1 as the second primary key column of the index.
        CreateIndexRequest request = new CreateIndexRequest(TABLE_NAME, indexMeta, true); // Add the index to the base table. Specify that the existing data of the base table is included in the index.
        CreateIndexRequest request = new CreateIndexRequest(TABLE_NAME, indexMeta, false); // Add the index to the base table. Specify that the existing data of the base table is not included in the index.
        /**You can set the IncludeBaseData parameter to true to enable the synchronization of existing data in the base table after an index is created. You can then query all data by using the global secondary index.
           The amount of time required to synchronize data to the index is determined by the amount of data in the base table.
         */
        request.setIncludeBaseData(true);
        client.createIndex(request); // Create the index.
    }
    Note When you create an index on the base table, you can choose whether to include the existing data of the base table. When the IncludeBaseData parameter of CreateInexRequest is set to true, the index will include the existing data. Otherwise, the index will not include the existing data.
  • Delete an index.
    private static void deleteIndex(SyncClient client) {
        DeleteIndexRequest request = new DeleteIndexRequest(TABLE_NAME, INDEX_NAME); // Specify the name of the index that you want to delete and the name of the corresponding base table.
        client.deleteIndex(request); // Delete the index.
    }
  • Read data from an index.

    If an index includes the attribute columns to return, you can directly retrieve data from the index.

    private static void scanFromIndex(SyncClient client) {
        RangeRowQueryCriteria rangeRowQueryCriteria = new RangeRowQueryCriteria(INDEX_NAME); // Specify the index name.
        // Specify the primary key to start from.
        PrimaryKeyBuilder startPrimaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
        startPrimaryKeyBuilder.addPrimaryKeyColumn(DEFINED_COL_NAME_1, PrimaryKeyValue.INF_MIN); // Specify the minimum value for an indexed attribute column of the index.
        startPrimaryKeyBuilder.addPrimaryKeyColumn(PRIMARY_KEY_NAME_1, PrimaryKeyValue.INF_MIN); // Specify the minimum value for a primary key of the base table.
        startPrimaryKeyBuilder.addPrimaryKeyColumn(PRIMARY_KEY_NAME_2, PrimaryKeyValue.INF_MIN); // Specify the minimum value for a primary key of the base table.
        rangeRowQueryCriteria.setInclusiveStartPrimaryKey(startPrimaryKeyBuilder.build());
        // Specify the primary key to end with.
        PrimaryKeyBuilder endPrimaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
        endPrimaryKeyBuilder.addPrimaryKeyColumn(DEFINED_COL_NAME_1, PrimaryKeyValue.INF_MAX); // Specify the maximum value for an indexed attribute column of the index.
        endPrimaryKeyBuilder.addPrimaryKeyColumn(PRIMARY_KEY_NAME_1, PrimaryKeyValue.INF_MAX); // Specify the maximum value for a primary key of the base table .
        endPrimaryKeyBuilder.addPrimaryKeyColumn(PRIMARY_KEY_NAME_2, PrimaryKeyValue.INF_MAX); // Specify the maximum value for a primary key of the base table.
        rangeRowQueryCriteria.setExclusiveEndPrimaryKey(endPrimaryKeyBuilder.build());
        rangeRowQueryCriteria.setMaxVersions(1);
        System.out.println("The results returned from an index are as follows:");
        while (true) {
            GetRangeResponse getRangeResponse = client.getRange(new GetRangeRequest(rangeRowQueryCriteria));
            for (Row row : getRangeResponse.getRows()) {
                System.out.println(row);
            }
            // If the nextStartPrimaryKey value is not null, you can continue to read data from the index.
            if (getRangeResponse.getNextStartPrimaryKey() ! = null) {
                rangeRowQueryCriteria.setInclusiveStartPrimaryKey(getRangeResponse.getNextStartPrimaryKey());
            } else {
                break;
            }
        }
    }
    If an index does not include attribute columns to return, you must query the base table.
    private static void scanFromIndex(SyncClient client) {
        RangeRowQueryCriteria rangeRowQueryCriteria = new RangeRowQueryCriteria(INDEX_NAME); // Specify the index name.     // Specify the primary key to start from.
        PrimaryKeyBuilder startPrimaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
        startPrimaryKeyBuilder.addPrimaryKeyColumn(DEFINED_COL_NAME_1, PrimaryKeyValue.INF_MIN); // Specify the minimum value for an indexed attribute column of an index.
        startPrimaryKeyBuilder.addPrimaryKeyColumn(PRIMARY_KEY_NAME_1, PrimaryKeyValue.INF_MIN); // Specify the minimum value for a primary key of the base table.
        startPrimaryKeyBuilder.addPrimaryKeyColumn(PRIMARY_KEY_NAME_2, PrimaryKeyValue.INF_MIN); // Specify the minimum value for a primary key of the base table.
        rangeRowQueryCriteria.setInclusiveStartPrimaryKey(startPrimaryKeyBuilder.build());
        // Specify the primary key to end with.
        PrimaryKeyBuilder endPrimaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
        endPrimaryKeyBuilder.addPrimaryKeyColumn(DEFINED_COL_NAME_1, PrimaryKeyValue.INF_MAX); // Specify the maximum value for an indexed attribute column of the index.
        endPrimaryKeyBuilder.addPrimaryKeyColumn(PRIMARY_KEY_NAME_1, PrimaryKeyValue.INF_MAX); // Specify the maximum value for a primary key of the base table.
        endPrimaryKeyBuilder.addPrimaryKeyColumn(PRIMARY_KEY_NAME_2, PrimaryKeyValue.INF_MAX); // Specify the maximum value for a primary key of the base table.
        rangeRowQueryCriteria.setExclusiveEndPrimaryKey(endPrimaryKeyBuilder.build());
        rangeRowQueryCriteria.setMaxVersions(1);
        while (true) {
            GetRangeResponse getRangeResponse = client.getRange(new GetRangeRequest(rangeRowQueryCriteria));
            for (Row row : getRangeResponse.getRows()) {
                PrimaryKey curIndexPrimaryKey = row.getPrimaryKey();
                PrimaryKeyColumn pk1 = curIndexPrimaryKey.getPrimaryKeyColumn(PRIMARY_KEY_NAME1);
                PrimaryKeyColumn pk2 = curIndexPrimaryKey.getPrimaryKeyColumn(PRIMARY_KEY_NAME2);
                PrimaryKeyBuilder mainTablePKBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
                mainTablePKBuilder.addPrimaryKeyColumn(PRIMARY_KEY_NAME1, pk1.getValue());
                mainTablePKBuilder.addPrimaryKeyColumn(PRIMARY_KEY_NAME2, ke2.getValue());
                PrimaryKey mainTablePK = mainTablePKBuilder.build(); // Specify primary keys for the base table based on the index primary keys.
                // Query the base table.
                SingleRowQueryCriteria criteria = new SingleRowQueryCriteria(TABLE_NAME, mainTablePK);
                criteria.addColumnsToGet(DEFINED_COL_NAME3); // Read the DEFINED_COL_NAME3 attribute column from the base table.
                // Set MaxVersions to 1 to read the latest version of data.
                criteria.setMaxVersions(1);
                GetRowResponse getRowResponse = client.getRow(new GetRowRequest(criteria));
                Row mainTableRow = getRowResponse.getRow();
                System.out.println(row); 
            }
             // If the nextStartPrimaryKey value is not null, you can continue to read data from the base table.
            if (getRangeResponse.getNextStartPrimaryKey() ! = null) {
                rangeRowQueryCriteria.setInclusiveStartPrimaryKey(getRangeResponse.getNextStartPrimaryKey());
            } else {
                break;
            }
        }
    }