【问题标题】:locate function usage on dataframe without using UDF Spark Scala在不使用 UDF Spark Scala 的情况下定位数据帧上的函数使用
【发布时间】:2022-12-09 23:10:17
【问题描述】:

我很好奇为什么这在数据帧上的 Spark Scala 中不起作用:

df.withColumn("answer", locate(df("search_string"), col("hit_songs"), pos=1))

它适用于 UDF,但不是按照上面的那样。 Col 与 String 方面。看起来很尴尬,缺乏方面。 IE。如何将列转换为字符串以传递给需要 String 的位置。

df("search_string") 允许生成字符串是我的理解。

但是得到的错误是:

command-679436134936072:15: error: type mismatch;
 found   : org.apache.spark.sql.Column
 required: String
df.withColumn("answer", locate(df("search_string"), col("hit_songs"), pos=1))

【问题讨论】:

  • 你得到什么错误?
  • @GaurangShah 更新了问题
  • 我也收到 pyspark 错误TypeError: 'DataFrame' object is not callable。火花3
  • 这是其他地方的问题。但这是关于scala的
  • 对不起。无法理解你的问题。我以为你说它在 python 中工作。下面的 API 总是相同的。所以它不可能在 python 中工作,但在 scala 中不行。我刚刚测试了它。它在 Python 中不起作用。原因是,API 需要字符串而不是列。两者都是不同的数据类型。

标签: scala apache-spark


【解决方案1】:

了解出了什么问题

我不确定您使用的是哪个版本的 Spark,但是 locate 方法在 both Spark 3.3.1(当前最新版本)和 Spark 2.4.5(在我本地运行的 Spark shell 上运行的版本)上具有以下函数签名.

此函数签名如下:

def locate(substr: String, str: Column, pos: Int): Column 

所以substr不能是Column,它需要是String。在你的例子中,你使用的是df("search_string")。这实际上调用了具有以下函数签名的applymethod

def apply(colName: String): Column 

所以你遇到问题是有道理的,因为 locate 函数需要 String

正在尝试解决您的问题

如果我理解正确的话,您希望能够在没有 UDF 的情况下从另一列的字符串中的一列中找到一个子字符串。您可以在 Dataset 上使用 map 来做到这一点。像这样:

import spark.implicits._

case class MyTest (A:String, B: String)

val df = Seq(
  MyTest("with", "potatoes with meat"),
  MyTest("with", "pasta with cream"),
  MyTest("food", "tasty food"),
  MyTest("notInThere", "don't forget some nice drinks")
).toDF("A", "B").as[MyTest]

val output = df.map{
  case MyTest(a,b) => (a, b, b indexOf a)
}
output.show(false)                                                                                                                                                                                                                                                       
+----------+-----------------------------+---+                                                                                                                                                                                                                                  
|_1        |_2                           |_3 |                                                                                                                                                                                                                                  
+----------+-----------------------------+---+                                                                                                                                                                                                                                  
|with      |potatoes with meat           |9  |                                                                                                                                                                                                                                  
|with      |pasta with cream             |6  |                                                                                                                                                                                                                                  
|food      |tasty food                   |6  |                                                                                                                                                                                                                                  
|notInThere|don't forget some nice drinks|-1 |                                                                                                                                                                                                                                  
+----------+-----------------------------+---+

一旦进入强类型 Datasetmap 操作,您就可以使用 Scala 语言。

希望这可以帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-16
    • 2019-01-24
    • 1970-01-01
    • 2017-09-22
    • 2020-07-22
    • 2018-09-17
    • 2023-01-09
    • 2015-12-25
    相关资源
    最近更新 更多