I'm going over examples on Spark RDDs and found the following snippet as an example:
JavaRDD distFile = sc.textFile("data.txt");
distFile.map(s -> s.toString().length()).reduce((a, b) -> a + b);
However, this code gives an error (straight from Netbeans):
bad operand types for binary operator '+'
first type: Object
second type: Object
Now, I fixed the problem by casting a and b to int (i.e. distFile.map(s -> s.toString().length()).reduce((a, b) -> (int) a + (int) b);), but I'm still wondering if there's a way to do this without casting or this is simply an error in their docs.
Thanks
Have you tried mapToInt It converts the stream to IntStream.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With