addIndices API和addIndicesWithData API用于创建索引表,前者可创建无数据的空索引表,后者可创建拥有数据填充的索引表。
void addIndices(TableName tableName, TableIndices tableIndices)
参数名称 |
取值类型 |
描述 |
---|---|---|
tableName |
TableName |
用户数据表 |
tableIndices |
TableIndices |
将要添加的索引表 |
//创建用户表myuser,并添加数据 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); //创建索引表myindex TableIndices myindex = new TableIndices(); HIndexSpecification spec = new HIndexSpecification("myindex"); //对数据表f1列族的q1创建索引,数据类型是STRING spec.addIndexColumn(Bytes.toBytes("f1"), Bytes.toBytes("q1"), ValueType.STRING); myindex.addIndex(spec); GlobalIndexAdmin globalIndexAdmin = GlobalIndexClient.newIndexAdmin(admin); globalIndexAdmin.addIndices(myuser, myindex);
void addIndicesWithData(TableName tableName, TableIndices tableIndices)
参数名称 |
取值类型 |
描述 |
---|---|---|
tableName |
TableName |
用户数据表 |
tableIndices |
TableIndices |
将要添加的索引表 |
//创建用户表myuser,并添加数据 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)); } //创建索引表myindex TableIndices myindex = new TableIndices(); HIndexSpecification spec = new HIndexSpecification("myindex"); //对数据表f1列族的q1创建索引,数据类型是STRING spec.addIndexColumn(Bytes.toBytes("f1"), Bytes.toBytes("q1"), ValueType.STRING); myindex.addIndex(spec); GlobalIndexAdmin globalIndexAdmin = GlobalIndexClient.newIndexAdmin(admin); globalIndexAdmin.addIndicesWithData(myuser, myindex);