Communication link failure. Failed to connect to server. Reason: HTTP Response code: 403

huangapple go评论252阅读模式
英文:

[Databricks][DatabricksJDBCDriver](500593) Communication link failure. Failed to connect to server. Reason: HTTP Response code: 403

问题

I am trying to connect to Databricks using Java code. Can someone help me please? Here is the code so far I have got:

package digital.eComm.ui.tests;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class DatabricksSetup {

    public static void main(String[] args) throws SQLException {
        String url = "jdbc:databricks://XXXX.azuredatabricks.net:443/default;transportMode=http;ssl=1;httpPath=sql/protocolv1/o/XXXXX;AuthMech=3;UID=token;PWD=XXXXXX";
        String username = "token";
        String password = "XXXX"; // Token generated from Databricks profile page.

        Connection connection = DriverManager.getConnection(url, username, password);

        System.out.println("Database connected!");
        if (connection != null) {
            System.out.println("Connection Established");
        } else {
            System.out.println("Connection Failed");
        }
    }
}

Below dependency is already added:

"com.databricks:databricks-jdbc:2.6.25"

Error:

Exception in thread "main" java.sql.SQLException: [Databricks][DatabricksJDBCDriver](500593) Communication link failure. Failed to connect to the server. Reason: HTTP Response code: 403, Error message: Unknown.
    ...

错误信息:连接到 Databricks 时出现问题,HTTP 响应代码:403,错误消息:未知。

英文:

I am trying to connect to databricks using java code. Can someone help me please? Here is the code so far I have got::

package digital.eComm.ui.tests;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class DatabricksSetup {


    public static void main(String[] args) throws SQLException {
        String url = "jdbc:databricks://XXXX.azuredatabricks.net:443/default;transportMode=http;ssl=1;httpPath=sql/protocolv1/o/XXXXX;AuthMech=3;UID=token;PWD=XXXXXX";
        String username = "token";
        String password = "XXXX"; //Token generated from databricks profile page.

        Connection connection = DriverManager.getConnection(url, username, password);


        System.out.println("Database connected!");
        if(connection != null){
            System.out.println("Connection Established");

        }
        else {
            System.out.println("Connection Failed");
        }

    }

}

Below dependancy is already added :

  "com.databricks:databricks-jdbc:2.6.25"

Error :

Exception in thread "main" java.sql.SQLException: [Databricks][DatabricksJDBCDriver](500593) Communication link failure. Failed to connect to server. Reason: HTTP Response code: 403, Error message: Unknown.
	at com.databricks.client.hivecommon.api.HS2Client.handleTTransportException(Unknown Source)
	at com.databricks.client.spark.jdbc.DowloadableFetchClient.handleTTransportException(Unknown Source)
	at com.databricks.client.hivecommon.api.HS2Client.openSession(Unknown Source)
	at com.databricks.client.hivecommon.api.HS2Client.<init>(Unknown Source)
	at com.databricks.client.spark.jdbc.DowloadableFetchClient.<init>(Unknown Source)
	at com.databricks.client.spark.jdbc.DownloadableFetchClientFactory.createClient(Unknown Source)
	at com.databricks.client.hivecommon.core.HiveJDBCCommonConnection.connectToServer(Unknown Source)
	at com.databricks.client.spark.core.SparkJDBCConnection.connectToServer(Unknown Source)
	at com.databricks.client.hivecommon.core.HiveJDBCCommonConnection.establishConnection(Unknown Source)
	at com.databricks.client.spark.core.SparkJDBCConnection.establishConnection(Unknown Source)
	at com.databricks.client.jdbc.core.LoginTimeoutConnection.connect(Unknown Source)
	at com.databricks.client.jdbc.common.BaseConnectionFactory.doConnect(Unknown Source)
	at com.databricks.client.jdbc.common.AbstractDriver.connect(Unknown Source)
	at java.sql.DriverManager.getConnection(DriverManager.java:664)
	at java.sql.DriverManager.getConnection(DriverManager.java:247)
	at digital.eComm.ui.tests.DatabricksSetup.main(DatabricksSetup.java:16)
Caused by: com.databricks.client.support.exceptions.ErrorException: [Databricks][DatabricksJDBCDriver](500593) Communication link failure. Failed to connect to server. Reason: HTTP Response code: 403, Error message: Unknown.
	... 16 more

答案1

得分: 1

"403" 是一个明确的拒绝。服务器告诉你:我知道你是谁,但你不能做你试图做的事情。

很可能是因为驱动程序尝试使用用户名/密码机制进行身份验证,而不是在 URL 中使用令牌,因为你明确地将参数传递给 getConnection()

DriverManager.getConnection(url, username, password); 替换为 DriverManager.getConnection(url); 并尝试。

来自 Databricks 的完整示例

Maven 上的 Databricks JDBC 驱动程序

Java 和 JVM 开发人员使用 JDBC 作为访问数据库的标准 API。Databricks JDBC 驱动程序现在可在 Maven 中央仓库中使用,让你可以在构建系统和 CI/CD 运行中使用此驱动程序。要将其包含在你的 Java 项目中,请将以下条目添加到你的应用程序的 pom.xml 中:

    <dependency>
      <groupid>com.databricks
      <artifactid>databricks-jdbc</artifactid>
      <version>2.6.25-1</version>
    </groupid></dependency>

这里是使用 JDBC 驱动程序查询数据的示例代码:

import java.sql.*;

public static void main(String[] args) throws Exception {
    // 打开连接

    // 替换下面的值
    String token = "dapi*****";
    String url = "jdbc:databricks://********.cloud.databricks.com:443/default;" +      
                 "transportMode=http;ssl=1;AuthMech=3;httpPath=sql/protocolv1/o/*****;" +
                 "UID=token;" +
                 "PWD=" + token;

    try (Connection conn = DriverManager.getConnection(url);
         Statement stmt = conn.createStatement();
         ResultSet rs = stmt.executeQuery("SELECT * FROM samples.nyctaxi.trips");) {
        // 从结果集中提取数据
        while (rs.next()) {
            // 根据列名检索
            System.out.print("ID: " + rs.getString("col_name"));
        }
    } 
}
英文:

403 is a clear refusal. Server telling you: I know who you are, but you can't do what you're trying to do.

Most likely because the driver is trying to use user/password mechanism to authenticate instead of token in URL because you're explicitly passing the params to getConnection().

replace DriverManager.getConnection(url, username, password); with DriverManager.getConnection(url); and try.

Full example from databricks:

> Databricks JDBC Driver on Maven
>
> Java and JVM developers use JDBC as a standard API for accessing databases. Databricks JDBC Driver is now available on the Maven Central repository, letting you use this driver in your build system and CI/CD runs. To include it in your Java project, add the following entry to your application’s pom.xml:
>
> xml
&gt; &lt;dependency&gt;
&gt; &lt;groupid&gt;com.databricks
&gt; &lt;artifactid&gt;databricks-jdbc&lt;/artifactid&gt;
&gt; &lt;version&gt;2.6.25-1&lt;/version&gt;
&gt; &lt;/groupid&gt;&lt;/dependency&gt;
&gt;

>Here is some sample code to query data using JDBC driver:
>java
&gt; import java.sql.*;
&gt;
&gt; public static void main(String[] args) throws Exception {
&gt; // Open a connection
&gt;
&gt; // replace the values below
&gt; String token = &quot;dapi*****&quot;;
&gt; String url = &quot;jdbc:databricks://********.cloud.databricks.com:443/default;&quot; +
&gt; &quot;transportMode=http;ssl=1;AuthMech=3;httpPath=sql/protocolv1/o/*****;&quot; +
&gt; &quot;UID=token;&quot; +
&gt; &quot;PWD=&quot; + token;
&gt;
&gt; try (Connection conn = DriverManager.getConnection(url);
&gt; Statement stmt = conn.createStatement();
&gt; ResultSet rs = stmt.executeQuery(&quot;SELECT * FROM samples.nyctaxi.trips&quot;);) {
&gt; // Extract data from result set
&gt; while (rs.next()) {
&gt; // Retrieve by column name
&gt; System.out.print(&quot;ID: &quot; + rs.getString(&quot;col_name&quot;));
&gt; }
&gt; }
&gt;}
&gt;

huangapple
  • 本文由 发表于 2023年6月8日 03:07:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76426385.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定