Phoenix 基本使用
进入Phoenix
phoenix-sqlline.py 10.248.161.18:2181:/hbase
或者进入目录下执行
cd /opt/cloudera/parcels/APACHE_PHOENIX/bin
关联Hbase原有表
- 创建Hbase表,列簇为 cf1 , cf2
create 'phoenix_hbase_test','cf1','cf2'
- 插入测试数据
put 'phoenix_hbase_test', 'key1','cf1:name','zhangsan'
put 'phoenix_hbase_test', 'key1','cf2:age','18'
put 'phoenix_hbase_test', 'key2','cf1:name','lisi'
put 'phoenix_hbase_test', 'key2','cf2:age','26'
put 'phoenix_hbase_test', 'key3','cf1:name','chenwu'
put 'phoenix_hbase_test', 'key4','cf2:age','10'
- 查看
- 创建Phoenix表关联
在Phoenix执行
create table "phoenix_hbase_test"("rowkey" varchar primary key, "cf1"."name" varchar , "cf2"."age" varchar) column_encoded_bytes=0;
或者创建视图(可以理解为视图是hive的外部表,table是内部表,删除内部表hbase表也会删除,删除视图hbase表还存在)
create view "phoenix_hbase_test"("rowkey" varchar primary key, "cf1"."name" varchar , "cf2"."age" varchar) ;
注意:
这里这里在Phoenix后面有column_encoded_bytes=0; 意思是禁用对字段值进行压缩编码(4.10)后的新特性,如果不加会查询会无数据,具体可参考:
- 查询数据
0: jdbc:phoenix:xxxx:2181:/hbase> select * from "phoenix_hbase_test";
+---------+-----------+------+
| rowkey | name | age |
+---------+-----------+------+
| key1 | zhangsan | 18 |
| key2 | lisi | 26 |
| key3 | chenwu | |
| key4 | | 10 |
+---------+-----------+------+
4 rows selected (0.025 seconds)
- Phoenix插入数据 并查看内容
0: jdbc:phoenix:xxx:2181:/hbase> upsert into "phoenix_hbase_test"("rowkey","cf1"."name","cf2"."age") values('key5','shazi','66');
1 row affected (0.007 seconds)
0: jdbc:phoenix:xxx:2181:/hbase> select * from "phoenix_hbase_test";
+---------+-----------+------+
| rowkey | name | age |
+---------+-----------+------+
| key1 | zhangsan | 18 |
| key2 | lisi | 26 |
| key3 | chenwu | |
| key4 | | 10 |
| key5 | shazi | 66 |
+---------+-----------+------+
5 rows selected (0.021 seconds)
如果是视图的场景下,是无法upsert into ,只能通过hbase put添加数据
0: jdbc:phoenix:xxx:2181:/hbase> upsert into "phoenix_hbase_test"("rowkey","cf1"."name","cf2"."age") values('key5','shazi','66');
Error: ERROR 505 (42000): Table is read only. (state=42000,code=505)
org.apache.phoenix.schema.ReadOnlyTableException: ERROR 505 (42000): Table is read only.
at org.apache.phoenix.compile.UpsertCompiler.compile(UpsertCompiler.java:359)
at org.apache.phoenix.jdbc.PhoenixStatement$ExecutableUpsertStatement.compilePlan(PhoenixStatement.java:784)
at org.apache.phoenix.jdbc.PhoenixStatement$ExecutableUpsertStatement.compilePlan(PhoenixStatement.java:770)
at org.apache.phoenix.jdbc.PhoenixStatement$2.call(PhoenixStatement.java:401)
at org.apache.phoenix.jdbc.PhoenixStatement$2.call(PhoenixStatement.java:391)
at org.apache.phoenix.call.CallRunner.run(CallRunner.java:53)
at org.apache.phoenix.jdbc.PhoenixStatement.executeMutation(PhoenixStatement.java:389)
at org.apache.phoenix.jdbc.PhoenixStatement.executeMutation(PhoenixStatement.java:378)
at org.apache.phoenix.jdbc.PhoenixStatement.execute(PhoenixStatement.java:1825)
at sqlline.Commands.execute(Commands.java:822)
at sqlline.Commands.sql(Commands.java:732)
at sqlline.SqlLine.dispatch(SqlLine.java:813)
at sqlline.SqlLine.begin(SqlLine.java:686)
at sqlline.SqlLine.start(SqlLine.java:398)
at sqlline.SqlLine.main(SqlLine.java:291)
视图有以下几点限制
1.只有通过VIEW进行更新时,才能维护VIEW上的INDEX。通过底层TABLE或父VIEW进行的更新不会反映在索引(PHOENIX-1499)中。
2.如果主键列的主键约束以可变长度列(PHOENIX-2157)结尾,则主键列不能添加到VIEW中。
3.VIEW可以通过简单的SELECT *查询仅在单个表上定义。您不能在多个连接的表格或聚合(PHOENIX-1505,PHOENIX-1506)上创建VIEW 。
4.当一个列添加到VIEW时,新列不会自动添加到任何子视图(PHOENIX-2054)。解决方法是手动将该列添加到子视图。
5.所有列在创建时都必须投影到VIEW中(即仅支持CREATE VIEW ... AS SELECT *)。但是,请注意,在通过ALTER VIEW命令创建后,您可以在VIEW中删除从基表继承的非主键列。在未来的版本(PHOENIX-1507)中支持在SELECT子句中提供列和或表达式的子集。