文档简介:
二级索引查询
功能简介
针对添加了二级索引的用户表,其数据查询性能高于针对无二级索引用户表的数据查询。
样例:使用二级索引查找数据
以下代码片段在cn.chinatelecom.hbase.sample包的“PhoenixSample”类的
sampleScanDataByIndex方法中。
public static boolean sampleScanDataByIndex(Configuration conf) {
log.info("Entering sampleScanDataByIndex.");
String url = "jdbc:phoenix:thin:url=http://" + conf.get("hbase.zookeeper.quorum") + ":" +
conf.get("phoenix.queryserver.http.port", "8765") +
";serialization=" + conf.get("phoenix.queryserver.serialization", "PROTOBUF");
String sql = "SELECT age FROM PHOENIX_SAMPLE";
try (Connection conn = DriverManager.getConnection(url); Statement stat = conn.createStatement()) {
stat.executeQuery(sql);
log.info("Scan index successfully.");
} catch (Exception e) {
log.error("Scan index failed.", e);
return false;
}
log.info("Exiting sampleScanDataByIndex.");
return true;
}
注意事项
需要预先对字段age创建二级索引。