Reduce return type

#1

I have a simple reduce method returning a double result, defined as this:

function reduce(outKey, interValues, job) {
    var aggregateConsumption = 0
    interValues.each( function(c) { aggregateConsumption += c } )
    return aggregateConsumption;
}

corresponding to a MapReduceJob defined as:

/**
* Map reduce job that returns the aggregate power consumption per manufacturer
*/
entity type AggregateConsumptionByManufacturerJob extends MapReduce<SmartBulb, string, double, double> type key 'AGG_CNS_MNFCT' {
 /**
 * Start of the evaluation interval
 */
 startDate: datetime
 /**
 * End of the evaluation interval
 */
 endDate: datetime
 /**
 * Metric evaluation interval
 */
 interval: string
 /**
 * Map function
 */
 map: ~ js server
 /**
 * Reduce function
 */
 reduce: ~ js server
}

When I run the job, it passes the map step and fails on the reduce step with the following error:

errorLog: c3.love.exceptions.C3RuntimeException: Unable to convert JavaScript java.lang.Double value to expected [double] at c3.engine.action.rhino.RhinoConversions$1.otherValue(RhinoConversions.java:494) at c3.engine.action.rhino.RhinoConversions$1.arry(RhinoConversions.java:279) at c3.love.typesys.ValueCoercer.coerce(ValueCoercer.java:177) at c3.engine.action.rhino.RhinoConversions.jsToJava(RhinoConversions.java:99) at c3.engine.action.RhinoActionEngine.executeInt(RhinoActionEngine.java:190) at c3.engine.action.javascript.JavaScriptActionEngine.execute(JavaScriptActionEngine.java:223) at c3.server.impl.Task.doFilter(Task.java:251) at c3.server.impl.ServerDispatcherBase$ActionFilterChainImpl.doFilter(ServerDispatcherBase.java:198) at c3.server.impl.dataCache.DataCacheEngine.doFilter(DataCacheEngine.java:125) at c3.server.impl.ServerDispatcherBase$ActionFilterChainImpl.doFilter(ServerDispatcherBase.java:196) at c3.server.impl.ServerDispatcherBase.doFilter(ServerDispatcherBase.java:166) at c3.server.impl.InteractiveDispatcher.doFilter(InteractiveDispatcher.java:81) at c3.server.impl.Task.run(Task.java:187) at c3.server.impl.InteractiveDispatcher.dispatch(InteractiveDispatcher.java:400) at c3.server.impl.InteractiveDispatcher.dispatch(InteractiveDispatcher.java:306) at c3.love.C3.dispatch(C3.java:593) at c3.love.C3.dispatchWithArgs(C3.java:665) at c3.love.C3.dispatch(C3.java:638) at c3.love.C3.dispatch(C3.java:634) at c3.type.mapr.MapReduce$Subclass.reduce(MapReduce.java:734) at c3.engine.database.async.MapReduceMethods.dispatchReduce(MapReduceMethods.java:723) at c3.type.mapr.methods.MapReduceMethodsBase$7.accept(MapReduceMethodsBase.java:98) at c3.type.mapr.methods.MapReduceMethodsBase$7.accept(MapReduceMethodsBase.java:96) at c3.server.engine.TypeSysEngine.execute(TypeSysEngine.java:67) at c3.server.impl.Task.doFilter(Task.java:251) at c3.server.impl.ServerDispatcherBase$ActionFilterChainImpl.doFilter(ServerDispatcherBase.java:198) at c3.server.impl.dataCache.DataCacheEngine.doFilter(DataCacheEngine.java:125) at c3.server.impl.ServerDispatcherBase$ActionFilterChainImpl.doFilter(ServerDispatcherBase.java:196) at c3.server.impl.ServerDispatcherBase.doFilter(ServerDispatcherBase.java:166) at c3.server.impl.InteractiveDispatcher.doFilter(InteractiveDispatcher.java:81) at c3.server.impl.Task.run(Task.java:187) at c3.server.impl.InteractiveDispatcher.dispatch(InteractiveDispatcher.java:400) at c3.server.impl.InteractiveDispatcher.dispatch(InteractiveDispatcher.java:306) at c3.love.C3.dispatch(C3.java:593) at c3.love.C3.dispatchWithArgs(C3.java:665) at c3.love.C3.dispatch(C3.java:638) at c3.love.C3.dispatch(C3.java:634) at c3.type.mapr.MapReduce$Subclass.dispatchReduce(MapReduce.java:773) at c3.engine.database.async.MapReduceQueueMethods.compute(MapReduceQueueMethods.java:61) at c3.type.async.methods.MapReduceQueueMethodsBase$1.accept(MapReduceQueueMethodsBase.java:39) at c3.type.async.methods.MapReduceQueueMethodsBase$1.accept(MapReduceQueueMethodsBase.java:37) at c3.server.engine.TypeSysEngine.execute(TypeSysEngine.java:67) at c3.server.impl.Task.doFilter(Task.java:251) at c3.server.impl.ServerDispatcherBase$ActionFilterChainImpl.doFilter(ServerDispatcherBase.java:198) at c3.server.impl.dataCache.DataCacheEngine.doFilter(DataCacheEngine.java:125) at c3.server.impl.ServerDispatcherBase$ActionFilterChainImpl.doFilter(ServerDispatcherBase.java:196) at c3.server.impl.ServerDispatcherBase.doFilter(ServerDispatcherBase.java:166) at c3.server.impl.InteractiveDispatcher.doFilter(InteractiveDispatcher.java:81) at c3.server.impl.Task.run(Task.java:187) at c3.server.impl.InteractiveDispatcher.dispatch(InteractiveDispatcher.java:400) at c3.server.impl.InteractiveDispatcher.dispatch(InteractiveDispatcher.java:306) at c3.love.C3.dispatch(C3.java:593) at c3.engine.database.async.InvalidationQueueMethods.compute(InvalidationQueueMethods.java:1240) at c3.engine.database.async.InvalidationQueueMethods.doDispatchCompute(InvalidationQueueMethods.java:397) at c3.engine.database.async.InvalidationQueueMethods.dispatchCompute(InvalidationQueueMethods.java:360) at c3.type.async.methods.InvalidationQueueMethodsBase$5.accept(InvalidationQueueMethodsBase.java:93) at c3.type.async.methods.InvalidationQueueMethodsBase$5.accept(InvalidationQueueMethodsBase.java:91) at c3.server.engine.TypeSysEngine.execute(TypeSysEngine.java:67) at c3.server.impl.Task.doFilter(Task.java:251) at c3.server.impl.ServerDispatcherBase$ActionFilterChainImpl.doFilter(ServerDispatcherBase.java:198) at c3.server.impl.dataCache.DataCacheEngine.doFilter(DataCacheEngine.java:125) at c3.server.impl.ServerDispatcherBase$ActionFilterChainImpl.doFilter(ServerDispatcherBase.java:196) at c3.server.impl.ServerDispatcherBase.doFilter(ServerDispatcherBase.java:166) at c3.server.impl.InteractiveDispatcher.doFilter(InteractiveDispatcher.java:81) at c3.server.impl.Task.run(Task.java:187) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745)

Any idea on what is wrong?

#2

Unable to convert JavaScript java.lang.Double value to expected [double]

This error message is fairly clear, a double cannot be converted to an array of doubles.

The return type for MapReduce.reduce is [OutValType]. I am guessing the OutValType for your MapReduce job type is double, so reduce should return an array of doubles.

1 Like
#3

Thank you @matt , I didn’t notice the meaning of [double] as an array of double.
Do you know you know how to return and array as expected?

#4

Change

return aggregateConsumption;

to

return [aggregateConsumption];
1 Like
#5

Great; I though it was necessary to use C3.typesys.Arry.

#6

If you are on a very old server version that may be necessary. In newer versions (since 7.0?) the platform knows how to auto-convert from JS arrays to C3 arrays and visa versa (maps and other common data structures as well).

1 Like