-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_03_average_numbers.java
More file actions
67 lines (57 loc) · 2.13 KB
/
code_03_average_numbers.java
File metadata and controls
67 lines (57 loc) · 2.13 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package com.usecases;
import org.apache.beam.sdk.Pipeline;
import org.apache.beam.sdk.transforms.MapElements;
import org.apache.beam.sdk.values.PCollection;
import org.apache.beam.sdk.transforms.Create;
import org.apache.beam.sdk.transforms.Mean;
import org.apache.beam.sdk.values.TypeDescriptors;
public class code_03_average_numbers {
public static void main(String args[]) {
Pipeline pipeline = Pipeline.create();
PCollection<Integer> values = pipeline.apply(Create.of(1,2,3,4,5,6,7,8,9,10));
PCollection<Double> result = values.apply(Mean.globally());
result.apply(MapElements
.into(TypeDescriptors.doubles())
.via((Double value) -> {
System.out.println(value);
return value;
})
);
pipeline.run();
}
}
//import org.apache.beam.sdk.Pipeline;
//import org.apache.beam.sdk.transforms.Combine;
//import org.apache.beam.sdk.transforms.MapElements;
//import org.apache.beam.sdk.transforms.SimpleFunction;
//import org.apache.beam.sdk.values.PCollection;
//import org.apache.beam.sdk.values.TypeDescriptor;
//
//public class AverageCalculator {
// public static void main(String[] args) {
// Pipeline pipeline = Pipeline.create();
//
// PCollection<Integer> numbers = pipeline.apply(Create.of(1, 2, 3, 4, 5));
//
// PCollection<Double> average = numbers.apply(Combine.globally((Iterable<Integer> input) -> {
// int sum = 0;
// int count = 0;
// for (Integer number : input) {
// sum += number;
// count++;
// }
// return sum / (double) count;
// }));
//
// average.apply(MapElements.into(TypeDescriptor.of(String.class))
// .via((Double avg) -> "Average: " + avg))
// .apply(MapElements.via(new SimpleFunction<String, Void>() {
// public Void apply(String input) {
// System.out.println(input);
// return null;
// }
// }));
//
// pipeline.run();
// }
//}