更新时间:2023-04-28 GMT+08:00
普通查询
功能介绍
CloudTable标签索引基于Lucene语法,提供了自研的查询接口LemonTable.query(LemonQuery query)。
样例代码
public void testNormalQuery() { LOG.info("Entering testNormalQuery."); try (Table table = conn.getTable(tableName)) { // Using Table instance to create LemonTable. LemonTable lemonTable = new LemonTable(table); // Build LemonQuery. LemonQuery query = LemonQuery.builder() // Set ad-hoc query condition. .setQuery("education:bachelor OR education:master") //(1) // Set how many rows should be cached on client for the initial request. .setCaching(10) //(2) // Set return column family/columns. .addFamily(FAM_M) .addColumn(FAM_N, QUA_N) //(3) // Set return return result just contains rowkeys, no any qualifier // the CF of LemonConstants.EMPTY_COLUMN_RETURN can be a random existing CF //.addColumn(FAM_M, LemonConstants.EMPTY_COLUMN_RETURN) .build(); ResultSet resultSet = lemonTable.query(query); // Read result rows. int count = resultSet.getCount(); LOG.info("the entity count of query is " + count); List<EntityEntry> entries = resultSet.listRows(); for (EntityEntry entry : entries) { Map<String, Map<String, String>> fams = entry.getColumns(); for (Map.Entry<String, Map<String, String>> familyEntry : fams.entrySet()) { String family = familyEntry.getKey(); Map<String, String> qualifiers = familyEntry.getValue(); for (Map.Entry<String, String> qualifier : qualifiers.entrySet()) { String Qua = qualifier.getKey(); String value = qualifier.getValue(); LOG.info("rowkey is " + Bytes.toString(entry.getRow()) + ", qualifier is " + family + ":" + Qua + ", value is " + value); } } } } catch (IOException e) { LOG.error("testNormalQuery failed ", e); } LOG.info("Exiting testNormalQuery."); }
注意事项
(1) 查询条件,遵循Lucene语法/BNF范式,例如:
"Male ANDMarried AND AGE:25-30 AND BLOOD_TYPE:A" "Male ANDMarried AND (AGE:25-30 OR AGE:30-35)AND BLOOD_TYPE:A"
(2) 查询返回多少行数据。
(3) 每行数据返回哪些qualifier,如果只设置列族,则表示返回列族下的所有列。
父主题: 样例代码说明