5

I noticed I can use CASE-THEN with Spark if I use an SQLContext and the .sql() function. Is there a way to use this in a JAVA syntax, directly on dataframes, too? How? Now, I write:

SparkConf sparkConf = new SparkConf();
JavaSparkContext ctx = new JavaSparkContext(sparkConf);
SQLContext sqlContext = new SQLContext(ctx);
DataFrame df = //some imported data 
df.registerTempTable("df");
sqlContext.sql("SELECT *use case-then in here* FROM df");

I'm looking for something like

df.select(case("this").then("that"));

1 Answer 1

8

Just import org.apache.spark.sql.functions then use when(Column col, Object obj).

import org.apache.spark.sql.functions;

df.select(functions.when(df.col("colName").equalTo("this"), "that").otherwise("something"));
Sign up to request clarification or add additional context in comments.

2 Comments

This is what I am currently doing, as I wrote in the question :) I left out the step of registering the temp table, sorry. Updated now. I'm interested whether this has a JAVA syntax equvalent. :)
Can you also chain these whens, like: df.select(functions.when(df.col("colName").equalTo("this"), "that"). when(df.col("colName").equalTo("somethingelse"), "dosomething").otherwise("something")); ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.