1

I have this field in my dataframe: myField: binary (nullable = false) (originally a Java byte[16]).

How do I select it and show it as a hex in scala from a spark shell? Please include necessary imports.

Marsellus Wallace
  • 17,991
  • 25
  • 90
  • 154

2 Answers2

1

Try

import org.apache.spark.sql.functions.{col, hex}

df.select(hex(col("myField")))
0

So I generalized @user6022341 solution to the following function

  def castBinaryToHexString(inputSrcDF: DataFrame):DataFrame={
    var srcDF = inputSrcDF

    // get all column names
    val allCols = srcDF.schema.fields.map(f => f.name.toLowerCase())

    // get all columns with data type = BinaryType
    val dateCols = srcDF.schema.fields.filter(f => f.dataType == BinaryType).map(f => f.name.toLowerCase())

    // cast all binary types to hex string
    srcDF = srcDF.select(allCols.map(x => if (dateCols.contains(x)) concat(lit("0x"),hex(col(x))).alias(x) else col(x) ): _*)

    return srcDF
  }
Matrix
  • 1,810
  • 1
  • 19
  • 20