首頁 > 軟體

巨量資料開發phoenix連線hbase流程詳解

2022-11-23 14:02:21

一、安裝phoennix新增設定

1、將phoenix-server-hbase-2.4-5.1.2.jar拷貝至hbase的的lib下

cp  phoenix-server-hbase-2.4-5.1.2.jar ../hbase/lib/

2、設定phoenix可以存取hbase的系統表

(1)將以下設定新增至hbase-site.xml中

   <property>
      <name>phoenix.schema.isNamespaceMappingEnabled</name>
      <value>true</value>
   </property>
    <property>
        <name>phoenix.schema.mapSystemTablesToNamespace</name>
        <value>true</value>
    </property>

(2)將hbase-stie.xml拷貝到phoenix/bin目錄下

cp ../hbase/conf/hbase-site.xml   ../phoenix/bin/

二、啟動phoenix服務

1、啟動hbase

../hbase/bin/start-hbase.sh

2、啟動phoenix

python3  ../phoenix/bin/sqlline.py   server200:2181

server200:2181為zookeeper地址

三、phoenix常用語法

 官網檔案   https://phoenix.apache.org/language/index.html

(1)建立表

create table test1(id varchar primary key,a varchar,b varchar);

id主鍵可視為hbase的rowkey

(2)插入資料

upsert into TEST1 values('202211160089','liuping','chenyingying');

(3) 查詢資料

select * from TEST1;

(4)檢視/表對映

由於phoenix 無法直接存取hbase建立的非系統表,可以通過檢視/表對映對非系統表進行查詢,但檢視不可修改,表對映可讀可寫

在hbase上建立表名為eftb列族為fm1 、fm2的表

create 'reftb','fm1','fm2'

向表中新增資料

 put 'reftb','010101','fm1:name','zhangsan'
 put 'reftb','010101','fm2:age','九千歲'

<1>檢視對映

create view "reftb"(id varchar primary key,"fm1"."name" varchar,"fm2"."age" varchar);

<2>表對映

create table "reftb"(id varchar primary key,"fm1"."name" varchar,"fm2"."age" varchar);

<3>檢視資料

(5)新增修改資料(增改語法相同)

 upsert into "reftb"  values('010102','諸葛村夫','五十');
 upsert into "reftb"  values('010101','常山趙子龍','七十');

(6)刪除資料

delete from "reftb" where ID='010101';

(7)建立schema(資料庫名,對用hbase是的namespace)

CREATE SCHEMA IF NOT EXISTS "my_schema";

四、java程式碼整合phoenix

1、新增依賴

implementation 'org.apache.phoenix:phoenix-client-hbase-2.4:5.1.2'

2、編寫程式碼

public class PhoenixJdbcUtils {
    private final static Logger LOGGER = LoggerFactory.getLogger(PhoenixJdbcUtils.class);
    private static   Connection connection;
    static {
        Properties properties =new Properties();
        PhoenixDriver instance = PhoenixDriver.INSTANCE;
        try {
            connection = instance.connect("jdbc:phoenix:server200:2181", properties);
            ///connection = DriverManager.getConnection("jdbc:phoenix:server200:2181", properties);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    /**
     * 插入資料
     * @throws SQLException
     */
    public static   void  testUpsertData() throws SQLException {
        PreparedStatement psUpsert = connection.prepareStatement( " upsert  into "reftb" values('168936','劉備','63')");
        boolean addData = psUpsert.execute();
        LOGGER.info("addData---------"+addData);
        connection.commit();
    }
    /**
     * 查詢資料
     * @throws SQLException
     */
    public static void  testQueryData() throws SQLException {
        PreparedStatement psQuery = connection.prepareStatement(" select  *  from  "reftb" ");
        ResultSet resultSet = psQuery.executeQuery();
        while (resultSet.next()) {
            LOGGER.info("id--{}",resultSet.getString(1));
            LOGGER.info("name--{}",resultSet.getString(2));
            LOGGER.info("age--{}",resultSet.getString(3));
        }
    }
    public static void main(String[] args) {
        try {
            testQueryData();
            testUpsertData();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

到此這篇關於巨量資料開發phoenix連線hbase流程詳解的文章就介紹到這了,更多相關phoenix連線hbase內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com