三分钟上手Phoenix

Phoenix简介

Phoenix定义

Phoenix是HBase的开源SQL皮肤。可以使用标准JDBC API代替HBase客户端API来创建表,插入数据和查询HBase数据。

Phoenix特点

  1. 容易集成:如SparkHivePigFlumeMap Reduce
  2. 操作简单: DML命令以及通过DDL命令创建和操作表和版本化增量更改
  3. 支持HBase二级索引[^1]创建

扩展:想要实现高效查询非RowKey字段,可以建立一张二级索引表,将RowKey与其他字段建立映射关系。

Phoenix架构

在这里插入图片描述

Phoenix快速入门

安装

  1. 官网地址

    http;//phoenix.apache.org/

  2. Phoenix部署
    注意:Hbase2.0.5与Phoenix2.0有一点小的不兼容。

    上传并解压tar包

    tar -zxvf apache-phoenix-5.0.0-HBase-2.0-bin.tar.gz -C /opt/module/
     
    mv apache-phoenix-5.0.0-HBase-2.0-bin phoenix
    
  3. 配置环境变量

    #phoenix
    export PHOENIX_HOME=/opt/module/phoenix
    export PHOENIX_CLASSPATH=$PHOENIX_HOME
    export PATH=$PATH:$PHOENIX_HOME/bin
    
  4. 复制server包(协处理器)并拷贝到各个节点的Hbase/lib

    [atguigu@hadoop102 module]$ cd /opt/module/phoenix/
    
    [atguigu@hadoop102 phoenix]$ cp /opt/module/phoenix/phoenix-5.0.0-HBase-2.0-server.jar /opt/module/hbase/lib/
    
    [atguigu@hadoop102 phoenix]$ xsync /opt/module/hbase/lib/phoenix-5.0.0-HBase-2.0-server.jar
    
  5. 重启Hbase

  6. 连接Phoenix (thick)

    [atguigu@hadoop101 phoenix]$ /opt/module/phoenix/bin/sqlline.py hadoop102,hadoop103,hadoop104:2181
    

Phoenix Shell操作

表的操作

  1. 创建schema

    create schema bigdata;
    

    :创建表之前,需要在hbase/bin/hbase-site.xmlphoenix/bin/hbase-site.xml中添加参数phoenix.schema.isNamespaceMappingEnabled

    在phoenix中,表名等会自动转换为大写,若要小写,使用双引号,如"us_population"。

  2. 使用schema

    use bigdata;
    
  3. 查看所有表

    !table!tables8
    
  4. 创建表

    CREATE TABLE IF NOT EXISTS student(
    id VARCHAR primary key,
    name VARCHAR,
    addr VARCHAR);
    
    # 指明多个列联合作为RowKey
    create table if not exists us_population(
    State char(2) not null,
    city varchar not null
    population bigint
    constraint my_pk primary key(state,city))
    

    建表时 phoenix 至Hbase中列名编码功能转换??

  5. 插入数据

    upsert into student values('1001','zhangsan','beijing');
    
  6. 查询记录

    select * from student;
    select * from student where id = '1001'
    

    : 在Hbase中查看在Phoenix中插入的数据时,会发现多一条数据。这条数据是为了插入的数据只有RowKey,其他字段为空。因为Hbase存储的是kv对,而只有RowKey插入时,需要一个value来帮助数据插入。

  7. 删除记录

    delete from student where id = '1001';
    
  8. 删除表

    delete table student;
    
  9. 退出命令行

    !quit
    

表的映射

默认情况下,直接在HBase中创建的表,通过Phoenix是查看不到的。如果要在Phoenix中操作直接在HBase中创建的表,则需要在Phoenix中进行表的映射。映射方式有两种:视图映射和表映射。

​ 首先在Hbase中创建test

[atguigu@hadoop102 ~]$ /opt/module/hbase/bin/hbase 
hbase(main):001:0> create 'test','info1','info2'
  1. 视图映射</

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值