【问题标题】:Transform a dataframe for the minHashLSH in spark在 spark 中转换 minHashLSH 的数据帧
【发布时间】:2021-05-05 22:51:25
【问题描述】:

我有这个数据框:

val df = (
    spark
    .createDataFrame(
        Seq((1L, 2L), (1L, 5L), (1L,8L), (2L,4L), (2L,6L), (2L,8L))
    )
    .toDF("A","B")
    .groupBy("A")
    .agg(collect_list("B").alias("B"))
)

我想将其转换为以下形式:

val dfTransformed = 
(
    spark
    .createDataFrame(
        Seq(
            (1, Vectors.sparse(9, Seq((2, 1.0), (5,1.0), (8,1.0)))),
            (2, Vectors.sparse(9, Seq((4, 1.0), (6,1.0), (8,1.0))))
        )
    ).toDF("A", "B")
)

我想这样做以便我可以使用 MinHashLSH 转换 (https://spark.apache.org/docs/2.2.3/api/scala/index.html#org.apache.spark.ml.feature.MinHashLSH)。

我尝试了如下的 UDF,但没有成功:

def f(x:Array[Long]) = Vectors.sparse(9, x.map(p => (p.toInt,1.0)).toSeq)

val udff = udf((x:Array[Long]) => f(x))

val dfTransformed = df.withColumn("transformed", udff(col("B"))).show()

有人可以帮帮我吗?

【问题讨论】:

    标签: scala apache-spark user-defined-functions apache-spark-ml minhash


    【解决方案1】:

    对 UDF 使用 Seq,而不是 Array

    def f(x: Seq[Long]) = Vectors.sparse(9, x.map(p => (p.toInt,1.0)))
    
    val udff = udf((x: Seq[Long]) => f(x))
    
    val dfTransformed = df.withColumn("transformed", udff(col("B")))
    
    dfTransformed.show(false)
    +---+---------+-------------------------+
    |A  |B        |transformed              |
    +---+---------+-------------------------+
    |1  |[2, 5, 8]|(9,[2,5,8],[1.0,1.0,1.0])|
    |2  |[4, 6, 8]|(9,[4,6,8],[1.0,1.0,1.0])|
    +---+---------+-------------------------+
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-05
      • 2016-09-27
      相关资源
      最近更新 更多