HBase支持全文索引


功能简介

通过org.apache.luna.client.LunaAdmin对象的createTable方法来创建表和索引,并指定表名、列族名、索引创建请求,mapping文件所在目录路径。也可通过addCollection往已有表中添加索引。查询时通过org.apache.luna.client.LunaAdmin对象的getTable方法来获取Table对象进行scan操作。

说明:

表的列名以及列族名不能包含特殊字符,可以由字母、数字以及下划线组成。

带有全文索引的HBase表限制:

1、不支持多实例;

2、不支持容灾备份恢复;

3、不支持删除行/列族操作;

4、Solr侧查询不支持强一致性;

5、需要将HBase表历史数据导入到Solr中需要使用hbase-indexer;

代码样例

  public int testFullTextScan() throws Exception {
    String mapping = System.getProperty("user.dir") + File.separator + "conf";
    Create create = new Create();
    create.setCollectionName("testCollection");
    create.setConfigName("confWithSchema");
    create.setNumShards(3);
    create.setReplicationFactor(2);
    HTableDescriptor desc = new HTableDescriptor(TableName.valueOf("testTable"));
    desc.addFamily(new HColumnDescriptor(Bytes.toBytes("f")));
    // 1. create table and collection
    LunaAdmin admin = new LunaAdmin(conf, "/solr");
    admin.createTable(desc, RegionSplitter.newSplitAlgoInstance(conf, RegionSplitter.HexStringSplit.class.getName()).split(10), create, mapping);

    // 2. put data.
    Table table = admin.getTable(TableName.valueOf("testTable"));
    int i = 0;
    while (i < 10) {
      byte[] row = Bytes.toBytes(i + "+sohrowkey");
      Put put = new Put(row);
      put.add(Bytes.toBytes("f"), Bytes.toBytes("s"), Bytes.toBytes("sku" + i));
      put.add(Bytes.toBytes("f"), Bytes.toBytes("n"), Bytes.toBytes("name" + i));
      put.add(Bytes.toBytes("f"), Bytes.toBytes("c"), Bytes.toBytes("cat" + i));
      put.add(Bytes.toBytes("f"), Bytes.toBytes("_"), Bytes.toBytes("_src_" + i));
      put.add(Bytes.toBytes("f"), Bytes.toBytes("o"), Bytes.toBytes("other" + i));
      table1.put(put);
      i++;
  }

  // 3. scan table.
    Scan scan = new Scan();
    SolrQuery query = new SolrQuery();
    query.setQuery("name:name1 AND sku:sku1");
    Filter filter = new FullTextFilter(query);
    scan.setFilter(filter);
    ResultScanner scanner = table1.getScanner(scan);
    Result result = null;
    while (null != (result = scanner.next())) {
      System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("f"), Bytes.toBytes("n"))));
    }

    // 4. delete collection.
    admin.deleteCollection(TableName.valueOf("testTable"), "testCollection");

    // 5. delete table.
    admin.deleteTable(TableName.valueOf("testTable"));
  }

解释

(1)创建索引请求

(2)创建表描述符

(3)获取LunaAdmin对象,LunaAdmin提供了建表和索引、添加索引、检查表是否存在、检查索引是否存在、删除索引和删除表等功能。

(4)调用LunaAdmin的建表方法。

(5)往表中插入数据。

(6)构造全文索引条件,设置FullTextFilter,进行查询。

(7)删除索引。

(8)删除表。

注意事项

  • 创建表和索引都必须不存在。
  • 创建表和索引时需指定mapping文件目录路径。
  • 必须使用LunaAdmin获取Table对象进行scan操作。
  • mapping文件样例格式:
    <?xml version="1.0"?>
    <mapping table="t1">
    <index>
    <field name="sku" column="f:sk"/>
    <field name="name" column="f:na"/>
    <field name="cat" column="f:ca"/>
    </index>
    <non-index>
    <field name="_src_" column="f:src"/>
    </non-index>
    </mapping>
    

results matching ""

    No results matching ""