我使用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...|
+--------------------+--------------------+
其中interactions
是String
。我希望爆炸这首先将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_]
有什么想法吗?
答案 0 :(得分:3)
Dataset.explode。除非你有理由,否则请远离它。你已被警告过了。
如果您确实有理由使用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|
+---+-----------------+---+--------+
使用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"))