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
|
package main
import (
"context"
"fmt"
"strconv"
"goflink/core"
"goflink/runtime"
"goflink/transport"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
source := make(chan core.StreamRecord[int])
mid := make(chan core.StreamRecord[int])
sink := make(chan core.StreamRecord[string])
// x*10, then render as text
mapped := runtime.RunTask(ctx,
transport.NewLocalReceiveChannel(source),
transport.NewLocalEmitChannel(mid),
core.NewMapOperator(func(in int) (int, error) { return in * 10, nil }),
)
printed := runtime.RunTask(ctx,
transport.NewLocalReceiveChannel(mid),
transport.NewLocalEmitChannel(sink),
core.NewMapOperator(func(in int) (string, error) { return "v=" + strconv.Itoa(in), nil }),
)
go func() {
defer close(source)
for i := 1; i <= 5; i++ {
source <- core.NewDataRecord("k", i, 0)
}
}()
for record := range sink {
fmt.Println(record.Value)
}
for _, errCh := range []<-chan error{mapped, printed} {
if err := <-errCh; err != nil {
fmt.Println("task failed:", err)
}
}
}
|