How to convert a column from hex string to long?

后端 未结 2 2009
眼角桃花
眼角桃花 2021-01-15 03:17

I have a DataFrame with Icao column with hex codes that I would like to convert to Long datatype. How could I do this in Spark SQL?



        
相关标签:
2条回答
  • 2021-01-15 03:52

    You can use java hex to Long converter

    java.lang.Long.parseLong(hex.trim(), 16)
    

    All you need is to define a udf function as below

    import org.apache.spark.sql.functions.udf
    def hexToLong = udf((hex: String) => java.lang.Long.parseLong(hex.trim(), 16))
    

    And call the udf function using .withColumn api

    df.withColumn("Icao", hexToLong($"Icao")).show(false)
    
    0 讨论(0)
  • 2021-01-15 03:54

    TL;DR Use conv standard function.

    conv(num: Column, fromBase: Int, toBase: Int): Column Convert a number in a string column from one base to another.

    With conv a solution could be as follows:

    scala> icao.show
    +------+-----+
    |  Icao|count|
    +------+-----+
    |471F8D|81350|
    |471F58|79634|
    |471F56|79112|
    |471F86|78177|
    |471F8B|75300|
    |47340D|75293|
    |471F83|74864|
    |471F57|73815|
    |471F4A|72290|
    |471F5F|72133|
    |40612C|69676|
    +------+-----+
    
    // conv is not available by default unless you're in spark-shell
    import org.apache.spark.sql.functions.conv
    
    val s1 = icao.withColumn("conv", conv($"Icao", 16, 10))
    scala> s1.show
    +------+-----+-------+
    |  Icao|count|   conv|
    +------+-----+-------+
    |471F8D|81350|4661133|
    |471F58|79634|4661080|
    |471F56|79112|4661078|
    |471F86|78177|4661126|
    |471F8B|75300|4661131|
    |47340D|75293|4666381|
    |471F83|74864|4661123|
    |471F57|73815|4661079|
    |471F4A|72290|4661066|
    |471F5F|72133|4661087|
    |40612C|69676|4219180|
    +------+-----+-------+
    

    conv has a feature of giving you a result of the type of the input column, so I started with strings and got strings.

    scala> s1.printSchema
    root
     |-- Icao: string (nullable = true)
     |-- count: string (nullable = true)
     |-- conv: string (nullable = true)
    

    If I had used ints I'd have got ints.

    You could cast the result of conv using another built-in method cast (or start with a proper type of the input column).

    val s2 = icao.withColumn("conv", conv($"Icao", 16, 10) cast "long")
    scala> s2.printSchema
    root
     |-- Icao: string (nullable = true)
     |-- count: string (nullable = true)
     |-- conv: long (nullable = true)
    
    scala> s2.show
    +------+-----+-------+
    |  Icao|count|   conv|
    +------+-----+-------+
    |471F8D|81350|4661133|
    |471F58|79634|4661080|
    |471F56|79112|4661078|
    |471F86|78177|4661126|
    |471F8B|75300|4661131|
    |47340D|75293|4666381|
    |471F83|74864|4661123|
    |471F57|73815|4661079|
    |471F4A|72290|4661066|
    |471F5F|72133|4661087|
    |40612C|69676|4219180|
    +------+-----+-------+
    
    0 讨论(0)
提交回复
热议问题