2021SC@SDUSC
public interface CustomStreamGrouping extends Serializable {
/**
* Tells the stream grouping at runtime the tasks in the target bolt. This information should be used in chooseTasks to determine the
* target tasks.
*
* It also tells the grouping the metadata on the stream this grouping will be used on.
*/
void prepare(WorkerTopologyContext context, GlobalStreamId stream, List<Integer> targetTasks);
/**
* This function implements a custom stream grouping. It takes in as input the number of tasks in the target bolt in prepare and returns
* the tasks to send the tuples to.
*
* @param values the values to group on
*/
List<Integer> chooseTasks(int taskId, List<Object> values);
}
实现backtype.storm.grouping.CustomStreamGrouping接口即可完成用户自定义Grouping
例如:单次计数按照第一单词的第一个字母mod task数的余数来分配
package CostumerGroup;
import backtype.storm.grouping.CustomStreamGrouping;
import backtype.storm.task.TopologyContext;
import backtype.storm.tuple.Fields;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
/**
* Created by hjw on 17/5/26.
*/
public class ModuleGrouping implements CustomStreamGrouping {//,Serializable
int numTasks = 0;
private List<Integer> targetTasks;
@Override
public void prepare(TopologyContext topologyContext, Fields fields, List<Integer> targetTasks) {
numTasks = targetTasks.size();
this.targetTasks = targetTasks;
}
@Override
public List<Integer> chooseTasks(List<Object> values) {
List<Integer> boltIDs = new ArrayList<Integer>();
if(values.size() >0 ){
String str = values.get(0).toString();
if (str.isEmpty())
boltIDs.add(targetTasks.get(0));
else
boltIDs.add(targetTasks.get((int)(str.charAt(0))%numTasks));//根据余数分配
}
return boltIDs;
}
}
builder.setBolt("word-normalizer", new WordNormalizer()).
customGrouping("word-reader", new ModuleGrouping());