Phoenix JDBC API

Phoenix JDBC

1、胖客户端

  1. maven依赖

    <dependencies>
        <dependency>
            <groupId>org.apache.phoenix</groupId>
            <artifactId>phoenix-core</artifactId>
            <version>5.0.0-HBase-2.0</version>
            <exclusions>
                <exclusion>
                    <groupId>org.glassfish</groupId>
                    <artifactId>javax.el</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.apache.hadoop</groupId>
                    <artifactId>hadoop-common</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.glassfish</groupId>
            <artifactId>javax.el</artifactId>
            <version>3.0.1-b06</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>2.8.4</version>
        </dependency>
    </dependencies>
    
  2. 编写代码

    package com.hpu.phoenix;
    
    import java.sql.*;
    import java.util.Properties;
    
    /**
     * @author zyn
     * @version 1.0
     * @date 2022/1/5 20:14
     */
    public class TestThickClient {
        public static void main(String[] args) throws SQLException {
            //1.获取url
            String url = "jdbc:phoenix:hadoop102,hadoop103,hadoop104:2181";
            //2.创建配置
            Properties properties = new Properties();
            //3.添加配置,需要客户端服务端参数保存一致
    //        properties.put("phoenix.schema.isNamespaceMappingEnabled", "true");
            //4.获取连接
            Connection connection = DriverManager.getConnection(url, properties);
            //5.编译SQL
            PreparedStatement preparedStatement = connection.prepareStatement("select * from \"test\"");
            //6.执行SQL
            ResultSet resultSet = preparedStatement.executeQuery();
            //7.遍历
            while (resultSet.next()) {
                System.out.println(resultSet.getString(1) + "-" + resultSet.getString(2) + "-" + resultSet.getString(3));
            }
            //8.关闭connection
            connection.close();
        }
    }
    

2、瘦客户端

  1. 启动query server

    queryserver.py [start]
    

    在这里插入图片描述

  2. maven依赖

    <dependencies>
            <!-- https://mvnrepository.com/artifact/org.apache.phoenix/phoenix-queryserver-client -->
            <dependency>
                <groupId>org.apache.phoenix</groupId>
                <artifactId>phoenix-queryserver-client</artifactId>
                <version>5.0.0-HBase-2.0</version>
            </dependency>
        </dependencies>
    
  3. 编写代码

    package com.hpu.phoenix;
    
    
    import org.apache.phoenix.queryserver.client.ThinClientUtil;
    
    import java.sql.*;
    
    /**
     * @author zyn
     * @version 1.0
     * @date 2022/1/5 20:23
     */
    public class TestThinClient {
        public static void main(String[] args) throws SQLException {
            //1.直接从瘦客户端获取连接
            String url = ThinClientUtil.getConnectionUrl("hadoop102", 8765);
            System.out.println(url);
            //2.获取连接
            Connection connection = DriverManager.getConnection(url);
            //3.编译SQL
            PreparedStatement preparedStatement = connection.prepareStatement("select * from \"test\"");
            //4.执行SQL
            ResultSet resultSet = preparedStatement.executeQuery();
            //5.遍历输出
            while (resultSet.next()) {
                System.out.println(resultSet.getString(1) + "-" + resultSet.getString(2) + "-" + resultSet.getString(3));
            }
            //6.关闭connection
            connection.close();
        }
    }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

MelodyYN

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

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

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

打赏作者

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

抵扣说明:

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

余额充值