【问题标题】:Impala vs SparkSQL: built-in function translation: fnv_hashImpala vs SparkSQL:内置函数翻译:fnv_hash
【发布时间】:2017-01-25 18:26:25
【问题描述】:

我在 Impala 中使用 fnv_hash 将一些字符串值转换为数字。现在我正在迁移到 Spark SQL,我可以使用 Spark SQL 中的类似功能吗?一个几乎 1-1 的函数将字符串值映射到数字应该可以工作。谢谢!

【问题讨论】:

    标签: apache-spark pyspark apache-spark-sql impala


    【解决方案1】:

    很遗憾,Spark 不提供直接替换。虽然内置 o.a.s.sql.functions.hash / pyspark.sql.functions.hash 使用 MurmurHash 3、which should have comparable properties with the same hash size,但 Spark 使用 32 位哈希(与 Impala 中的 64 位 fnv_hash 相比)。如果这是可以接受的,只需导入 hash 就可以了:

    from pyspark.sql.functions import hash as hash_
    
    df = sc.parallelize([("foo", ), ("bar", )]).toDF(["foo"])
    
    df.select(hash_("foo"))
    
    DataFrame[hash(foo): int]
    

    如果您需要更大,可以查看XXH64。它不是直接使用 SQL 函数公开的,但 Catalyst expression 是公共的,因此您只需要一个简单的包装器。大致是这样的:

    package com.example.spark.sql
    
    import org.apache.spark.sql.Column
    import org.apache.spark.sql.catalyst.expressions.XxHash64
    
    object functions {
      def xxhash64(cols: Column*): Column = new Column(
        new XxHash64(cols.map(_.expr))
      )
    }
    
    from pyspark import SparkContext
    from pyspark.sql.column import Column, _to_java_column, _to_seq
    
    def xxhash64(*cols):
        sc = SparkContext._active_spark_context
        jc = sc._jvm.com.example.spark.sql.functions.xxhash64(
            _to_seq(sc, cols, _to_java_column)
        )
        return Column(jc)
    
    df.select(xxhash64("foo"))
    
    DataFrame[xxHash(foo): bigint]
    

    【讨论】:

      猜你喜欢
      • 2017-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-11
      相关资源
      最近更新 更多