-
Notifications
You must be signed in to change notification settings - Fork 0
/
SerializablePlayerExample.java
69 lines (60 loc) · 1.79 KB
/
SerializablePlayerExample.java
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
68
69
import java.io.Serializable;
import java.util.Objects;
import javax.annotation.Nullable;
import org.apache.beam.sdk.Pipeline;
import org.apache.beam.sdk.options.PipelineOptions;
import org.apache.beam.sdk.options.PipelineOptionsFactory;
import org.apache.beam.sdk.transforms.Create;
import org.apache.beam.sdk.transforms.MapElements;
import org.apache.beam.sdk.values.TypeDescriptors;
public class SerializablePlayerExample {
static class Player implements Serializable {
@Nullable
String name;
@Nullable
Integer scores;
public Player() { }
public Player(String name, Integer scores) {
this.name = name;
this.scores = scores;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Player)) {
return false;
}
Player player = (Player) o;
if (!Objects.equals(name, player.name)) {
return false;
}
return Objects.equals(scores, player.scores);
}
@Override
public int hashCode() {
int result = name != null ? name.hashCode() : 0;
result = 31 * result + (scores != null ? scores.hashCode() : 0);
return result;
}
}
public static void main(String[] args) {
PipelineOptions options =
PipelineOptionsFactory.fromArgs(args).withValidation().create();
Pipeline p = Pipeline.create(options);
p
.apply("Create", Create.of(
new Player("owl", 22),
new Player("finch", 95),
new Player("kestrel", 100))
)
.apply("Print", MapElements.into(TypeDescriptors.nulls()).via(
(Player player) -> {
System.out.println(player.name + ": " + player.scores);
return null;
}))
;
p.run().waitUntilFinish();
}
}