简体   繁体   中英

Spark SQL - Select all AND computed columns?

This is a total noob question, sorry for that. In Spark, I can use select as:

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!

只需这样做:

df.select(df.col("*"), df.col("colName").plus(1));

You can use withColumn() method, this will create a new column to the DataFrame.

df.select("*")
  .withColumn("ColName", col("colName").plus(1))

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.

You can directly use withColumn :

df.withColumn("ColName", col("colName").plus(1))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM