phoenix-java代碼訪問

寫在前面

本文接上篇phoenix安裝部署:https://blog.csdn.net/weixin_42814075/article/details/94436278
主要介紹如何通過java代碼訪問phoenix,進行查詢操作

版本

hbase 1.1.2
phoenix 4.7.0

POM依賴

<properties>
            <phoenix.version>4.7.0-HBase-1.1</phoenix.version>
    </properties>
<dependencies>
    <dependency>
        <groupId>org.apache.phoenix</groupId>
        <artifactId>phoenix-core</artifactId>
        <version>${phoenix.version}</version>
    </dependency>
</dependencies>

主函數

		Properties props = new Properties();
        props.setProperty("phoenix.query.timeoutMs", "1200000");
        props.setProperty("hbase.rpc.timeout", "1200000");
        props.setProperty("hbase.client.scanner.timeout.period", "1200000");
        try {
            Class.forName("org.apache.phoenix.jdbc.PhoenixDriver");

            // 這裏配置zookeeper的地址,可單個,也可多個。可以是域名或者ip
            String url= "jdbc:phoenix:z-001,z-002,z-003:2181:/hbase-unsecure";
            Connection conn= DriverManager.getConnection(url, props);
            System.out.println(conn);
            Statement  statement = conn.createStatement();

            String sql  = "select count(1) from test";
            long time = System.currentTimeMillis();
            ResultSet rs   = statement.executeQuery(sql);
            while (rs.next()) {
                int count = rs.getInt(1);
                System.out.println("row count is " + count);
            }
            long timeUsed = System.currentTimeMillis() - time;
            System.out.println("time " + timeUsed + "mm");
            // 關閉連接
            rs.close();
            statement.close();
            conn.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

這裏有個點,windows上開發代碼的時候一定要注意地址映射配置修改hosts文件

結果輸出

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章