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文件

结果输出

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

局外人一枚

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

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

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

打赏作者

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

抵扣说明:

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

余额充值