使用 JDBC 和 Java 连接到 apache phoenix

Connecting to apache phoenix using JDBC and Java

我有一个小的 java 程序,我在其中尝试建立到我有 运行ning 的远程 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

与我的 Phoenix 和 Hbase 版本匹配。

我做错了什么?

我认为您遗漏了数据库的名称,而且 URL 接缝不正确:

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

你必须尝试使用​​这个 URL 相反:

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

URL 不应该包含 "//" ,它应该是这样的: jdbc:phoenix:192.168.4.251:2181:/hbase-不安全