Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select constant values from Dataframe coding in Java

I have a dataframe df1 with fixed number of columns. I have applied an inner join with another dataframe df2.

However, while the select is written, I need to select some constant numbers which I am currently unable to.

I have read examples in scala but corresponding java isn't working.

df1.join(df2).filter(df1.col("a1").$eq$eq$eq(df2.col("a1")))
.select(df1.col("a1"), df1.col("a2"), df2.col("a2"), 8)

Suggest a way to select 8 as in above example.

I am aware of withColumn api as well but not sure about the implementation.

Thanks.

like image 957
Kanav Sharma Avatar asked May 02 '16 06:05

Kanav Sharma


1 Answers

This should work.

val joinedDF = df1.join(df2).filter(df1.col("a1").$eq$eq$eq(df2.col("a1")))
.select(df1.col("a1"), df1.col("a2"), df2.col("a2")).withColumn("constant", lit(8))

That is if you want to add an integer. If you want to add a constant string, use this after select

.withColumn("constantString",lit("some_string"))
like image 128
dheee Avatar answered Oct 04 '22 03:10

dheee