-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublish.go
51 lines (40 loc) · 1.13 KB
/
publish.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
package pubsubio
import (
"context"
"cloud.google.com/go/pubsub"
"github.com/apache/beam/sdks/v2/go/pkg/beam"
"github.com/apache/beam/sdks/v2/go/pkg/beam/register"
)
func init() {
register.DoFn2x1[context.Context, []byte, *pubsub.PublishResult](&Publisher{})
}
type PubsubResultError struct {
MessageID string
ErrorMessage string
}
func Publish(s beam.Scope, project string, topic string, batchSize int, targetData beam.PCollection) beam.PCollection {
s = s.Scope("arqbeam.pubsubio.Publish")
return beam.ParDo(s, &Publisher{
Project: project,
Topic: topic,
BatchSize: batchSize,
}, targetData)
}
type Publisher struct {
Project string
Topic string
BatchSize int
topic *pubsub.Topic
}
func (fn *Publisher) Setup(ctx context.Context) {
pubSubClient, err := pubsub.NewClient(ctx, fn.Project)
if err != nil {
panic(err)
}
pubSubTopic := pubSubClient.Topic(fn.Topic)
pubSubTopic.PublishSettings.CountThreshold = fn.BatchSize
fn.topic = pubSubTopic
}
func (fn *Publisher) ProcessElement(ctx context.Context, value []byte) *pubsub.PublishResult {
return fn.topic.Publish(ctx, &pubsub.Message{Data: value})
}