forked from apache/beam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
contains.go
99 lines (82 loc) · 2.89 KB
/
contains.go
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
"flag"
"fmt"
"reflect"
"regexp"
"strings"
"github.com/apache/beam/sdks/go/pkg/beam"
"github.com/apache/beam/sdks/go/pkg/beam/io/textio"
"github.com/apache/beam/sdks/go/pkg/beam/log"
"github.com/apache/beam/sdks/go/pkg/beam/transforms/stats"
"github.com/apache/beam/sdks/go/pkg/beam/x/beamx"
"github.com/apache/beam/sdks/go/pkg/beam/x/debug"
)
// Options used purely at pipeline construction-time can just be flags.
var (
input = flag.String("input", "gs://apache-beam-samples/shakespeare/kinglear.txt", "File(s) to read.")
search = flag.String("search", "", "Only return words that contain this substring.")
)
func init() {
beam.RegisterType(reflect.TypeOf((*includeFn)(nil)).Elem())
}
// FilterWords returns PCollection<KV<word,count>> with (up to) 10 matching words.
func FilterWords(s beam.Scope, lines beam.PCollection) beam.PCollection {
s = s.Scope("FilterWords")
words := beam.ParDo(s, extractFn, lines)
filtered := beam.ParDo(s, &includeFn{Search: *search}, words)
counted := stats.Count(s, filtered)
return debug.Head(s, counted, 10)
}
var wordRE = regexp.MustCompile(`[a-zA-Z]+('[a-z])?`)
func extractFn(line string, emit func(string)) {
for _, w := range wordRE.FindAllString(line, -1) {
emit(w)
}
}
// includeFn outputs (word) iif the word contains substring Search.
type includeFn struct {
Search string `json:"search"`
}
func (f *includeFn) ProcessElement(s string, emit func(string)) {
if strings.Contains(s, f.Search) {
emit(s)
}
}
func formatFn(w string, c int) string {
return fmt.Sprintf("%s: %v", w, c)
}
func main() {
flag.Parse()
beam.Init()
ctx := context.Background()
if *search == "" {
log.Exit(ctx, "No search string provided. Use --search=foo")
}
log.Info(ctx, "Running contains")
// Construct a pipeline that only keeps 10 words that contain the provided search string.
p := beam.NewPipeline()
s := p.Root()
lines := textio.Read(s, *input)
filtered := FilterWords(s, lines)
formatted := beam.ParDo(s, formatFn, filtered)
debug.Print(s, formatted)
if err := beamx.Run(ctx, p); err != nil {
log.Exitf(ctx, "Failed to execute job: %v", err)
}
}