【问题标题】:How to use DataFrame.explode with a custom UDF to split a string into substrings?如何使用 DataFrame.explode 和自定义 UDF 将字符串拆分为子字符串?
【发布时间】:2019-01-10 03:05:56
【问题描述】:

我使用 Spark 1.5

我有一个 DataFrame A_DF 如下:

+--------------------+--------------------+
|                  id|        interactions|
+--------------------+--------------------+
|        id1         |30439831,30447866...|
|        id2         |37597858,34499875...|
|        id3         |30447866,32896718...|
|        id4         |33029476,31988037...|
|        id5         |37663606,37627579...|
|        id6         |37663606,37627579...|
|        id7         |36922232,37675077...|
|        id8         |37359529,37668820...|
|        id9         |37675077,37707778...|
+--------------------+--------------------+

其中interactionsString。我想explode首先将interactions 字符串拆分为一组用逗号分隔的子字符串,我尝试如下操作:

val splitArr = udf { (s: String) => s.split(",").map(_.trim) }

val B_DF = A_DF.explode(splitArr($"interactions"))

但我收到以下错误:

error: missing arguments for method explode in class DataFrame;
follow this method with `_' if you want to treat it as a partially applied function A_DF.explode(splitArr($"interactions"))

我不明白。所以我尝试了更复杂的方法:

val B_DF = A_DF.explode($"interactions") { case (Row(interactions: String) =>
        interactions.split(",").map(_.trim))
     }

我收到了检查警告,内容如下:

Expression of Type Array[String] does not conform to expected type TraversableOnce[A_]

有什么想法吗?

【问题讨论】:

  • explode 不是取UDF,它只是一个正常的功能。应该更像这样:A_DF.explode("interactions", "interaction") { (s: String) => s.split(",").map(_.trim) }

标签: scala apache-spark apache-spark-sql


【解决方案1】:

Dataset.explode 自 Spark 2.0.0 起已弃用。除非你有理由,否则远离它。您已收到警告。

如果您确实有理由使用DataFrame.explode,请查看签名:

explode[A, B](inputColumn: String, outputColumn: String)(f: (A) ⇒ TraversableOnce[B])(implicit arg0: scala.reflect.api.JavaUniverse.TypeTag[B]): DataFrame

explode[A <: Product](input: Column*)(f: (Row) ⇒ TraversableOnce[A])(implicit arg0: scala.reflect.api.JavaUniverse.TypeTag[A]): DataFrame

在任何一种情况下,explode 使用两个参数组,因此出现第一个错误。

(这是 Spark 2.1.0-SNAPSHOT

scala> spark.version
res1: String = 2.1.0-SNAPSHOT

scala> val A_DF = Seq(("id1", "30439831,30447866")).toDF("id", "interactions")
A_DF: org.apache.spark.sql.DataFrame = [id: string, interactions: string]

scala> A_DF.explode(split($"interactions", ","))
<console>:26: error: missing argument list for method explode in class Dataset
Unapplied methods are only converted to functions when a function type is expected.
You can make this conversion explicit by writing `explode _` or `explode(_)(_)(_)` instead of `explode`.
       A_DF.explode(split($"interactions", ","))
                   ^

您可以按以下方式进行操作(请注意,在我使用 2.1.0-SNAPSHOT 时,有关弃用 explode 的警告):

scala> A_DF.explode[String, String]("interactions", "parts")(_.split(",")).show
warning: there was one deprecation warning; re-run with -deprecation for details
+---+-----------------+--------+
| id|     interactions|   parts|
+---+-----------------+--------+
|id1|30439831,30447866|30439831|
|id1|30439831,30447866|30447866|
+---+-----------------+--------+

您可以使用另一个explode,如下所示:

scala> import org.apache.spark.sql.Row
import org.apache.spark.sql.Row

scala> case class Interaction(id: String, part: String)
defined class Interaction

scala> A_DF.explode[Interaction]($"id", $"interactions") { case Row(id: String, ins: String) => ins.split(",").map { it => Interaction(id, it) } }.show
warning: there was one deprecation warning; re-run with -deprecation for details
+---+-----------------+---+--------+
| id|     interactions| id|    part|
+---+-----------------+---+--------+
|id1|30439831,30447866|id1|30439831|
|id1|30439831,30447866|id1|30447866|
+---+-----------------+---+--------+

改用explode function,你应该没问题,如scaladoc中所述(引用如下):


鉴于已弃用,作为替代方案,您可以使用 functions.explode() 展开列:

ds.select(explode(split('words, " ")).as("word"))

flatMap():

ds.flatMap(_.words.split(" "))

然后你可以使用explode函数如下:

A_DF.select($"id", explode(split('interactions, ",") as "part"))

【讨论】:

    猜你喜欢
    • 2023-04-04
    • 1970-01-01
    • 2015-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-25
    • 1970-01-01
    相关资源
    最近更新 更多