Converting a column from decimal to binary string in Spark / Scala

9.1k Views Asked by At

I have a Spark dataframe with decimal column. I want to convert this column to a binary string. Are there any function for this can anybody help?

Thank you!

2

There are 2 best solutions below

0
On BEST ANSWER

There is a bin inbuilt function which states

An expression that returns the string representation of the binary value of the given long column. For example, bin("12") returns "1100".

So if you have a dataframe as

+-----+
|Value|
+-----+
|4    |
+-----+

root
 |-- Value: decimal(10,0) (nullable = true)

You can use bin function as

import org.apache.spark.sql.functions._
data.withColumn("Value_Binary", bin(col("Value")))

which should give you

+-----+------------+
|Value|Value_Binary|
+-----+------------+
|4    |100         |
+-----+------------+

root
 |-- Value: decimal(10,0) (nullable = true)
 |-- Binary_value: string (nullable = true)
1
On

I solved this issue with creating a user defined function.

val toBinStr: Int => String = _.toBinaryString

import org.apache.spark.sql.functions.udf
val toBinStrUDF = udf(toBinStr)

// Apply the UDF to change the source dataset
data.withColumn("Value_Binary", toBinStrUDF($"Value")).show