gpt4 book ai didi

scala - 如何迭代scalawrappedArray? ( Spark )

转载 作者:行者123 更新时间:2023-12-02 04:25:59 26 4
gpt4 key购买 nike

我执行以下操作:

val tempDict = sqlContext.sql("select words.pName_token,collect_set(words.pID) as docids 
from words
group by words.pName_token").toDF()

val wordDocs = tempDict.filter(newDict("pName_token")===word)

val listDocs = wordDocs.map(t => t(1)).collect()

listDocs: Array

[Any] = Array(WrappedArray(123, 234, 205876618, 456))

我的问题是如何迭代这个包装数组或将其转换为列表?

我为 listDocs 获得的选项是 applyasInstanceOfcloneisInstanceOflengthtoStringupdate

我该如何继续?

最佳答案

这是解决此问题的一种方法。

import org.apache.spark.sql.Row
import org.apache.spark.sql.functions._
import scala.collection.mutable.WrappedArray

val data = Seq((Seq(1,2,3),Seq(4,5,6),Seq(7,8,9)))
val df = sqlContext.createDataFrame(data)
val first = df.first

// use a pattern match to deferral the type
val mapped = first.getAs[WrappedArray[Int]](0)

// now we can use it like normal collection
mapped.mkString("\n")

// get rows where has array
val rows = df.collect.map {
case Row(a: Seq[Any], b: Seq[Any], c: Seq[Any]) =>
(a, b, c)
}
rows.mkString("\n")

关于scala - 如何迭代scalawrappedArray? ( Spark ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38257630/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com