更新时间:2022-12-14 GMT+08:00
支持全文索引
通过org.apache.luna.client.LunaAdmin对象的createTable方法来创建表和索引,并指定表名、列族名、索引创建请求,mapping文件所在目录路径。也可通过addCollection往已有表中添加索引。查询时通过org.apache.luna.client.LunaAdmin对象的getTable方法来获取Table对象进行scan操作。
表的列名以及列族名不能包含特殊字符,可以由字母、数字以及下划线组成。
带有全文索引的HBase表限制:
- 不支持容灾备份恢复。
- 不支持删除行/列族操作。
- Solr侧查询不支持强一致性。
代码样例片段
以下代码片段在hbase.examples包的“LunaSample”类的testFullTextScan方法中。
public static void testFullTextScan() throws Exception {
/**
* Create create request of Solr. Specify collection name, confset name,
* number of shards, and number of replication factor.
*/
Create create = new Create();
create.setCollectionName(COLLECTION_NAME);
create.setConfigName(CONFSET_NAME);
create.setNumShards(NUM_OF_SHARDS);
create.setReplicationFactor(NUM_OF_REPLICATIONFACTOR);
/**
* Create mapping. Specify index fields(mandatory) and non-index
* fields(optional).
*/
List<ColumnField> indexedFields = new ArrayList<ColumnField>();
indexedFields.add(new ColumnField("name", "f:n"));
indexedFields.add(new ColumnField("cat", "f:t"));
indexedFields.add(new ColumnField("features", "f:d"));
Mapping mapping = new Mapping(indexedFields);
/**
* Create table descriptor of HBase.
*/
HTableDescriptor desc = new HTableDescriptor(HBASE_TABLE);
desc.addFamily(new HColumnDescriptor(TABLE_FAMILY));
/**
* Create table and collection at the same time.
*/
LunaAdmin admin = null;
try {
admin = new AdminSingleton().getAdmin();
admin.deleteTable(HBASE_TABLE);
if (!admin.tableExists(HBASE_TABLE)) {
admin.createTable(desc, Bytes.toByteArrays(new String[] { "0", "1", "2", "3", "4" }),
create, mapping);
}
/**
* Put data.
*/
Table table = admin.getTable(HBASE_TABLE);
int i = 0;
while (i < 5) {
byte[] row = Bytes.toBytes(i + "+sohrowkey");
Put put = new Put(row);
put.addColumn(TABLE_FAMILY, Bytes.toBytes("n"), Bytes.toBytes("ZhangSan" + i));
put.addColumn(TABLE_FAMILY, Bytes.toBytes("t"), Bytes.toBytes("CO" + i));
put.addColumn(TABLE_FAMILY, Bytes.toBytes("d"), Bytes.toBytes("Male, Leader of M.O" + i));
table.put(put);
i++;
}
/**
* Scan table.
*/
Scan scan = new Scan();
SolrQuery query = new SolrQuery();
query.setQuery("name:ZhangSan1 AND cat:CO1");
Filter filter = new FullTextFilter(query, COLLECTION_NAME);
scan.setFilter(filter);
ResultScanner scanner = table.getScanner(scan);
LOG.info("-----------------records----------------");
for (Result r = scanner.next(); r != null; r = scanner.next()) {
for (Cell cell : r.rawCells()) {
LOG.info(Bytes.toString(CellUtil.cloneRow(cell)) + ":"
+ Bytes.toString(CellUtil.cloneFamily(cell)) + ","
+ Bytes.toString(CellUtil.cloneQualifier(cell)) + ","
+ Bytes.toString(CellUtil.cloneValue(cell)));
}
}
LOG.info("-------------------end------------------");
/**
* Delete collection.
*/
admin.deleteCollection(HBASE_TABLE, COLLECTION_NAME);
/**
* Delete table.
*/
admin.deleteTable(HBASE_TABLE);
} catch (IOException e) {
e.printStackTrace();
} finally {
/**
* When everything done, close LunaAdmin.
*/
admin.close();
}
}
注意事项
- 创建表和索引都必须不存在。
- 必须使用LunaAdmin获取Table对象进行scan操作。
父主题: 使用HBase