kruparulz14 kruparulz14 - 3 months ago 46
Scala Question

WrappedArray to Array in HiveQL

I'm using

collect_list
to group some column like:

val res = hiveContext.sql("SELECT date, time, collect_list(id) AS id_list FROM table1 GROUP BY date, time")


The
id_list
returns as a
WrappedArray
:

WrappedArray(1,2,1,2)
WrappedArray(4,3,4)
WrappedArray(6,7,6,7,6)


However I'm passing the
id_list
into
myFunc
that takes an
Array[Double]
as input:

def myFunc(xs: Array[Double]) {...}


My question is how can I call
myFunc
correctly to parse the
id_list
. I'm having something like:

res.collect.foreach(x => myFunc(x(2)))


but it's giving me an
type mismatch; found : Any required: Array[Double]
error.

What is the correct way to implicitly convert the
WrappedArray
into an
Array
or how can I call
myFunc
in an optimized way?

Thanks!

Answer

The main question here is rather to explicitly convert Any to WrappedArray[Double] that you may do as:

x(2).asInstanceOf[mutable.WrappedArray[Double]].toArray

in your case. Or you may use pattern matching for type conversion that you may refer How to use a Scala match expression instead of isInstanceOf (to match types) for details.