phoenix工具类 与 使用例子

phoenix工具类 

package com.hbase.util;

import com.util.PropertiesUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.*;
import java.util.Properties;
import java.util.UUID;

/**
 * @Auther: wjc
 * @Date: 2018/11/13
 * @Description: Phoenix工具类
 */
public class PhoenixUtils {

    private static final Logger logger  = LoggerFactory.getLogger(PhoenixUtils.class);
    private static Properties prop;

    static{
        try {
            Class.forName("org.apache.phoenix.jdbc.PhoenixDriver");
        } catch (ClassNotFoundException e) {
            logger.error("PhoenixDriver 驱动加载异常");
        }
        prop = PropertiesUtils.getObject("phoenix.properties");
    }

    private PhoenixUtils(){}

    /**
     * 获得phoenix SQL连接
     * @return
     * @throws SQLException
     */
    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(prop.getProperty("phoenix.url"));
    }

    /**
     * 生成唯一的游标名称
     * 避免多线程情况,执行游标时,phoenix出现异常
     * @return
     */
    public static String getCursorName(){
        // 游标名称不能数字开头 Error: ERROR 603 (42P00): Syntax error. Unexpected input. Expecting "NAME", got...
        String first = "ag";
        String uuid = UUID.randomUUID().toString().replace("-","");
        String cursorName = first + uuid;
        return cursorName;
    }

    /**
     * 关闭游标
     * 关闭资源前执行方法
     * @param con 连接
     * @param stmt 执行
     * @param cursorName 游标名称
     */
    public static void closeCursor(Connection con, PreparedStatement stmt, String cursorName){
        // 资源如果为空,直接返回
        if(con == null){
            return;
        }

        if(stmt == null){
            return;
        }

        // 关闭游标
        try {
            String sql = "CLOSE " + cursorName;
            stmt = con.prepareStatement(sql);
            stmt.execute();
        } catch (SQLException e) {
            logger.error(e.getMessage(),e);
        }
    }

    /**
     * 关闭资源
     * @param con 连接
     * @param stmt 执行
     * @param rset 结果集 可填 null
     */
    public static void closeResource(Connection con, Statement stmt, ResultSet rset){
        // 先释放结果集
        if(rset != null){
            try {
                rset.close();
            } catch (SQLException e) {
                logger.error(e.getMessage(),e);
            }
        }

        // 然后释放执行 Statement 或 PreparedStatement
        if(stmt != null){
            try {
                stmt.close();
            } catch (SQLException e) {
                logger.error(e.getMessage(),e);
            }
        }

        // 最后释放连接
        if(con != null){
            try {
                con.close();
            } catch (SQLException e) {
                logger.error(e.getMessage(),e);
            }
        }
    }

}

 

简单实用示例:

private void saveLoginLogToHBase(List loginVOList) throws Exception{
        // 获得phoenix连接
        Connection con = PhoenixUtils.getConnection();
        Statement stmt = con.createStatement();

        StringBuilder loginSql = new StringBuilder();
        for(LoginVO vo:loginVOList){
            loginSql.append("UPSERT INTO T_ORIGINAL_CONNECT_LOGIN(LOGIN_DAY,SEQ_ID,LOGIN_DATE,HOURS,LOGIN_TYPE,BRAND,DEVICE,OS,OSV,APV,EMAIL,USER_NAME,USER_TYPE,GENDER,IP,COUNTRY,CREATED_DATE) VALUES (");
            loginSql.append("'"+vo.getLoginDay()+"',");
            loginSql.append("NEXT VALUE FOR SEQ_T_ORIGINAL_CONNECT_LOGIN_SEQ_ID,");
            loginSql.append("'"+vo.getLoginDate()+"',");
            loginSql.append("'"+vo.getHours()+"',");
            loginSql.append("'"+vo.getLoginType()+"',");
            loginSql.append("'"+vo.getBrand()+"',");
            loginSql.append("'"+vo.getDevice()+"',");
            loginSql.append("'"+vo.getOs()+"',");
            loginSql.append("'"+vo.getOsv()+"',");
            loginSql.append("'"+vo.getApv()+"',");
            loginSql.append("'"+vo.getEmail()+"',");
            loginSql.append("'"+vo.getUserName()+"',");
            loginSql.append("'"+vo.getUserType()+"',");
            loginSql.append("'"+vo.getGender()+"',");
            loginSql.append("'"+vo.getIp()+"',");
            loginSql.append("'"+vo.getCountry()+"',");
            loginSql.append("'"+vo.getCreatedDate());
            loginSql.append("')");

            // 添加执行sql
            stmt.executeUpdate(loginSql.toString());

            // 清空StringBuilder
            loginSql.delete(0, loginSql.length());
        }

        // 批量提交
        con.commit();
        // 资源释放
        PhoenixUtils.colseResource(con, stmt, null);
    }

 

你可能感兴趣的:(Phoenix)