收集数据框上的火花 [英] collect on a dataframe spark

查看:55
本文介绍了收集数据框上的火花的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这样写:

df.select(col("colname")).distinct().collect.map(_.toString()).toList

结果为

List("[2019-06-24]", "[2019-06-22]", "[2019-06-23]")

我想得到的地方:

List("2019-06-24", "2019-06-22", "2019-06-23")

如何更改此值

推荐答案

您需要更改 .map(_。toString()) .map(_。getAs [String]( colname))

通过 .map(_。toString()),您正在调用 org.apache.spark.sql.Row.toString ,这就是为什么输出类似于 List( [2019-06-24], [2019-06-22], [2019-06-23] )

正确的方法是:

val list = df.select( colname)。distinct() .collect()。map(_。getAs [String]( colname))。toList


输出将是:

You need to change .map(_.toString()) to .map(_.getAs[String]("colname")).

With .map(_.toString()), you are calling org.apache.spark.sql.Row.toString, that's why the output is like List("[2019-06-24]", "[2019-06-22]", "[2019-06-23]").

Correct way is:
val list = df.select("colname").distinct().collect().map(_.getAs[String]("colname")).toList

Output will be:

List("2019-06-24", "2019-06-22", "2019-06-23")

这篇关于收集数据框上的火花的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆