Adding a Secondary Index
Prerequisites
This function is available only in MRS 1.7 or later version.
Function Description
You can use the methods provided by org.apache.hadoop.hbase.hindex.client.HIndexAdmin to manage HIndexes. This class provides methods of adding an index to an existing table.
You can add an index to a table by using either of the following methods based on whether you want to build index data when adding an index:
- addIndicesWithData()
- addIndices()
Sample Code
The following code snippets are in the addIndicesExample method in the HIndexExample class of the com.huawei.bigdata.hbase.examples packet.
addIndices (): Add an index to a table without data.
public void addIndicesExample() {
LOG.info("Entering Adding a Hindex.");
// Create index instance
TableIndices tableIndices = new TableIndices();
HIndexSpecification spec = new HIndexSpecification(indexNameToAdd);
// For MRS 2.0.0 or later, you are advised to replace HColumnDescriptor with ColumnFamilyDescriptorBuilder to build an operation for adding columns as follows:
//spec.addIndexColumn(ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("info")).build(),"name",
//ValueType.STRING, null);
spec.addIndexColumn(new HColumnDescriptor("info"), "name", ValueType.STRING);
tableIndices.addIndex(spec);
Admin admin = null;
HIndexAdmin iAdmin = null;
try {
admin = conn.getAdmin();
iAdmin = HIndexClient.newHIndexAdmin(admin);
// add index to the table
iAdmin.addIndices(tableName, tableIndices);
// Alternately, add the specified indices with data
// iAdmin.addIndicesWithData(tableName, tableIndices);
LOG.info("Successfully added indices to the table " + tableName);
} catch (IOException e) {
LOG.error("Add Indices failed for table " + tableName + "." + e);
} finally {
if (iAdmin != null) {
try {
// Close the HIndexAdmin object.
iAdmin.close();
} catch (IOException e) {
LOG.error("Failed to close HIndexAdmin ", e);
}
}
if (admin != null) {
try {
// Close the Admin object.
admin.close();
} catch (IOException e) {
LOG.error("Failed to close admin ", e);
}
}
}
LOG.info("Exiting Adding a Hindex.");
} The following code snippets are in the addIndicesExampleWithData method in the HIndexExample class of the com.huawei.bigdata.hbase.examples packet.
addIndicesWithData (): Add an index to a table with a large amount of data.
public void addIndicesExampleWithData() {
LOG.info("Entering Adding a Hindex With Data.");
// Create index instance
TableIndices tableIndices = new TableIndices();
HIndexSpecification spec = new HIndexSpecification(indexNameToAdd);
// For MRS 2.0.0 or later, you are advised to replace HColumnDescriptor with ColumnFamilyDescriptorBuilder to build an operation for adding columns as follows:
//spec.addIndexColumn(ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("info")).build(),"age",
//ValueType.STRING, null);
spec.addIndexColumn(new HColumnDescriptor("info"), "age", ValueType.STRING);
tableIndices.addIndex(spec);
Admin admin = null;
HIndexAdmin iAdmin = null;
try {
admin = conn.getAdmin();
iAdmin = HIndexClient.newHIndexAdmin(admin);
// add index to the table
iAdmin.addIndicesWithData(tableName, tableIndices);
// Alternately, add the specified indices with data
// iAdmin.addIndicesWithData(tableName, tableIndices);
LOG.info("Successfully added indices to the table " + tableName);
} catch (IOException e) {
LOG.error("Add Indices failed for table " + tableName + "." + e);
} finally {
if (iAdmin != null) {
try {
// Close the HIndexAdmin object.
iAdmin.close();
} catch (IOException e) {
LOG.error("Failed to close HIndexAdmin ", e);
}
}
if (admin != null) {
try {
// Close the Admin object.
admin.close();
} catch (IOException e) {
LOG.error("Failed to close admin ", e);
}
}
}
LOG.info("Exiting Adding a Hindex With Data.");
} Last Article: Using a Filter
Next Article: Enabling/Disabling a Secondary Index
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.