How to call Groovy or Scala UDF to update Oracle, using Java?

106 Views Asked by At

I have a JDBC connection to an Oracle DB. I also have some function f(x) written in Groovy or Scala. For example, f(x) simply returns 2x.

Now my question is: how should I call this f(x) in my Java code, to apply f(x) to all values in a column, and update this column to 2x in the above example?

1

There are 1 best solutions below

2
Ramesh Maharjan On

You can use withColumn api to update the column's values.

If your function is returning a constant lit should be a good choice. You can find a lot of functions too.

You can use udf function too as

def func = udf((column: Int)=> 2*column)
dataframe.withColumn("column1", func(dataframe("column1")))

but udf function would need serialization and deserialization column values. So if other functions should be preferred.