How to use the prepare() method

34 views
Skip to first unread message

Matthias Scherer

unread,
Nov 13, 2009, 9:45:16 AM11/13/09
to cascadi...@googlegroups.com
Hi,

We are writing our first custom aggregator. Collecting the group values
by using a custom context class works fine. But we also want to
initialize some "global" context values in the prepare() method.

In the following example we tried to use the same context object as a
container for "global" values and for group values. The context object
is created in the prepare() method and should be reused in the start(),
aggregate() and complete() methods.

When we run the aggregator, we get an NullPointerException (at
TGPAggregation.MyAggregator.start(Unknown Source)). When we add a call
to create a new context object in the start() method, the program runs,
but we cannot access the "global" value which we set in the prepare()
method.

Regards
Matthias


public class MyAggregator extends BaseOperation<MyAggregator.Context>
implements Aggregator<MyAggregator.Context>
{

class Context
{
String globalResource = "initial global value";
String groupResource = "initial group value";
}


public MyAggregator()
{
super();
}


public void start(FlowProcess flowProcess, AggregatorCall<Context>
aggregatorCall)
{
// We assume that the context object has already been created in the
prepare() method
Context context = (Context) aggregatorCall.getContext();
context.groupResource = "initial value";
}


public void aggregate(FlowProcess flowProcess, AggregatorCall<Context>
aggregatorCall)
{
Context context = (Context) aggregatorCall.getContext();
// get the argument tuple
TupleEntry arguments = aggregatorCall.getArguments();
// do some aggregations
context.groupResource = "aggregated value";
}


public void prepare(FlowProcess flowProcess, AggregatorCall<Context>
aggregatorCall)
{
aggregatorCall.setContext(new Context());
Context context = (Context) aggregatorCall.getContext();

context.globalResource = "global value";
}


public void complete(FlowProcess flowProcess, AggregatorCall<Context>
aggregatorCall)
{
Context context = (Context) aggregatorCall.getContext();

// create a TupleEntry to hold our result values
Fields fields = new Fields("GROUP_RESOURCE", "GLOBAL_RESOURCE");
Tuple result = new Tuple();
result.add(context.groupResource);
result.add(context.globalResource);
// return the result Tuple
aggregatorCall.getOutputCollector().add(new
TupleEntry(fields,result));
}

}

Chris K Wensel

unread,
Nov 13, 2009, 7:00:18 PM11/13/09
to cascadi...@googlegroups.com
Let me see if I can repro this and get a fix.

ckw
> --
>
> You received this message because you are subscribed to the Google
> Groups "cascading-user" group.
> To post to this group, send email to cascadi...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/cascading-user?hl=
> .
>
>

--
Chris K Wensel
ch...@concurrentinc.com
http://www.concurrentinc.com

Chris K Wensel

unread,
Nov 14, 2009, 12:07:39 PM11/14/09
to cascadi...@googlegroups.com
I can't seem to reproduce this.

Can you test your Aggregator in CascadingTestCase#invokeAggregator()
and see if you get the same issues?

ckw

On Nov 13, 2009, at 6:45 AM, Matthias Scherer wrote:

Reply all
Reply to author
Forward
0 new messages