-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathBulkIteration.java
More file actions
51 lines (38 loc) · 1.39 KB
/
BulkIteration.java
File metadata and controls
51 lines (38 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package iterator;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.operators.DataSource;
import org.apache.flink.api.java.operators.IterativeDataSet;
import org.apache.flink.api.java.operators.MapOperator;
import java.util.List;
/**
* @author XINZE
*
* 使用 Bulk iterator API 实现圆周率的计算
*
*/
public class BulkIteration {
public static void main(String[] args) throws Exception {
ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
env.setParallelism(1);
DataSource<Integer> data = env.fromElements(0);
// 循环数据
IterativeDataSet<Integer> loop = data.iterate(1000);
// 计算过程
MapOperator<Integer, Integer> process = loop.map(new MapFunction<Integer, Integer>() {
@Override
public Integer map(Integer i) throws Exception {
double x = Math.random();
double y = Math.random();
int result = (x * x + y * y) < 1 ? 1 : 0;
return i + result;
}
});
// 使用 closeWith 调用计算过程
List<Integer> collect = loop.closeWith(process).collect();
// 输出最终结果
for (Integer i : collect) {
System.out.println( i / 1000.0 * 4);
}
}
}