如何使用自定义UDF使用DataFrame.clame.explode将字符串拆分为子字符串



我使用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。我想通过首先将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_]

有什么想法吗?

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", ","))
                   ^

您可以按照以下方式进行操作(请注意有关explode贬值的警告,因为我使用2.1.0-snapshot):

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|
+---+-----------------+---+--------+

改用爆炸函数,您应该按照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"))

相关内容

  • 没有找到相关文章

最新更新