blob: 7a56d0b75b2db2b7c3eb19d3373004e9e8a520db [file] [log] [blame] [view]
Prompt:
Write a sample Java code snippet that writes data to a Google Pub/Sub topic using Apache Beam.
Response:
Your Apache Beam pipeline can write data to a Google Pub/Sub topic using the Apache Beam PubSubIO connector.
Here is an example of how to use the PubSubIO connector to accomplish this:
```java
package pubsub;
import org.apache.beam.sdk.Pipeline;
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.transforms.Create;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Arrays;
import java.util.List;
// Pipeline to write data to a Google Pub/Sub topic
public class WritePubSubTopic {
private static final Logger LOG = LoggerFactory.getLogger(WritePubSubTopic.class);
// Pipeline options to configure the pipeline
public interface WritePubSubTopicOptions extends PipelineOptions {
@Description("PubSub topic name to write to")
String getTopicName();
void setTopicName(String value);
}
public static void main(String[] args) {
// Parse the pipeline options from the command line
WritePubSubTopicOptions options = PipelineOptionsFactory.fromArgs(args).withValidation().as(WritePubSubTopicOptions.class);
// Create a pipeline
Pipeline p = Pipeline.create(options);
// Sample messages to write to the Pub/Sub topic
final List<String> messages = Arrays.asList(
"PubSub message 1",
"PubSub message 2",
"PubSub message 3"
);
p
// Create a list of messages to write to the Pub/Sub topic
.apply(Create.of(messages))
// Write the messages to the Pub/Sub topic
.apply(PubsubIO.writeStrings().to(options.getTopicName()));
// Execute the pipeline
p.run();
}
}
```
This code snippet utilizes the pipeline options pattern to parse command-line arguments.