2016-07-08 3 views
6

나는 다음과 같은 작업을 수행합니다 :이 포장 배열을 반복하거나 목록에이 변환 어떻게scala wrappedArray를 반복하는 방법은 무엇입니까? (스파크)

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 어떻게 진행합니까?

답변

6

여기에서이 문제를 해결하는 방법 중 하나가 있습니다. 지금 ** 발 arrDocs = listDocs (0) 발 온도 = arrDocs.asInstanceOf [mutable.WrappedArray [긴]] ** 온도 :

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") 
+0

는 사실은 내가 이런 짓이 내 사건을 해결하는 것 기본적으로 반복자를 제공합니다. – boY

+0

감사합니다. @boY, 답변을 업데이트했습니다. 이전 하나는 약간 장황했다. –

+0

WrappedArray에 문제가있어서 Seq [Int]로 바꿀 수있었습니다. – jspooner