0

I have found a solution to my problem here Create new column with function in Spark Dataframe

But i am having difficulty in converting the below code to Java since it's in Scala

import org.apache.spark.sql.functions._
val myDF = sqlContext.parquetFile("hdfs:/to/my/file.parquet")
val coder: (Int => String) = (arg: Int) => {if (arg < 100) "little" else "big"}
val sqlfunc = udf(coder)
myDF.withColumn("Code", sqlfunc(col("Amt")))

Can someone provide me the Java equivalent code for this?. I am stuck in converting below 2 lines

val coder: (Int => String) = (arg: Int) => {if (arg < 100) "little" else "big"}
val sqlfunc = udf(coder)

Thanks,

Community
  • 1
  • 1
serverliving.com
  • 437
  • 1
  • 6
  • 16

2 Answers2

2

Create your User Defined Function:

public class CodeUdf implements  UDF1<Integer, String>{
    @Override
    public String call(Integer integer) throws Exception {
        if(integer < 100)
            return "little";
        else
            return"big";
    }
}

Tell Spark about it

sqlContext.udf().register("Code", new CodeUdf(), DataTypes.IntegerType);

Use it in a select.

df.selectExpr("value", "Code(value)").show();
Michael Lloyd Lee mlk
  • 14,561
  • 3
  • 44
  • 81
-1
import org.apache.spark.sql.functions._
val myDF = sqlContext.parquetFile("hdfs:/to/my/file.parquet")
//val coder: (Int => String) = (arg: Int) => {if (arg < 100) "little" else "big"}
//val sqlfunc = udf(coder)
myDF.selectExpr("Code", "case when Amt < 100 'little' else 'big' end ")
Pankaj Arora
  • 544
  • 2
  • 6