首页 > 解决方案 > 使用 Simba Spark JDBC 驱动程序的多语句查询

问题描述

我正在尝试使用 jdbc 连接调用 Databricks 集群。在这里,我想发送多个语句。一种是“缓存表选择”,另一种是选择创建的表。

我一直无法理解一系列事情,因为这两个语句的类型不同,我试图通过两次选择来进行第一次测试,但我一直无法这样做。

我的代码

Connection connection = null;
    
try {
    // Create the connection
    Class.forName(driver);
    connection = DriverManager.getConnection(url, username, password);
    if(connection != null){
        System.out.println("Connection Established");
    }
    else {
        System.out.println("Connection Failed");
    }
    // create the statement
    //Statement statement = connection.createStatement();
    StringBuffer sql = new StringBuffer( "select 1;" );
    sql.append( " select 2" );
    CallableStatement stmt = connection.prepareCall(sql.toString());
    boolean results = stmt.execute();
    while ( results ) {
        ResultSet rs = stmt.getResultSet();
        try {
            while (rs.next()) {
                // read the data
            }
        } finally {
            try { rs.close(); } catch (Throwable ignore) {}
        }
    }
}
catch (Exception e) {
    System.out.println(e.toString());
}
connection.close();

我得到的错误

[Simba]SparkJDBCDriver 错误处理查询/语句。错误代码:0,SQL 状态:运行查询时出错:org.apache.spark.sql.catalyst.parser.ParseException:无关输入 'select' 期望 {,';'}(第 1 行,第 10 行)

我已经尝试使用 PreparedStatement 类或仅使用 Statement 而不是 CallableStatement,但我得到了同样的错误。

将选项 allowMultiQueries 添加到连接字符串也没有任何区别,但我相信问题出在此之前。

标签: javaazure-databricksspark-jdbc

解决方案


推荐阅读