Creating HBase Global Secondary Indexes
Function
Manage HBase global secondary indexes by calling the method in org.apache.hadoop.hbase.hindex.global.GlobalIndexAdmin. addIndices is used for creating global secondary indexes.
To create a global secondary index, you need to specify the index column, overwrite column (optional), and index table pre-partition (optional but recommended).
To create global secondary indexes on a table with existing data, you need to create an index pre-partion to prevent hotspots in an index table. The rowkey of the index table data consists of index columns and contains separators. The format is \x01Index value\x00.
\x010,\x011,\x012....
Code Sample
The following code snippets are in the addIndices method in the GlobalSecondaryIndexSample class of the com.huawei.bigdata.hbase.examples package.
In this case, an index named index_id_age is created for the user_table data table. The id and age columns in the data are used as index columns and the name column is overwritten. (The query condition is not used, but the query result needs to return to this column).
/**
* createIndex
*/
public void testCreateIndex() {
LOG.info("Entering createIndex.");
// Create index instance
TableIndices tableIndices = new TableIndices();
// Create index spec
// idx_id_age covered info:name
HIndexSpecification indexSpec = new HIndexSpecification("idx_id_age");
// Set index column
indexSpec.addIndexColumn(Bytes.toBytes("info"), Bytes.toBytes("id"), ValueType.STRING);
indexSpec.addIndexColumn(Bytes.toBytes("info"), Bytes.toBytes("age"), ValueType.STRING);
// Set covered column
// If you want cover one column, use addCoveredColumn
// If you want cover all column in one column family, use addCoveredFamilies
// If you want cover all column of all column family, use setCoveredAllColumns
indexSpec.addCoveredColumn(Bytes.toBytes("info"), Bytes.toBytes("name"));
// Need specify index table split keys, it should specify by index column.
// Note: index data's row key has a same prefix "\x01"
// For example:
// Our index column include "id" and "age", "id" is a number, we
// could specify split key like \x010 \x011 \x012...
byte[][] splitKeys = new byte[10][];
for (int i = 0; i < 10; i++) {
splitKeys[i] = Bytes.toBytesBinary("\\x01" + i);
}
indexSpec.setSplitKeys(splitKeys);
tableIndices.addIndex(indexSpec);
// iAdmin will close the inner admin instance
try (GlobalIndexAdmin iAdmin = GlobalIndexClient.newIndexAdmin(conn.getAdmin())) {
// add index to the table
iAdmin.addIndices(tableName, tableIndices);
LOG.info("Create index successfully.");
} catch (IOException e) {
LOG.error("Create index failed.", e);
}
LOG.info("Exiting createIndex.");
} What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot