Phoenix jdbc举一个栗子

作者: 大猪大猪 | 来源:发表于2018-09-28 10:32 被阅读17次

    使用

    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");
        }
    }
    

    相关文章

      网友评论

        本文标题:Phoenix jdbc举一个栗子

        本文链接:https://www.haomeiwen.com/subject/vsvuoftx.html