scalaapache-sparknumber-systems

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


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!


Solution

  • 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)