Phoenix JDBC
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>
-
编写代码
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、瘦客户端
-
启动query server
queryserver.py [start]
-
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>
-
编写代码
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(); } }