引发选择并添加具有别名的列 [英] spark select and add columns with alias

查看:224
本文介绍了引发选择并添加具有别名的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想选择几列,添加几列或除以某些列,并用空格填充这些列,并以新名称存储它们作为别名.例如,SQL中的内容应类似于:

I want to select few columns, add few columns or divide, with some columns as space padded and store them with new names as alias. For example in SQL should be something like:

select "   " as col1, b as b1, c+d as e from table 

如何在Spark中实现这一目标?

How can I achieve this in Spark?

推荐答案

您还可以使用本机DF函数.例如:

You can also use the native DF functions as well. For example given:

import org.apache.spark.sql.functions._
val df1 = Seq(
 ("A",1,5,3),
 ("B",3,4,2),
 ("C",4,6,3),
 ("D",5,9,1)).toDF("a","b","c","d")

将列选择为:

df1.select(lit(" ").as("col1"),
           col("b").as("b1"),
           (col("c") + col("d")).as("e"))

为您提供了预期的结果:

gives you the expected result:

+----+---+---+
|col1| b1|  e|
+----+---+---+
|    |  1|  8|
|    |  3|  6|
|    |  4|  9|
|    |  5| 10|
+----+---+---+

这篇关于引发选择并添加具有别名的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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