第一步:HBase安装Phoenix插件
HBase安装Phoenix的流程可以自行百度。有很多,就不说了。
第二步:在JAVA项目中添加依赖
在Linux中正常连接访问到Phoenix后,在项目中添加依赖【注意依赖的版本要和你安装的版本大致相同】
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.phoenix/phoenix -->
<dependency>
<groupId>org.apache.phoenix</groupId>
<artifactId>phoenix</artifactId>
<!-- 这里注意要和你安装的Phoenix版本号相同 -->
<version>4.14.0-cdh5.11.2</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.apache.phoenix</groupId>
<artifactId>phoenix-core</artifactId>
<!-- 这里注意要和你安装的Phoenix版本号相同 -->
<version>4.14.0-cdh5.11.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
</dependencies>
第三步:编写Phoenix工具类
【注意】Phoenix依赖一个Hadoop的外部依赖插件hadoop-common-2.2.0-bin-master。该外部依赖自行百度下载,解压后即可。
需要设置成为系统的全局变量。在Java中进行引入:System.setProperty(“hadoop.home.dir”, “D:\develop\hadoop\hadoop-common-2.2.0-bin-master”);
3.1先测试连接
/**
* 创建Phoenix连接
* @param ip zookeeper地址
* @param hadoopCommonUrl HadoopCommon的url
* @param schema 查询的Schema前缀(没有可为空字符串)
* @return 成功后的连接信息
* @throws ClassNotFoundException E
* @throws SQLException E
*/
public java.sql.Connection connectionPhoenix(String ip, String hadoopCommonUrl, String schema) throws ClassNotFoundException, SQLException {
System.setProperty("hadoop.home.dir", hadoopCommonUrl);
Class.forName("org.apache.phoenix.jdbc.PhoenixDriver");
java.sql.Connection conn = DriverManager.getConnection("jdbc:phoenix:" + ip);
if (!StringUtils.isEmpty(schema)) {
conn.setSchema(schema);
}
return conn;
}
3.2尝试发送SQL语句
/**
* 发送SQL语句查询信息
* @param conn 创建好的Phoenix连接
* @param schema 要查询的所属Schema(如果有前缀就就加,没有就不加)
* @param sql 要发送的SQL语句(select * from tableName)
* @return 查询后的结果
* @throws SQLException E
*/
public ResultSet sqlPhoenix(java.sql.Connection conn, String schema, String sql) throws SQLException {
conn.setSchema(schema);
Statement stmt = conn.createStatement();
return stmt.executeQuery(sql);
}
3.3根据返回的查询结果,获取表中的字段信息
try {
//获取表中的字段信息
PhoenixResultSet resultSet = (PhoenixResultSet)hBaseUtil.sqlPhoenix(conn, schema, ("select * from " + table));
//这里用到了JAVA反射,因为字段信息集合在jar依赖中是private类型,所以通过反射来获取到他的字段名称信息【工具类在下方】
RowProjector row = (RowProjector) ReflectionUtil.getPrivateField(resultSet, "rowProjector");
List<? extends ColumnProjector> column = row.getColumnProjectors();
//String[] arry = new String[column.size()];
for (int i = 0; i < column.size(); i++) {
System.out.println("这是字段信息 ====>" + column.get(i).getName());
//arry[i] = column.get(i).getName();
}
}catch (PhoenixParserException e){
System.out.println("不允许查询的:" + table);
}finally {
System.out.println("*****************分割符************");
}
附:JAVA反射工具类
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* @explain JAVA反射工具类
* @author Song
* @date 2019/12/17
*/
public class ReflectionUtil {
/**
* 获取私有成员变量的值
* @param instance 要获取的对象
* @param filedName 获取的变量名称
* @return 返回获取变量的信息(需要强转)
*/
public static Object getPrivateField(Object instance, String filedName) throws NoSuchFieldException, IllegalAccessException {
Field field = instance.getClass().getDeclaredField(filedName);
field.setAccessible(true);
return field.get(instance);
}
/**
* 设置私有成员的值
* @param instance 要获取的对象
* @param fieldName 要获取的变量名
* @param value 设置的值
*/
public static void setPrivateField(Object instance, String fieldName, Object value) throws NoSuchFieldException, IllegalAccessException {
Field field = instance.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
field.set(instance, value);
}
/**
* 访问私有方法
* @param instance 要获取的对象
* @param methodName 私有方法的名称
* @param classes CLASS的返回信息
* @param objects 参数信息
* @return
*/
public static Object invokePrivateMethod(Object instance, String methodName, Class[] classes, String objects) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
Method method = instance.getClass().getDeclaredMethod(methodName, classes);
method.setAccessible(true);
return method.invoke(instance, objects);
}
}
时间原因,下班啦,赶紧回家,就先这样。稍后在更新。