Search code examples
javascalaorc

Dynamic object creation in method/function arguments


I am generating orc files using the following

import org.apache.hadoop.io.Text;
import org.apache.hadoop.hive.ql.io.orc._;

val orcLine:OrcStruct = OrcUtils.createOrcStruct(
                        typeInfo,
                        new Text(value1),
                        new Text(value2),
                        new Text(value3),
                        new Text(value4),
                        new Text(value5));

However, the challenge here is sometimes I may have 5 values (value1 ... value5), sometimes 10 (value1 ... value10) and sometimes twenty (value1 ... value20) depends on certain conditions.

How would I be able to bring this dynamic behavior here?

Like if values are 10 I should have

val orcLine:OrcStruct = OrcUtils.createOrcStruct(
                        typeInfo,
                        new Text(value1),
                        new Text(value2),
                        new Text(value3),
                        new Text(value4),
                        new Text(value5),
                        new Text(value6),
                        new Text(value7),
                        new Text(value8),
                        new Text(value9),
                        new Text(value10));

I may also have sometimes

val orcLine:OrcStruct = OrcUtils.createOrcStruct(
                       typeInfo,
                       new Text(value1),
                       new ShortWritable(Short.valueOf(value2)),
                       new LongWritable(Long.valueOf(value3)),
                       new DoubleWritable(Double.valueOf(value4)),
                       new FloatWritable(Float.valueOf(value5)));

I am currently using Scala version 2.11.

Any help is much appreciated!


Solution

  • Let's say your parameters are stored in list:

    val params = List(
        new Text(value1),
        new ShortWritable(Short.valueOf(value2)),
        new LongWritable(Long.valueOf(value3)),
        new DoubleWritable(Double.valueOf(value4)),
        new FloatWritable(Float.valueOf(value5)))
    )
    

    As you probably noticed OrcUtils.createOrcStruct take variadic list of objects as second argument.

    In order to pass the list as varargs in scala you need to use :_*. It would "spread" your list as varargs during method call:

    OrcUtils.createOrcStruct(typeInfo, params:_*)