我使用了大多数网站中存在的这个例子。
int letterCnt = G.grid().reduce(
GridClosureCallMode.SPREAD,
F.yield("Counting Letters In This Phrase".split(" "),
new C1<String, Integer>() {
@Override public Integer apply(String word) {
return word.length();
}
}
),
F.sumIntReducer()
);
第一个参数表示工作负荷的平均分配(我认为它更像循环基础) 第二个参数具有将在所有已发现节点中执行的代码 第三个接收来自apply()的所有结果数据,这些数据在不同的节点和相应的进程中执行。
我想知道是否可以用我们自己的函数替换第三个参数F.sumIntReducer()。如果是的话,我想看一个例子。 假设创建具有与F.sumIntReducer()相同功能的相同函数(即,求和不同节点找到的所有长度)。
答案 0 :(得分:0)
是的,您可以定义自己的自定义减速器。示例是here。
答案 1 :(得分:0)
reduce方法具有全局输入和输出数据类型。其结构示意如下:
1 resultValue = grid.reduce(
2 job distribution mode,
3 executable logic function,
4 split function,
5 collect function);
简单的例子:(不仅仅使用网格作为内存操作而不是cpu密集型,所以不要期望改进单次执行)
private static int countLettersReducer(String phrase) throws GridException {
// final GridLogger log = grid.log();
int letterCount = 0;
// Execute Hello World task.
try {
@SuppressWarnings("unchecked")
int letterCnt =
grid.reduce(GridClosureCallMode.BALANCE,
// input: string
// output: integer
new GridClosure<String, Integer>() {
private static final long serialVersionUID = 1L;
// Create executable logic block for a job part
@Override
public Integer apply(String word) {
// Print out a given word, just so we can
// see which node is doing what.
// System.out.println(">>> Calculating for word: " + word);
// Return the length of a given word, i.e. number of
// letters.
return word.length();
}
},
// split tasks for single jobs according to this function
// split at linebreaks
Arrays.asList(phrase.split("\n")),
// Collection of words.
// Collect the results from each job of the nodes
//input and output is integer
new GridReducer<Integer, Integer>() {
/**
*
*/
private static final long serialVersionUID = 1L;
private int sum;
@Override
public boolean collect(Integer res) {
sum += res;
return true; // True means continue collecting until
// last result.
}
// return the collected results
@Override
public Integer apply() {
return sum;
}
});
letterCount = letterCnt;
} catch (Exception e) {
}
return letterCount;
}