文档简介:
创建二级索引
功能简介
使用Phoenix创建二级索引。
示例代码
以下代码片段在cn.chinatelecom.hbase.sample.PhoenixSample.sampleCreateIndex方法中。
/**
* Create Index
*/
public static boolean sampleCreateIndex(Configuration conf) {
log.info("Entering sampleCreateIndex.");
int result = -1;
//default assume zookeeper is stand-alone, and phoenix query server is same as zookeeper
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 = "CREATE INDEX IF NOT EXISTS IDX_PHOENIX_SAMPLE_AGE ON PHOENIX_SAMPLE (age)";
try (Connection conn = DriverManager.getConnection(url); Statement stat = conn.createStatement()) {
result = stat.executeUpdate(sql);
log.info("Create index successfully.");
} catch (Exception e) {
log.error("Create index failed.", e);
}
log.info("Exiting sampleCreateIndex.");
return result >= 0;
}
注意事项
需要先创建Phoenix表再创建二级索引。
二级索引包含全局索引和本地索引两大类,又有函数索引、覆盖索引和联合索引等多种实现。本样例仅展示创建二级索引的基本代码结构,不同索引SQL语法上的差异请查阅Phoenix官方文档。