Phoenix是一个支持在HBase上进行SQL查询的引擎。由于HBase的语言和传统SQL有较大不同,不利于数据库开发者的迁移,因此Phoenix作为中间件,可以极大的提高数据开发者的开发效率。
Phoenix官方文档:http://phoenix.apache.org/,这是目前最权威的Phoenix文档,可惜的是没有中文版;
阿里云上的中文简介:https://help.aliyun.com/document_detail/53600.html?spm=a2c4g.11186623.6.581.EuG7gQ;
1.打开Phoenix环境
在公司的Xshell上连接测试服务器之后,输入
[root@name01-test ~]# su hdfs
[hdfs@name01-test root]$ sqlline.py data01-test
Setting property: [incremental, false]
Setting property: [isolation, TRANSACTION_READ_COMMITTED]
issuing: !connect jdbc:phoenix:data01-test none none org.apache.phoenix.jdbc.PhoenixDriver
Connecting to jdbc:phoenix:data01-test
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/opt/cloudera/parcels/phoenix-4.8.0-cdh5.8.0/phoenix-4.8.1/phoenix-4.8.1-HBase-1.1-client.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/opt/cloudera/parcels/CDH-5.15.0-1.cdh5.15.0.p0.21/jars/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
18/07/19 13:21:04 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
Connected to: Phoenix (version 4.8)
Driver: PhoenixEmbeddedDriver (version 4.8)
Autocommit status: true
Transaction isolation: TRANSACTION_READ_COMMITTED
Building list of tables and columns for tab-completion (set fastconnect to true to skip)...
92/92 (100%) Done
Done
sqlline version 1.1.9
0: jdbc:phoenix:data01-test> !table
+------------+--------------+-------------+---------------+----------+------------+----------------------------+-----------------+--------------+-----------------+---------------+---------------+-------------+
| TABLE_CAT | TABLE_SCHEM | TABLE_NAME | TABLE_TYPE | REMARKS | TYPE_NAME | SELF_REFERENCING_COL_NAME | REF_GENERATION | INDEX_STATE | IMMUTABLE_ROWS | SALT_BUCKETS | MULTI_TENANT | VIEW_STATEM |
+------------+--------------+-------------+---------------+----------+------------+----------------------------+-----------------+--------------+-----------------+---------------+---------------+-------------+
| | SYSTEM | CATALOG | SYSTEM TABLE | | | | | | false | null | false | |
| | SYSTEM | FUNCTION | SYSTEM TABLE | | | | | | false | null | false | |
| | SYSTEM | SEQUENCE | SYSTEM TABLE | | | | | | false | null | false | |
| | SYSTEM | STATS | SYSTEM TABLE | | | | | | false | null | false | |
| | | TEST | TABLE | | | | | | false | null | false | |
| | | TEST_TEST | TABLE | | | | | | false | null | false | |
+------------+--------------+-------------+---------------+----------+------------+----------------------------+-----------------+--------------+-----------------+---------------+---------------+-------------+
0: jdbc:phoenix:data01-test>
2.Phoenix常用命令
从Linux进入Phoenix环境:
su hdfs
sqlline.py data01-test
显示所有表格:!table
删除表格:drop table 表名;
3.Phoenix的数据类型
翻译自官方文档:http://phoenix.apache.org/language/datatypes.html;
INTEGER,整数,取值在-2147483648 to 2147483647,带符号四字节表示;
UNSIGNED_INT,取值在0 to 2147483647;
BIGINT,取值在-9223372036854775808 to 9223372036854775807,带符号八字节表示;
UNSIGNED_LONG,取值在0 to 9223372036854775807;
TINYINT,取值在 -128 to 127,带符号单字节;
UNSIGNED_TINYINT,取值在0 to 127;
SMALLINT,取值在-32768 to 32767,有符号两字节;
UNSIGNED_SMALLINT,取值在0 to 32767,无符号两字节;
FLOAT,取值在-3.402823466 E + 38 to 3.402823466 E + 38,有符号四字节;
UNSIGNED_FLOAT,取值0 to 3.402823466 E + 38,无符号四字节;
DOUBLE,-1.7976931348623158 E + 308 to 1.7976931348623158 E + 308;
UNSIGNED_DOUBLE,0 to 1.7976931348623158 E + 308;
DECIMAL,小数,可以指定DECIMAL(精度,比例),例如DECIMAL(10,2)的最大精度是38位,但是注意,在Phoenix中一般不给定括号中的值,直接DECIMAL就可以;
BOOLEAN,取值为TRUE/FALSE,表示1/0;
TIME,时间表征数据类型,格式为’yyyy-MM-dd hh:mm:ss’;
DATE,日期表征数据类型,格式为’yyyy-MM-dd hh:mm:ss’,其日期和时间精度可达毫秒;
TIMESTAMP,时间戳数据类型,数据类型 yyyy-MM-dd hh:mm:ss[.nnnnnnnnn];
UNSIGNED_TIME,无符号的TIME;
UNSIGNED_DATE,无符号的DATE;
UNSIGNED_TIMESTAMP,无符号的TIMESTAMP;
VARCHAR,长度可变的字符串,可用VARCHAR或VARCHAR(10),但在Phoenix中一般只用VARCHAR;
CHAR,长度固定的字符串,可用CHAR或CHAR(10),但在Phoenix中一般只用CHAR;
BINARY ,固定长度字节数列;
VARBINARY ,可变长度字节数列;
ARRAY,数组类型,仅支持一维数组;
4.语法
翻译自官方文档:http://phoenix.apache.org/language/index.html#hint;
SELECT,从一个或多个表格中获取数据。
UPSERT,没有值的话,插入值;否则的话,更新值。
UPSERT SELECT,没有值的话,插入值;否则的话,基于查询语句的结果插入行。
DELETE,删除行。
DECLARE CURSOR,
OPEN CURSOR,
FETCH NEXT,
CLOSE,
CREATE TABLE,创建表。
DROP TABLE,删除表。
CREATE FUNCTION,
DROP FUNCTION,
CREATE VIEW,
DROP VIEW,
CREATE SEQUENCE,
DROP SEQUENCE,
ALTER,
CREATE INDEX,
DROP INDEX,
ALTER INDEX,
EXPLAIN,
UPDATE STATISTICS,
CREATE SCHEMA,
USE,
DROP SCHEMA,
GRANT,
REVOKE,
Constraint,
Options,
Hint,
Scan Hint,
Cache Hint,
Index Hint,
Small Hint,
Seek To Column Hint,
Join Hint,
Serial Hint,
Column Def,
Table Ref,
Sequence Ref,
Column Ref,
Select Expression,
Select Statement,
Split Point,
Table Spec,
Aliased Table Ref,
Join Type,
Func Argument,
Class Name,