Rate This Document
Findability
Accuracy
Completeness
Readability

Creating an Index

The addIndices API and addIndicesWithData API are used to create index tables. The addIndices API is used to create index tables without data, and the addIndicesWithData API is used to create index tables with data.

addIndices API

  • API
    void addIndices(TableName tableName, TableIndices tableIndices)
  • Function description

    Clients call this API to create an index table. The new index table is empty and no data is filled.

  • API description
    1. Package name: package com.huawei.boostkit.hindex
    2. Class name: GlobalIndexAdmin
    3. Method name: addIndices
    4. Parameter description:

      Parameter

      Value

      Description

      tableName

      TableName

      User data table.

      tableIndices

      TableIndices

      Index table to be added.

  • Example
    // Create a user table myuser and add data.
    Admin admin = connection.getAdmin();
    TableName myuser = TableName.valueOf("myuser");
    HTableDescriptor hTableDescriptor = new HTableDescriptor(myuser);
    HColumnDescriptor f1 = new HColumnDescriptor("f1");
    hTableDescriptor.addFamily(f1);
    admin.createTable(hTableDescriptor);
    
    // Create an index table myindex.
    TableIndices myindex = new TableIndices();
    HIndexSpecification spec = new HIndexSpecification("myindex");
    // Create an index for q1 in the f1 column family of the data table. The data type is STRING.
    spec.addIndexColumn(Bytes.toBytes("f1"), Bytes.toBytes("q1"), ValueType.STRING);
    myindex.addIndex(spec);
    GlobalIndexAdmin globalIndexAdmin = GlobalIndexClient.newIndexAdmin(admin);
    globalIndexAdmin.addIndices(myuser, myindex);
  • Result

    The myindex index table is created for the myuser data table, and the index table contains no data.

addIndicesWithData API

  • API
    void addIndicesWithData(TableName tableName, TableIndices tableIndices)
  • Function description

    Clients call this API to create an index table and fill in data. The new index table has complete data.

  • API description
    1. Package name: package com.huawei.boostkit.hindex
    2. Class name: GlobalIndexAdmin
    3. Method name: addIndicesWithData
    4. Parameter description:

      Parameter

      Value

      Description

      tableName

      TableName

      User data table.

      tableIndices

      TableIndices

      Index table to be added.

  • Example
    // Create a user table myuser and add data.
    Admin admin = connection.getAdmin();
    TableName myuser = TableName.valueOf("myuser");
    HTableDescriptor hTableDescriptor = new HTableDescriptor(myuser);
    HColumnDescriptor f1 = new HColumnDescriptor("f1");
    hTableDescriptor.addFamily(f1);
    admin.createTable(hTableDescriptor);
    try (Table table = conn.getTable(myuser)) {
        Put put0 = new Put(Bytes.toBytes("001"));
        put0.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("q1"), Bytes.toBytes("test"));
        table.put(ImmutableList.of(put0));
    }
    
    // Create an index table myindex.
    TableIndices myindex = new TableIndices();
    HIndexSpecification spec = new HIndexSpecification("myindex");
    // Create an index for q1 in the f1 column family of the data table. The data type is STRING.
    spec.addIndexColumn(Bytes.toBytes("f1"), Bytes.toBytes("q1"), ValueType.STRING);
    myindex.addIndex(spec);
    GlobalIndexAdmin globalIndexAdmin = GlobalIndexClient.newIndexAdmin(admin);
    globalIndexAdmin.addIndicesWithData(myuser, myindex);
  • Result

    The myindex index table is created for the myuser data table, and the index table has complete data.