Phoenix + HBase,讓你像操做MySQL同樣操做HBase

Phoenix關聯HBase的操做(三種狀況)

狀況一:Hbase已經有已存在的表了,可在Phoenix中建立對應的視圖,視圖只能作查詢操做,不能作增刪改

  • hbase中已建立表且有數據,表名:phoenix

Phoenix + HBase,讓你像操做MySQL同樣操做HBase

  • 在phoenix中建立對應視圖
create view "phoenix"(
pk varchar primary key,
"info"."name" varchar,
"info"."age" varchar
);
  • 查詢視圖數據

Phoenix + HBase,讓你像操做MySQL同樣操做HBase

狀況二:Hbase已存在表,可在Phoenix中建立對應的表,對錶的操做可讀可寫,意味着能夠對Hbase的表進行插入查詢,刪除操做。

  • hbase中已存在表而且有數據
    Phoenix + HBase,讓你像操做MySQL同樣操做HBasejava

  • 在phoenix中建立對應的表
    create table "t_person"(
    id VARCHAR PRIMARY KEY,
    "f"."id" VARCHAR,
    "f"."name" VARCHAR,
    "f"."age" VARCHAR
    ) column_encoded_bytes=0;
  • 在phoenix中查詢數據
    Phoenix + HBase,讓你像操做MySQL同樣操做HBasegit

  • 在phoenix中插入數據
    Phoenix + HBase,讓你像操做MySQL同樣操做HBase

此時查看hbase中對應的t_person表數據
Phoenix + HBase,讓你像操做MySQL同樣操做HBasesql

狀況三:Hbase沒有表;可直接在Phoeinx中建立表,對應的Habse中也會自動建表,在Phoenix中對錶操做就是直接對Hbase中的表進行操做。

  • 在phoenix中直接建表,t_test表會在hbase中同步建立
    Phoenix + HBase,讓你像操做MySQL同樣操做HBaseide

  • 插入數據看phoenix和hbase中的效果
    Phoenix + HBase,讓你像操做MySQL同樣操做HBasecode

    Phoenix + HBase,讓你像操做MySQL同樣操做HBase

  • 經過Java客戶端操做phoenix
package com.fwmagic.hbase;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.sql.*;

/**
 * 經過Phoenix操做Hbase
 */
public class PhoenixQueryHbase {
    Connection connection = null;

    PreparedStatement ps = null;

    ResultSet rs = null;

    @Before
    public void init() throws Exception {
        connection = DriverManager.getConnection("jdbc:phoenix:hd1,hd2,hd3:2181");
    }

    /**
     * 建表並查詢
     *
     * @throws Exception
     */
    @Test
    public void create() throws Exception {
        Statement statement = connection.createStatement();
        statement.executeUpdate("create  table test(id integer primary key ,animal varchar )");

        //新增和更新都是一個操做:upsert
        statement.executeUpdate("upsert into test values (1,'dog')");
        statement.executeUpdate("upsert into test values (2,'cat')");
        connection.commit();

        PreparedStatement preparedStatement = connection.prepareStatement("select * from  test");
        rs = preparedStatement.executeQuery();
        while (rs.next()) {
            String id = rs.getString("id");
            String animal = rs.getString("animal");
            String format = String.format("id:%s,animal:%s", id, animal);
            System.out.println(format);
        }
    }

    /**
     * 查詢已有的表
     *
     * @throws Exception
     */
    @Test
    public void testQuery() throws Exception {
        String sql = "select * from tc";
        try {
            ps = connection.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
                String id = rs.getString("ID");
                String name = rs.getString("NAME");
                String age = rs.getString("AGE");
                String sex = rs.getString("SEX");
                String format = String.format("id:%s,name:%s,age:%s,sex:%s", id, name, age, sex);
                System.out.println(format);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (rs != null) rs.close();
            if (ps != null) ps.close();
            if (connection != null) connection.close();
        }
    }

    /**
     * 刪除數據
     *
     * @throws Exception
     */
    @Test
    public void delete() throws Exception {
        try {
            ps = connection.prepareStatement("delete from test where id=2");
            ps.executeUpdate();
            connection.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (rs != null) rs.close();
            if (ps != null) ps.close();
            if (connection != null) connection.close();
        }
    }

    /**
     * 刪除表
     *
     * @throws Exception
     */
    @Test
    public void dropTable() throws Exception {
        try {
            ps = connection.prepareStatement("drop table test");
            ps.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (rs != null) rs.close();
            if (ps != null) ps.close();
            if (connection != null) connection.close();
        }
    }

    @After
    public void close() throws Exception {
        if (rs != null) rs.close();
        if (ps != null) ps.close();
        if (connection != null) connection.close();

    }
}
  • 示例代碼下載
https://gitee.com/fang_wei/fwmagic-hbase
相關文章
相關標籤/搜索