通过 databricks 针对 JDBC 读取 Python 中的 SQL 文件到 运行

Read a SQL file in Python to run against a JDBC via databricks

我有一个 SQL 语句,我想 运行 使用数据块中的 JDBC 驱动程序针对 oracle 数据库。如果 SQL 语句很短,例如,如果它只是 selecting 来自 table 的所有数据,没有过滤器等,我可以成功地 运行。 . (例如 select * 来自 tbl)

但是,我有一个非常长的 SQL 代码需要执行,所以我创建了一个字符串,通过从 .[=38= 加载它来传递给 JDBC 驱动程序] 文件保存在数据块文件存储中。

当 运行 显示此错误时,在调查/打印文本文件的结果时,我发现它删除了一些 SQL 语句并在恢复之前提供了一条消息sql 声明:

*** WARNING: skipped 62431 bytes of output ***

实际上它在打印的字符串中看起来像这样:

    sum (
        case
            when dpr.pricing_Type in ('P', 'C') then
                nvl (
                    decode (dpr.price / 100, null, 0,
                        decode (apr.price, 'N', 

*** WARNING: skipped 62431 bytes of output ***

        then
            dpr.percentage_applied
        else 
            0
        end
    ) as price_percent,

请注意,警告之前的代码用于与警告消息之后的代码完全不同的字段。

对于这个问题的原因和解决方法有什么建议吗?

下面是我运行ning的完整脚本供参考,另请注意,我使用的.sql文件只有113kb,我使用的是python 3.7.5通过 运行time 7.4 of databricks:

%python

# setup jdbc credentials (from key vault) and url
jdbcUsername = dbutils.secrets.get(scope="USER", key="ID")
jdbcPassword = dbutils.secrets.get(scope="PWD", key="PWD")
jdbcUrl = "jdbc:oracle:thin:@<REDACTED>"
jdbcDrv = "oracle.jdbc.driver.OracleDriver"

# Table Name
OutputTbl = "db.tblCore"

# Drop table. 
spark.sql("DROP TABLE IF EXISTS " + OutputTbl  )

# parallelism
lbound = 20160101
ubound = 20210115
fsize = "1000"
colname = "date_value_yyyymmdd"
numParts = "10"

# Get sql stetment from file.
with open('/dbfs/FileStore/shared_uploads/<REDACTED>/SQL', 'r') as f:
    sql = file.read()

# Create DF and write output to a table. 
spdf = (spark.read.format("jdbc")
  .option("driver", jdbcDrv)
  .option("url", jdbcUrl)
  .option("user", jdbcUsername)
  .option("password", jdbcPassword)
  .option("dbtable", sql)
  .option("numPartitions", numParts)
  .option("fetchsize", fsize)
  .option("partitionColumn", colname) 
  .option("lowerBound", lbound)
  .option("upperBound", ubound)
  .load())

spdf.write.mode("overwrite").saveAsTable(OutputTbl)

这不是错误,它只是一个警告,表示输出被截断以防止浏览器过载等。您可以通过集群的 Spark UI 查看驱动程序和执行程序日志 -应该有更多信息...

我还建议首先尝试直接针对 Oracle 执行该语句,只是为了检查它是否有效