Phoenix jdbc举一个栗子

使用

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.security.UserGroupInformation;
import org.junit.Before;
import org.junit.Test;

import java.sql.*;

public class PhoenixTest {
    private static String url = "jdbc:phoenix:storm4.demo.com,storm2.demo.com,storm3.demo.com";
    private static String driverName ="org.apache.phoenix.jdbc.PhoenixDriver";
    private static Connection conn;
    private static PreparedStatement ps;
    private static ResultSet rs;

    @Before
    public void init() throws Exception {
        System.setProperty("java.security.krb5.conf", "/etc/krb5.conf");
        final String user = "admin/admin@DEMO.COM";
        final String keyPath = "/etc/security/keytabs/admin.keytab";

        Configuration conf = new Configuration();
        conf.set("hbase.zookeeper.quorum", "storm4.demo.com,storm2.demo.com,storm3.demo.com");
        conf.set("hadoop.security.authentication", "kerberos");

        UserGroupInformation.setConfiguration(conf);
        UserGroupInformation.loginUserFromKeytab(user, keyPath);
    }

    public Connection getConnnection() {
        try {
            Class.forName(driverName);
            conn = DriverManager.getConnection(url);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.exit(1);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }
    public PreparedStatement prepare(Connection conn, String sql) {
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ps;
    }
    public void getAll(String tablename) {
        conn=getConnnection();
        String sql="select count(1) from \""+tablename+"\"";
        System.out.println(sql);
        try {
            ps=prepare(conn, sql);
            rs=ps.executeQuery();
            int columns=rs.getMetaData().getColumnCount();
            while(rs.next()) {
                for(int i=1;i<=columns;i++) {
                    System.out.print(rs.getString(i));
                    System.out.print("\t\t");
                }
                System.out.println();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testCount(){
        getAll("TEST_LOG");
    }
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容