Matthias Scherer
unread,Nov 13, 2009, 9:45:16 AM11/13/09Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
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));
}
}