scala 如何使用JDBC驱动程序连接到Databricks增量表

d6kp6zgx  于 2022-11-23  发布在  Scala
关注(0)|答案(1)|浏览(118)

如何使用JDBC连接到Databricks Delta表?
我已经尝试连接simba驱动程序,但我得到了驱动程序类名和url配置的困难时间。
任何解决方案都是赞赏。我不能粘贴代码在这里作为其公司代码。
先谢谢你。

shyt4zoc

shyt4zoc1#

请检查下面的此链接。此链接包含使用JDBC配置增量的步骤
http://sedeks.blogspot.com/2019/05/how-to-connect-to-databricks-delta.html
此链接中提供代码:

import java.sql.DriverManager 
import java.sql.Driver
import java.sql.Connection 
import javax.sql.DataSource

object ScalaJdbcConnectSelect {
    def main(args: Array[String]) {
        val driver = "com.simba.spark.jdbc41.Driver"  //attach the Spark jar to the Classpath.
        val url = "jdbc:spark://field-eng.cloud.databricks.com:443/default;transportMode=http;ssl=true;httpPath=sql/protocolvl/o/0/0911-153027-hopes19";    
        val username = "token" 
        val password = "<token-value>" //Token generated from databricks profile page.
        var connection:Connection = null
        try {
        // Create the connection
            Class.forName(driver)
            connection = DriverManager.getConnection(url, username, password)
            if(connection != null){
                println("Connection Established");

            }
            else {
                println("Connection Failed");
            }
            // create the statement
            val statement = connection.createStatement()
            val resultSet = statement.executeQuery("<<Query>") // Profile your query here.
            while ( resultSet.next() ) {
                // Iterate through Result set
            } 
        catch {
            case e => e.printStackTrace
        }
        connection.close()
    }
}

相关问题