【发布时间】: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...|
+--------------------+--------------------+
其中interactions 是String。我想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