1

I get bellow exception from my java codes.

 java.lang.ClassCastException: scala.collection.immutable.Map$Map1 cannot be cast to java.util.HashMap
    at au.com.vroc.udf.medianUDF.update(medianUDF.java:79)

I am getting error in my spark application when I cast the buffer to HashMap of java.utill. This is my codes:

    public void update(MutableAggregationBuffer buffer, Row input) {
    if (!input.isNullAt(0)) {   

        HashMap currentBuffer=(HashMap) buffer.get(0);//getting exception here
        //HashMap currentBuffer=new HashMap();  
        currentBuffer.put(input.getLong(0), input.getDouble(0));

        //currentBuffer.add(currentMap);

        buffer.update(0, currentBuffer);

    }
}

I guess instead of java hashmap I have to use "scala.collection.immutable.Map$Map1" inside my java class. Can I use any tool in "JavaConversions" namespace.

Anyhep would be appreciated!

0

2 Answers 2

2

Simplest approach would likely be to use Scala Converters.

It should look something like this (not tested, but type-checks):

import scala.collections.JavaConverters

java.util.Map currentBuffer = JavaConverters.mapAsJavaMapConverter(buffer.get(0)).asJava();

Please note that it returns type-parameterized map (i.e. java.util.Map<K, V>), not the non-parameterized java.util.HashMapin your example - you might want to alter the rest of your code to work on the parameterized maps for better type safety.

Sign up to request clarification or add additional context in comments.

1 Comment

Upvoted, but it is not necessary. There is getJavaMap method which applies conversions automatically.
1

You get java.util.Map you should use getJavaMap method:

java.util.Map<T, U> currentBuffer = (java.util.Map<T, U>) first.getJavaMap(0)

Note that this is not HashMap - initialized value is Encoded on update and decoded on get. To modify it, you have to make a copy.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.