Phoenix表映射

2022-04-14 00:00:00 视图 数据 字段 专区 映射

Phoenix定位为OLTP和操作型分析(operational analytics),大多用于在线业务,稳定性要求位Phoenix的功能很强大,也很灵活Phoenix SQL基于SQL-92标准,但是还是有很多方言,使用时需要特别注意。

 

基础软件版本:

Apache Hadoop:hadoop-2.8.5

Apache HBase:hbase-1.4.10

Apache Phoenix:phoenix-4.14.3-HBase-1.4-bin


Phoenix版本支持:

Phoenix Current release 4.15.0 can run on Apache HBase 1.3, 1.4 and 1.5. CDH HBase 5.11, 5.12, 5.13 and 5.14 is supported by 4.14.0. Apache HBase 2.0 is supported by 5.0.0.

 

进入hbase客户端

hbase shell

进入phoenix客户端

phoenix-4.14.3-HBase-1.4-bin/bin/sqlline.py


Phoenix视图映射

# HBase数据表仍为ZLXX:WT_TRADE_REFUND

映射到表ZLXX:WT_TRADE_REFUND的视图都先删除掉


    0: jdbc:phoenix:> drop view if exists zlxx.wt_trade_refund;
    No rows affected (0.083 seconds)
    0: jdbc:phoenix:> drop view if exists zlxx.wt_trade_refusumnd2;
    No rows affected (0.023 seconds)


    创建映射表(完美)并查询数据

    前面视图删除之后,并不会影响hbase shell创建的表里面的数据

      create table ZLXX.WT_TRADE_REFUND (
      id varchar primary key,
      info.trade_id varchar,
      info.amount varchar,
      info.status varchar,
      info.modify_time varchar
      ) column_encoded_bytes=0;

      执行效果:


        0: jdbc:phoenix:> create table ZLXX.WT_TRADE_REFUND (
        . . . . . . . . > id varchar primary key,
        . . . . . . . . > info.trade_id varchar,
        . . . . . . . . > info.amount varchar,
        . . . . . . . . > info.status varchar,
        . . . . . . . . > info.modify_time varchar
        . . . . . . . . > ) column_encoded_bytes=0;
        529 rows affected (6.181 seconds)

        查询数据试试看(limit 5):

          0: jdbc:phoenix:> select * from ZLXX.WT_TRADE_REFUND limit 5;
          +----------+------------+---------+---------+-----------------+
          | ID | TRADE_ID | AMOUNT | STATUS | MODIFY_TIME |
          +----------+------------+---------+---------+-----------------+
          | 1005528 | T12059592 | 100 | 01 | 20200228185803 |
          | 1005529 | T12059591 | 10 | 03 | 20200228185846 |
          | 1005530 | T12059606 | 10 | 01 | 20200228195442 |
          | 1005531 | T12059605 | 10 | 01 | 20200228195442 |
          | 1005532 | noTradeId | 100 | 03 | 20200229224015 |
          +----------+------------+---------+---------+-----------------+
          5 rows selected (0.052 seconds)

          使用某个字段如id进行查询(primary key):


            0: jdbc:phoenix:> select * from ZLXX.WT_TRADE_REFUND where id='2009999';
            +----------+-------------+---------+---------+-----------------+
            | ID | TRADE_ID | AMOUNT | STATUS | MODIFY_TIME |
            +----------+-------------+---------+---------+-----------------+
            | 2009999 | ZXR3099999 | 100 | 00 | 20200715124015 |
            +----------+-------------+---------+---------+-----------------+
            1 row selected (0.044 seconds)


            简单查看一下Phoenix表元数据信息:

            TABLE_TYPE为TABLE

              0: jdbc:phoenix:> !tables
              +------------+--------------+------------------+---------------+----------+------------+----------------------------+-----------------+--------------+-----------------+---------------+--+
              | TABLE_CAT | TABLE_SCHEM | TABLE_NAME | TABLE_TYPE | REMARKS | TYPE_NAME | SELF_REFERENCING_COL_NAME | REF_GENERATION | INDEX_STATE | IMMUTABLE_ROWS | SALT_BUCKETS | |
              +------------+--------------+------------------+---------------+----------+------------+----------------------------+-----------------+--------------+-----------------+---------------+--+
              | | SYSTEM | CATALOG | SYSTEM TABLE | | | | | | false | null | |
              | | SYSTEM | FUNCTION | SYSTEM TABLE | | | | | | false | null | |
              | | SYSTEM | LOG | SYSTEM TABLE | | | | | | true | 32 | |
              | | SYSTEM | SEQUENCE | SYSTEM TABLE | | | | | | false | null | |
              | | SYSTEM | STATS | SYSTEM TABLE | | | | | | false | null | |
              | | ZLXX | WT_TRADE_REFUND | TABLE | | | | | | false | null | |
              +------------+--------------+------------------+---------------+----------+------------+----------------------------+-----------------+--------------+-----------------+---------------+--+

               

              HBase表里更新一条数据试试看Phoenix有没有及时更新

              走hbase shell客户端put一条新数据

              走Phoenix这边使用刚刚put的数据id查一查


              # 需要注意的问题,可以使用to_number,性能需要额外测试


              上面映射的时候所有字段都是字符串,也就是varchar

              显然在进行sum等聚合计算的时候,非数字类型会报错。


                0: jdbc:phoenix:> select sum(amount) from zlxx.wt_trade_refund;
                Error: ERROR 203 (22005): Type mismatch. expected: [DECIMAL] but was: VARCHAR at SUM argument 1 (state=22005,code=203)
                org.apache.phoenix.schema.ArgumentTypeMismatchException: ERROR 203 (22005): Type mismatch. expected: [DECIMAL] but was: VARCHAR at SUM argument 1

                如果映射的时候,数据类型为decimal,也会有问题,就是byte编码问题会导致Phoenix这边字段值的精度有问题,比如HBase这边字段值为Phoenix这边会变成-1.02020202020201E+126

                使用to_number函数:

                  0: jdbc:phoenix:> select sum(to_number(amount)) from zlxx.wt_trade_refund;
                  +------------------------------+
                  | SUM(TO_NUMBER(INFO.AMOUNT)) |
                  +------------------------------+
                  | 11730.14 |
                  +------------------------------+
                  1 row selected (0.049 seconds)


                  # 建议好还是表创建、数据写入和数据查询都从Phoenix这个口子完成


                  附录hbase整合Phoenix的基本配置:

                    hbase-site.xml
                    <property>
                    <name>phoenix.schema.isNamespaceMappingEnabled</name>
                    <value>true</value>
                    </property>
                    <property>
                    <name>phoenix.schema.mapSystemTablesToNamespace</name>
                    <value>true</value>
                    </property>

                    【Phoenix表映射到HBase表、END】


                    往期推荐:

                    Phoenix视图映射

                    Kafka消息送达语义说明

                    Kafka基础知识总结

                    Hadoop YARN:ApplicationMaster向ResourceManager注册AM源码调试

                    Apache Hadoop YARN:Client<-->ResourceManager源码解析

                    Apache Hadoop YARN:Client<-->ResourceManager源码DEBUG

                    Hadoop YARN:ApplicationMaster与ResourceManager交互源码解析

                    Hive企业级调优

                    HiveQL查询连续三天有销售记录的店铺

                    HiveQL实战蚂蚁森林低碳用户排名分析:解法一

                    HiveQL实战蚂蚁森林低碳用户排名分析:解法二

                    HiveQL实战蚂蚁森林植物申领统计分析

                    Hive-函数

                    Hive-查询

                    Hive-DML(Data Manipulation Language)数据操作语言

                    Hive-DDL(Data Definition Language)数据定义

                    Hive优化(整理版)

                    Spark Core之Shuffle解析

                    数据仓库开发规范

                    相关文章