在火花中遍历每列并找到最大长度 [英] In spark iterate through each column and find the max length
本文介绍了在火花中遍历每列并找到最大长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是Spark Scala的新手,在
以下的情况是这样的,我在群集上有一个表 TEST_TABLE(可以是蜂巢表)
我正在将其转换为数据框
作为:
I am new to spark scala and I have following situation as below I have a table "TEST_TABLE" on cluster(can be hive table) I am converting that to dataframe as:
scala> val testDF = spark.sql("select * from TEST_TABLE limit 10")
现在DF可以是视为
scala> testDF.show()
COL1|COL2|COL3
----------------
abc|abcd|abcdef
a|BCBDFG|qddfde
MN|1234B678|sd
我想要以下输出
COLUMN_NAME|MAX_LENGTH
COL1|3
COL2|8
COL3|6
在spark scala中这样做可行吗?
Is this feasible to do so in spark scala?
推荐答案
简单而又简单:
import org.apache.spark.sql.functions._
val df = spark.table("TEST_TABLE")
df.select(df.columns.map(c => max(length(col(c)))): _*)
这篇关于在火花中遍历每列并找到最大长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文