英文:
[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
> <dependency>
> <groupid>com.databricks
> <artifactid>databricks-jdbc</artifactid>
> <version>2.6.25-1</version>
> </groupid></dependency>
>
>Here is some sample code to query data using JDBC driver:
>java
> import java.sql.*;
>
> public static void main(String[] args) throws Exception {
> // Open a connection
>
> // replace the values below
> 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");) {
> // Extract data from result set
> while (rs.next()) {
> // Retrieve by column name
> System.out.print("ID: " + rs.getString("col_name"));
> }
> }
>}
>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论