使用jdbc和java连接apachephoenix

noj0wjuj  于 2021-06-10  发布在  Hbase
关注(0)|答案(2)|浏览(385)

我有一个小的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>/

我从ApachePhoenix发行版中添加了以下jar:

phoenix-4.9.0-HBase-1.2-client.jar

和我的Phoenix城和hbase vesions相配。
我做错什么了?

9bfwbjaz

9bfwbjaz1#

我认为您缺少数据库的名称,而且url不正确:

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

您必须尝试使用此url:

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

8ehkhllq2#

url不应包含“/”,应如下所示:jdbc:phoenix:192.168.4.251:2181:/hbase不安全

相关问题