[英]How to Append Two Spark Dataframes with different columns in Java
I have One Dataframe on which I am performing one UDF operation and then operation of UDF gives only one column in Dataframe.我有一个数据框,我在它上面执行一个 UDF 操作,然后 UDF 的操作在数据框中只给出一列。 How i can append it to previous Datafram.
我如何将它附加到以前的数据帧。 Example: Dataframe 1: sr_no , name, salary
示例:数据框 1: sr_no ,姓名,薪水
Dataframe 2: UDF is giving output as ABS(Salary) - only one column as output from UDF applied on Dataframe1数据帧 2:UDF 将输出作为 ABS(Salary) - 只有一列作为应用于 Dataframe1 的 UDF 的输出
How i can have output dataframe as Dataframe1 + Dataframe2 in JAVA ie sr_no, name, salary, ABS(Salary) output我如何在 JAVA 中将数据帧输出为 Dataframe1 + Dataframe2,即 sr_no、姓名、薪水、ABS(薪水)输出
Looks like you are searching for .withColumn
method:看起来您正在搜索
.withColumn
方法:
df1.withColumn("ABS(salary)", yourUdf.apply(col("salary")))
(Snippet requires to import static method col
from org.apache.spark.sql.functions
) (代码段需要从
org.apache.spark.sql.functions
导入静态方法col
)
Got the ans.得到答案。 Just do it like this : df= df.selectExpr("*","ABS(salary)");
就这样做: df= df.selectExpr("*","ABS(salary)"); This will give you output of udf with your entire dataframe.
这将为您提供带有整个数据帧的 udf 输出。 Else it will give only one column.
否则它只会给出一列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.