Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Java PubSub Read with dead letter topic pipeline example #55

Merged
merged 2 commits into from
Feb 28, 2024
Merged
Changes from 1 commit
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
75 changes: 75 additions & 0 deletions Java/src/main/java/pubsub/ReadWithDeadLetterTopicPubSub.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright 2024 Google LLC
*
* Licensed 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
*
* https://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 pubsub;

import org.apache.beam.sdk.Pipeline;
import org.apache.beam.sdk.coders.StringUtf8Coder;
import org.apache.beam.sdk.io.gcp.pubsub.PubsubIO;
import org.apache.beam.sdk.options.Description;
import org.apache.beam.sdk.options.PipelineOptions;
import org.apache.beam.sdk.options.PipelineOptionsFactory;
import org.apache.beam.sdk.options.Validation;
import org.apache.beam.sdk.transforms.SimpleFunction;
import org.apache.beam.sdk.values.TypeDescriptors;

import java.nio.charset.StandardCharsets;

public class ReadWithDeadLetterTopicPubSub {
liferoad marked this conversation as resolved.
Show resolved Hide resolved

public static final String ID_EXAMPLE_SUFFIX = "id=";

/** Pipeline options for read from Google Cloud Pub/Sub. */
public interface ReadPubSubOptions extends PipelineOptions {
@Description("Google Cloud Pub/Sub subscription to read from")
@Validation.Required
String getSubscription();

void setSubscription(String subscription);

@Description("Google Cloud Pub/Sub dead letter topic to write errors to")
@Validation.Required
String getDeadLetterTopic();

void setDeadLetterTopic(String deadLetterTopic);
}

public static void main(String[] args) {
ReadPubSubOptions options =
PipelineOptionsFactory.fromArgs(args).withValidation().as(ReadPubSubOptions.class);

Pipeline p = Pipeline.create(options);

p.apply(
"Read from Pub/Sub",
PubsubIO.readStrings()
.fromSubscription(options.getSubscription())
.withDeadLetterTopic(options.getDeadLetterTopic())
.withCoderAndParseFn(
StringUtf8Coder.of(),
SimpleFunction.fromSerializableFunctionWithOutputType(
message -> {
String str = new String(message.getPayload(), StandardCharsets.UTF_8);

//Example check if it is correct string
if (!str.contains(ID_EXAMPLE_SUFFIX)) {
throw new RuntimeException("This message will be delivered to dead letter topic");
}
return str;
},
TypeDescriptors.strings())));
p.run();
}
}