代码之家  ›  专栏  ›  技术社区  ›  Galuoises

在spark中转换minHashLHS的数据帧

  •  0
  • Galuoises  · 技术社区  · 5 年前

    我有这个数据帧:

    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()
    

    有人能帮帮我吗?

    0 回复  |  直到 5 年前
        1
  •  1
  •   mck    5 年前

    使用 Seq 对于UDF,不是 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])|
    +---+---------+-------------------------+
    
    推荐文章