我们可以使用以下函数来转换单个整数值。
val x=100
Integer.toString(x, 16).toUpperCase
但是如何将其应用于整数列以生成带有十六进制字符串的新列?谢谢!
下面的方法不起作用。
testDF = testDF.withColumn("data_hex_string", Integer.toString(testDF("data"), 16).toUpperCase)
AFAIK,没有火花本机功能,所以做一个 udf 功能来做到这一点。
import org.apache.spark.sql.functions.udf
def toHex = udf((int: Int) => java.lang.Integer.toString(int, 16).toUpperCase)
df.withColumn("hex", toHex($"int")).show()
+---+---+---+
| id|int|hex|
+---+---+---+
| 1| 1| 1|
| 2| 11| B|
| 3| 23| 17|
+---+---+---+
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句