HBase详解(4)

Phoenix

安装(续)

  1. 在HBase中添加配置,是的Phoenix支持二级索引

    # 进入HBase的安装目录下
    cd /opt/software/hbase-2.5.5/conf/
    # 编辑文件
    vim hbase-site.xml

    在文件中添加

    <property>
         <name>hbase.regionserver.wal.codec</name>
         <value>org.apache.hadoop.hbase.regionserver.wal.IndexedWALEditCodec</value>
    </property>
    <property>
        <name>hbase.region.server.rpc.scheduler.factory.class</name>
        <value>org.apache.hadoop.hbase.ipc.PhoenixRpcSchedulerFactory</value>
    </property>
    <property>
        <name>hbase.rpc.controller.factory.class</name>
        <value>org.apache.hadoop.hbase.ipc.controller.ServerRpcControllerFactory</value>
    </property>
  2. 远程分发

    scp hbase-site.xml root@hadoop02:$PWD
    scp hbase-site.xml root@hadoop03:$PWD
  3. 启动:Zookeeper -> HDFS -> HBase,启动Phoenix

    sqlline.py hadoop01:2181

操作

概述

  1. Phoenix将类SQL在底层转化为HBase API操作来执行,所以Phoenix中的每一个字段对应了HBase的命令

  2. 在Phoenix中,建表的时候需要指定一个或者多个字段作为主键,主键被映射成HBase的行键

  3. Phoenix在建表的时候,如果没有指定列族,那么这些列会默认被放入0列族

  4. 注意:Phoenix会自动的将表明、列名等转化为大写形式,如果要保持小写形式,那么需要使用双引号来标记

SQL

  1. 查看所有的表

    show tables;
    -- 或者
    !table
    -- 或者
    !tables
  2. 创建表

    create table employers(
        id varchar primary key,
        name varchar,
        age integer,
        gender varchar
    );
    ​
    -- 保持小写
    create table "department"(
        "id" varchar primary key,
        "dept_name" varchar
    );
  3. 多个字段作为联合主键。注意:

    1. 如果需要将多个字段来作为联合主键,那么字段的声明顺序和主键的定义顺序必须相同

    2. 多字段作为联合主键的时候,要求字段类型必须一致

    3. 多字段的值不能为空

    create table students(
        grade integer not null,
        class integer not null,
        id integer not null,
        name varchar,
        age integer,
        gender varchar    
        constraint s_pk primary key(grade, class, id)
    );
  4. 需要注意的是,Phoenix为了减少对磁盘空间的占用,默认会对每一列的数据进行编码处理。如果不需要进行编码处理,那么在建表的时候可以在尾部添加

    column_encoded_bytes=0
  5. 建表的时候需要指定的列族

    create table users(
        id varchar primary key,
        basic.username varchar,
        basic.phone varchar,
        basic.password varchar,
        info.address varchar,
        info.age integer
    );
  6. 插入数据

    upsert into employers values('e0001', 'Danny', 25, 'male');
  7. 查询数据

    select * from employers;
  8. 删除数据

    delete from employers where id = 'e0001';
  9. 删除表

    drop table employers;
  10. 退出命令行

    !quit

JDBC操作

Thick Client
  1. Thick Client(胖客户端)是将Phoenix的所有的功能都集成到了客户端,因此提供的功能比较齐全,缺点是客户端打成jar包体积较大

  2. API操作

    package com.fesco.phoenix;
    ​
    import java.sql.*;
    ​
    // 胖客户端
    public class ThickClientDemo {
    ​
        public static void main(String[] args) throws SQLException {
    ​
            // 获取连接
            Connection con = DriverManager.getConnection("jdbc:phoenix:hadoop01:2181");
            // 获取表述
            Statement stat = con.createStatement();
            // 查询
            ResultSet rs = stat.executeQuery("select * from students");
            while(rs.next()){
                String name = rs.getString("name");
                System.out.println(name);
            }
            // 关闭连接
            rs.close();
            stat.close();
            con.close();
    ​
        }
    ​
    }
Thin Client
  1. Thin Client(瘦客户端)将Phoenix的功能进行了拆解,大部分的功能基本上由Phoenix服务器来承担,客户端只负责发送请求获取数据

  2. 从Phoenix5.X开始,Phoenix安装包中默认不提供对瘦客户端的支持,所以需要额外安装PhoenixQueryServer来增加支持

  3. 安装PhoenixQueryServer

    # 进入预安装目录
    cd /opt/presoftware/
    # 上传或者下载安装包
    # 解压
    tar -xvf phoenix-queryserver-6.0.0-bin.tar.gz -C ../software/
    # 进入安装路径
    cd /opt/software/phoenix-queryserver-6.0.0/
    # 将Phoenix的连接jar包复制到当前路径下
    cp /opt/software/phoenix-5.1.3/phoenix-client-hbase-2.5-5.1.3.jar ./
    # 启动QueryServer
    bin/queryserver.py start

二级索引

概述

  1. 在HBase中,查询数据的时候,需要指定行键,通过行键来锁定数据

    get 'tableName', 'rowKey', 'cf:c'

    但是Phoenix将命令包装成了SQL形式,那么也就意味着必然会出现不通过主键来查询的情况,例如:

    -- 通过主键查询,对应到HBase上是有行键的
    select age from person where id = 'p001';
    -- 没有通过主键来查询指定数据
    select age from person name = 'tom';

    这个SQL在HBase上是先对全表来进行扫描(scan),然后扫描完成之后进行的过滤,因此效率会非常低

  2. Phoenix针对这种情况提供了二级索引:针对非主键列建立索引。那么建立索引之后,能够在不使用主键的前提下,来提升查询效率

全局索引(Global Index)

  1. 在Phoenix中,当建立索引的时候,如果不指定,那么默认建立的就是全局索引

  2. 建立全局索引的时候,在HBase中会生成一张新的表专门用于存储这个索引列,也就意味着,如果更改原始表中的数据,索引表中的数据同步更改,因此全局索引更适合于读多写少的场景

  3. 基本语法

    create index 索引(表)名 on 表名(列名);
    -- 例如
    create index s_name_index on students(name);
    -- 会在HBase中生成一个索引表s_name_index,其中以name列的值作为s_name_index的行键
    create index s_name_index on students(name, gender);
    -- 会在HBase中生成一个索引表s_name_index,其中以name列和gender列拼接的值作为s_name_index的行键

包含索引(Covered Index)

  1. 基本语法

    create index 索引(表)名 on 表名(列名1) include(列名2);
    create index s_name_index on students(name) include(gender);
    -- 针对name和gender类建立索引,但是在索引表中,只把name作为行键,gender作为普通列来存储

本地索引(Local Index)

  1. 全局索引是建立一个新的索引表,本地索引不是建立索引表,而是在原表中添加一个列族作为索引列族

  2. 这种方式的优势:当需要对数据进行更新的时候,不需要跨表更新

  3. 基本语法

    create local index 索引(表)名 on 表名(列名);
    create local index s_name_index on students(name, gender);

  • 25
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BigData-缑溪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值