Java Spark SQL - 选择所有 AND 计算列?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38467763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spark SQL - Select all AND computed columns?
提问by lte__
This is a total noob question, sorry for that. In Spark, I can use select as:
这是一个完全的菜鸟问题,抱歉。在 Spark 中,我可以使用 select 作为:
df.select("*"); //to select everything
df.select(df.col("colname")[, df.col("colname")]); //to select one or more columns
df.select(df.col("colname"), df.col("colname").plus(1)) //to select a column and a calculated column
But. How can I select all the columns PLUS a calculated one? Obviously
select("*", df.col("colname").plus(1))
doesn't work (compilation error). How can this be done under JAVA?
Thank you!
但。如何选择所有列加上计算的列?显然
select("*", df.col("colname").plus(1))
不起作用(编译错误)。这在JAVA下怎么做?谢谢!
采纳答案by Yuan JI
Just do:
做就是了:
df.select(df.col("*"), df.col("colName").plus(1));
回答by Daniel Gallegos
You can use withColumn()
method, this will create a new column to the DataFrame.
您可以使用withColumn()
方法,这将为 DataFrame 创建一个新列。
df.select("*")
.withColumn("ColName", col("colName").plus(1))
回答by Saurabh
The difference between .select() and .withColumn() methods is that .select() returns only the columns you specify, while .withColumn() returns all the columns of the DataFrame in addition to the one you defined.
.select() 和 .withColumn() 方法之间的区别在于 .select() 仅返回您指定的列,而 .withColumn() 除了您定义的列之外,还返回 DataFrame 的所有列。
You can directly use withColumn:
您可以直接使用withColumn:
df.withColumn("ColName", col("colName").plus(1))