Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
## Breaking Changes

* X behavior was changed ([#X](https://github.com/apache/beam/issues/X)).
* Go: The pubsubio.Read transform now accepts ReadOptions as a value type instead of a pointer, and requires exactly one of Topic or Subscription to be set (they are mutually exclusive). Additionally, the ReadOptions struct now includes a Topic field for specifying the topic directly, replacing the previous topic parameter in the Read function signature ([#35369])(https://github.com/apache/beam/pull/35369).

## Deprecations

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ When writing data to Kafka using Apache Beam, it is important to ensure that the
For detailed [information](https://beam.apache.org/releases/javadoc/2.0.0/org/apache/beam/sdk/io/kafka/KafkaIO.html)
{{if (eq .Sdk "go")}}
```
data := pubsubio.Read(s, "pubsub-public-data", "taxirides-realtime", nil)
data := pubsubio.Read(s, "pubsub-public-data", pubsubio.ReadOptions{Topic: "taxirides-realtime"})
kvData := beam.ParDo(s, func(elm []byte) ([]byte, []byte) { return []byte(""), elm }, data)
windowed := beam.WindowInto(s, window.NewFixedWindows(15*time.Second), kvData)
kafkaio.Write(s, *expansionAddr, *bootstrapServers, *topic, windowed)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func main() {

// In the main function, the code creates a Beam pipeline, reads from the Pub/Sub source, transforms the data into a key-value pair, applies a windowing function to the data, and writes the windowed data to a Kafka topic.

data := pubsubio.Read(s, "pubsub-public-data", "taxirides-realtime", nil)
data := pubsubio.Read(s, "pubsub-public-data", pubsubio.ReadOptions{Topic: "taxirides-realtime"})
kvData := beam.ParDo(s, func(elm []byte) ([]byte, []byte) { return []byte(""), elm }, data)
windowed := beam.WindowInto(s, window.NewFixedWindows(15*time.Second), kvData)
kafkaio.Write(s, *expansionAddr, *bootstrapServers, *topic, windowed)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,9 @@ func main() {
// PubSub notifications will be emitted containing the path of the resource once
// it is written to the store. Simultaneously read notifications and resources
// from PubSub and store, respectively.
resourceNotifications := pubsubio.Read(s, *gcpopts.Project, *pubsubTopic, nil)
resourceNotifications := pubsubio.Read(s, *gcpopts.Project, pubsubio.ReadOptions{
Topic: *pubsubTopic,
})
resourcesInFhirStore, deadLetters := fhirio.Read(s, resourceNotifications)

// Log the read resources or read errors to the server.
Expand Down
5 changes: 4 additions & 1 deletion sdks/go/examples/kafka/taxi.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,10 @@ func main() {
s := p.Root()

// Read from Pubsub and write to Kafka.
data := pubsubio.Read(s, "pubsub-public-data", "taxirides-realtime", nil)
opts := pubsubio.ReadOptions{
Topic: "taxirides-realtime",
}
data := pubsubio.Read(s, "pubsub-public-data", opts)
kvData := beam.ParDo(s, func(elm []byte) ([]byte, []byte) { return []byte(""), elm }, data)
windowed := beam.WindowInto(s, window.NewFixedWindows(15*time.Second), kvData)
kafkaio.Write(s, *expansionAddr, *bootstrapServers, *topic, windowed)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ func main() {
_, err = pubsubx.EnsureTopic(ctx, client, inputTopic)
fatalf(err, "Failed to ensure topic: %v", err)

source := pubsubio.Read(s, project, inputTopic, nil)
source := pubsubio.Read(s, project, pubsubio.ReadOptions{
Topic: inputTopic,
})
keyedSource := beam.AddFixedKey(s, source) // simulate keyed data by adding a fixed key
mainInput := beam.WindowInto(
s,
Expand Down
2 changes: 1 addition & 1 deletion sdks/go/examples/streaming_wordcap/wordcap.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func main() {
p := beam.NewPipeline()
s := p.Root()

col := pubsubio.Read(s, project, *input, &pubsubio.ReadOptions{Subscription: sub.ID()})
col := pubsubio.Read(s, project, pubsubio.ReadOptions{Subscription: sub.ID()})
str := beam.ParDo(s, func(b []byte) string {
return (string)(b)
}, col)
Expand Down
44 changes: 30 additions & 14 deletions sdks/go/pkg/beam/io/pubsubio/pubsubio.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,32 +52,48 @@ func init() {

// ReadOptions represents options for reading from PubSub.
type ReadOptions struct {
Subscription string
Topic string // Topic sets the topic to be read from. A new subscription will be generated for the job. Mutually exclusive with setting a Subscription.
Subscription string // Subscription sets the name of an existing subscription to read from. Mutually exclusive with setting a Topic.
IDAttribute string
TimestampAttribute string
WithAttributes bool
}

// Read reads an unbounded number of PubSubMessages from the given
// pubsub topic. It produces an unbounded PCollecton<*PubSubMessage>,
// pubsub topic or subscription. It produces an unbounded PCollecton<*PubSubMessage>,
// if WithAttributes is set, or an unbounded PCollection<[]byte>.
func Read(s beam.Scope, project, topic string, opts *ReadOptions) beam.PCollection {
//
// The topic or subscription is required and must be set with ReadOptions.
func Read(s beam.Scope, project string, opts ReadOptions) beam.PCollection {
s = s.Scope("pubsubio.Read")

payload := &pipepb.PubSubReadPayload{
Topic: pubsubx.MakeQualifiedTopicName(project, topic),
// Validate: only one of Topic or Subscription should be set
if (opts.Topic == "" && opts.Subscription == "") || (opts.Topic != "" && opts.Subscription != "") {
panic("Exactly one of Topic or Subscription must be set in ReadOptions")
}
if opts != nil {
payload.IdAttribute = opts.IDAttribute
payload.TimestampAttribute = opts.TimestampAttribute
if opts.Subscription != "" {
payload.Subscription = pubsubx.MakeQualifiedSubscriptionName(project, opts.Subscription)
}
payload.WithAttributes = opts.WithAttributes

payload := &pipepb.PubSubReadPayload{}

if opts.Topic != "" {
payload.Topic = pubsubx.MakeQualifiedTopicName(project, opts.Topic)
} else {
payload.Subscription = pubsubx.MakeQualifiedSubscriptionName(project, opts.Subscription)
}

out := beam.External(s, readURN, protox.MustEncode(payload), nil, []beam.FullType{typex.New(reflectx.ByteSlice)}, false)
if opts != nil && opts.WithAttributes {
payload.IdAttribute = opts.IDAttribute
payload.TimestampAttribute = opts.TimestampAttribute
payload.WithAttributes = opts.WithAttributes

out := beam.External(
s,
readURN,
protox.MustEncode(payload),
nil,
[]beam.FullType{typex.New(reflectx.ByteSlice)},
false,
)

if opts.WithAttributes {
return beam.ParDo(s, unmarshalMessageFn, out[0])
}
return out[0]
Expand Down
56 changes: 56 additions & 0 deletions sdks/go/pkg/beam/io/pubsubio/pubsubio_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// 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 pubsubio

import (
"github.com/apache/beam/sdks/v2/go/pkg/beam"
"testing"
)

func TestRead_BothTopicAndSubscriptionPanics(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Fatal("expected panic when both topic and subscription are set")
}
}()

beam.Init()

p := beam.NewPipeline()
s := p.Root()

opts := ReadOptions{
Topic: "topic",
Subscription: "sub",
}
Read(s, "test-project", opts)
}

func TestRead_NeitherTopicNorSubscriptionPanics(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Fatal("expected panic when neither topic nor subscription is set")
}
}()

beam.Init()

p := beam.NewPipeline()
s := p.Root()

opts := ReadOptions{}
Read(s, "test-project", opts)
}
Loading