使用JDBC和Java连接到apache phoenix

时间:2017-03-28 11:47:21

标签: java jdbc hbase phoenix

我有一个小的java程序,我尝试与我运行的远程Phoenix服务器建立连接。

package jdbc_tests;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class PhoenixTest {

    static final String JDBC_DRIVER = "org.apache.phoenix.jdbc.PhoenixDriver";
    static final String IP = "<placeholder>"
    static final String PORT = "<placeholder>"
    static final String DB_URL = "jdbc:phoenix://" + IP + ":" + PORT + "/";

    public static void main(String[] args) {
        Connection conn = null;
        Statement st = null;

        try {
            Class.forName("org.apache.phoenix.jdbc.PhoenixDriver");

            System.out.println("Connecting to database..");

            conn = DriverManager.getConnection(DB_URL);

            System.out.println("Creating statement...");

            st = conn.createStatement();
            String sql;
            sql = "SELECT DISTINCT did FROM sensor_data";
            ResultSet rs = st.executeQuery(sql);

            while(rs.next()) {
                String did = rs.getString(1);
                System.out.println("Did found: " + did);
            }

            rs.close();
            st.close();
            conn.close();

        } catch (SQLException se) {
            se.printStackTrace();
        } catch (Exception e) {
            // Handle errors for Class.forName
            e.printStackTrace();
        } finally {
            // finally block used to close resources
            try {
                if (st != null)
                    st.close();
            } catch (SQLException se2) {
            } // nothing we can do
            try {
                if (conn != null)
                    conn.close();
            } catch (SQLException se) {
                se.printStackTrace();
            } // end finally try
        } // end try
    System.out.println("Goodbye!");
    }
}

当我尝试运行我的程序时,我得到:

java.sql.SQLException: No suitable driver found for jdbc:phoenix://<ip>:<port>/

我在Apache Phoenix发行版中添加了以下jar:

phoenix-4.9.0-HBase-1.2-client.jar

与我的凤凰和Hbase血统相匹配。

我做错了什么?

2 个答案:

答案 0 :(得分:2)

我认为您错过了数据库的名称以及URL接缝不正确:

"jdbc:phoenix://" + IP + ":" + PORT + "/" + DB_NAME
//--------------------------------------------^^

您必须尝试使用​​此网址:

Connection con = DriverManager.getConnection("jdbc:phoenix:<IP>:<port>:/<DB_NAME>");

答案 1 :(得分:0)

URL不应包含“ //”,应如下所示: jdbc:phoenix:192.168.4.251:2181:/ hbase-unsecure