将加载的列表从文本文件传递到 Spark SQL 中的 sql 查询



我从使用,作为分隔符的文本文件中读取了帐户:

val csv = spark.read.text("src/main/resources/in/insight/account_issues.txt")
//implicits
import spark.sqlContext.implicits._
val string_account = csv.map(_.getString(0)).collect.toList.toString()
//print(string_account)
val query = s"""(SELECT
               |    ACCOUNT_NUMBER,
               |    CASE WHEN STMT.CRF_TYPE='CREDIT' THEN STMT.AMOUNT_LCY
               |        ELSE NULL
               |    END as 'CreditAmount',
               |    CASE WHEN STMT.CRF_TYPE='DEBIT' THEN STMT.AMOUNT_LCY
               |        ELSE  NULL
               |    END as 'DebitAmount',
               |    STMT.BOOKING_DATE,
               |    STMT.VALUE_DATE,
               |    CRF_TYPE
               |FROM [InsightLanding].[dbo].[v_STMT_ENTRY] AS STMT
               |    LEFT JOIN [InsightWarehouse].[dbo].[v_Account] AS A ON a.AccountNum = STMT.ACCOUNT_NUMBER
               |
               |WHERE STMT.MIS_DATE='$BusinessDate'
               | AND STMT.ACCOUNT_NUMBER IN ($string_account) ) tmp """.stripMargin
val responseWithSelectedColumns = spark
  .read
  .format("jdbc")
  .option("url", url)
  .option("driver", driver)
  .option("dbtable", query)
  .load()

我无法获得作品,而是收到错误:

: 'List' is not a recognized built-in function name

我的代码出了什么问题?

创建string_account时,您会在列表中使用toString()。这将为您提供一个字符串List(...),例如:

scala> List(1,2,3).toString()
res0: String = List(1, 2, 3)

您要改用的是mkString(",")

scala> List(1,2,3).mkString(",")
res1: String = "1,2,3"

在这种情况下,这将是:

val string_account = csv.map(_.getString(0)).collect.toList.mkString(",")

注意:如果需要,可以轻松地将括号添加到string_account而不是将它们放在SQL查询中mkString("(", ",", ")")

相关内容

  • 没有找到相关文章

最新更新