Apache Phoenix的视图操作
一、应用场景
因为我们之前已经创建了 MOMO_CHAT:MSG 表,而且数据添加的方式都是以PUT方式原生API来添加的。故此时,我们不再需要再使用Phoenix创建新的表,而是使用Phoenix中的视图,通过视图来建立与HBase表之间的映射,从而实现数据快速查询。
二、视图介绍
我们可以在现有的HBase或Phoenix表上创建一个视图。表、列蔟和列名必须与现有元数据完全匹配,否则会出现异常。当创建视图后,就可以使用SQL查询视图,和操作Table一样。
三、语法说明
代码语言:javascript复制create view "my_hbase_table" (
rowkey varchar primary key,
列族.列名1 数据类型,
列族.列名2 数据类型,
....
)
考虑以下几个问题:
- 视图如何映射到HBase的表? 视图的名字必须是:命名空间.表名
- 视图中的列如何映射到HBase的列蔟和列? 列名必须是:列蔟.列名
- 视图中的类如何映射到HBase的ROWKEY? 指定某个列为primary key,自动映射ROWKEY
四、建立MOMO_CHAT:MSG的视图
1、参考SQL语句
-- 创建MOMO_CHAT:MSG视图
代码语言:javascript复制CREATE VIEW
IF
NOT EXISTS "MOMO_CHAT"."MSG" (
"pk" VARCHAR PRIMARY KEY, -- 指定ROWKEY映射到主键
"C1"."msg_time" VARCHAR,
"C1"."sender_nickyname" VARCHAR,
"C1"."sender_account" VARCHAR,
"C1"."sender_sex" VARCHAR,
"C1"."sender_ip" VARCHAR,
"C1"."sender_os" VARCHAR,
"C1"."sender_phone_type" VARCHAR,
"C1"."sender_network" VARCHAR,
"C1"."sender_gps" VARCHAR, "C1"."receiver_nickyname" VARCHAR,
"C1"."receiver_ip" VARCHAR,
"C1"."receiver_account" VARCHAR,
"C1"."receiver_os" VARCHAR,
"C1"."receiver_phone_type" VARCHAR,
"C1"."receiver_network" VARCHAR,
"C1"."receiver_gps" VARCHAR,
"C1"."receiver_sex" VARCHAR,
"C1"."msg_type" VARCHAR,
"C1"."distance" VARCHAR
);
2、尝试查询一条数据
代码语言:javascript复制SELECT * FROM "MOMO_CHAT"."MSG" LIMIT 1;
五、完成陌陌查询案例
1、需求
根据日期、发送人账号、接收人账号查询历史消息
2、编写SQL实现陌陌案例
代码语言:javascript复制SELECT
C1."sender_account",
C1."receiver_account",
C1."msg_time",
C1."message"
FROM
MOMO_CHAT.MSG
WHERE
substr( C1."msg_time", 0, 10 ) = '2021-07-16'
AND C1."sender_account" = '18461866438'
AND C1."receiver_account" = '13641568674';
3、使用java连接Phoenix:
代码语言:javascript复制@Override
public List<Msg> getMessage(String date, String sender, String receiver) throws Exception {
Class.forName(PhoenixDriver.class.getName());
Connection connection = DriverManager.getConnection("jdbc:phoenix:node1:2181");
PreparedStatement ps = connection.prepareStatement("SELECT * FROM MOMO_CHAT.MSG T WHERE substr("msg_time", 0, 10) = ? "
"AND T."sender_account" = ? "
"AND T."receiver_account" = ? ");
ps.setString(1, date);
ps.setString(2, sender);
ps.setString(3, receiver);
ResultSet rs = ps.executeQuery();
List<Msg> msgList = new ArrayList<>();
while(rs.next()) {
Msg msg = new Msg();
msg.setMsg_time(rs.getString("msg_time"));
msg.setSender_nickyname(rs.getString("sender_nickyname"));
msg.setSender_account(rs.getString("sender_account"));
msg.setSender_sex(rs.getString("sender_sex"));
msg.setSender_ip(rs.getString("sender_ip"));
msg.setSender_os(rs.getString("sender_os"));
msg.setSender_phone_type(rs.getString("sender_phone_type"));
msg.setSender_network(rs.getString("sender_network"));
msg.setSender_gps(rs.getString("sender_gps"));
msg.setReceiver_nickyname(rs.getString("receiver_nickyname"));
msg.setReceiver_ip(rs.getString("receiver_ip"));
msg.setReceiver_account(rs.getString("receiver_account"));
msg.setReceiver_os(rs.getString("receiver_os"));
msg.setReceiver_phone_type(rs.getString("receiver_phone_type"));
msg.setReceiver_network(rs.getString("receiver_network"));
msg.setReceiver_gps(rs.getString("receiver_gps"));
msg.setReceiver_sex(rs.getString("receiver_sex"));
msg.setMsg_type(rs.getString("msg_type"));
msg.setDistance(rs.getString("distance"));
msgList.add(msg);
}
return msgList;
}