dpcollins-google commented on a change in pull request #11919: URL: https://github.com/apache/beam/pull/11919#discussion_r443596917
########## File path: sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/pubsublite/CloudPubsubTransforms.java ########## @@ -0,0 +1,95 @@ +/* + * 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 org.apache.beam.sdk.io.gcp.pubsublite; + +import static com.google.cloud.pubsublite.cloudpubsub.MessageTransforms.fromCpsPublishTransformer; +import static com.google.cloud.pubsublite.cloudpubsub.MessageTransforms.toCpsPublishTransformer; +import static com.google.cloud.pubsublite.cloudpubsub.MessageTransforms.toCpsSubscribeTransformer; + +import com.google.cloud.pubsublite.Message; +import com.google.cloud.pubsublite.SequencedMessage; +import com.google.cloud.pubsublite.cloudpubsub.KeyExtractor; +import com.google.pubsub.v1.PubsubMessage; +import io.grpc.StatusException; +import org.apache.beam.sdk.transforms.DoFn; +import org.apache.beam.sdk.transforms.PTransform; +import org.apache.beam.sdk.transforms.ParDo; +import org.apache.beam.sdk.values.PCollection; + +// A class providing transforms between Cloud Pub/Sub and Pub/Sub Lite message types. +public final class CloudPubsubTransforms { + private CloudPubsubTransforms() {} + + // Transform a collection of SequencedMessages to Cloud Pub/Sub received PubsubMessages. + public static PTransform<PCollection<? extends SequencedMessage>, PCollection<PubsubMessage>> + toCpsSubscribeTransform() { + return ParDo.of( + new DoFn<SequencedMessage, PubsubMessage>() { + @ProcessElement + public void processElement( + @Element SequencedMessage sequencedMessage, OutputReceiver<PubsubMessage> output) + throws StatusException { + output.output(toCpsSubscribeTransformer().transform(sequencedMessage)); + } + }); + } + + // Transform a collection of Cloud Pub/Sub publishable PubsubMessages (ignoring message_id and + // publish_time) to Pub/Sub Lite Messages. + public static PTransform<PCollection<? extends PubsubMessage>, PCollection<Message>> + fromCpsPublishTransform() { + return ParDo.of( + new DoFn<PubsubMessage, Message>() { + @ProcessElement + public void processElement(@Element PubsubMessage message, OutputReceiver<Message> output) + throws StatusException { + output.output(fromCpsPublishTransformer(KeyExtractor.DEFAULT).transform(message)); + } + }); + } + + // Transform a collection of Pub/Sub Lite Messages to publishab Cloud Pub/Sub incomplete, + // publishable + // PubsubMessages. + public static PTransform<PCollection<? extends Message>, PCollection<PubsubMessage>> + toCpsPublishTransform() { + return ParDo.of( + new DoFn<Message, PubsubMessage>() { + @ProcessElement + public void processElement(@Element Message message, OutputReceiver<PubsubMessage> output) + throws StatusException { + output.output(toCpsPublishTransformer().transform(message)); + } + }); + } + + // Ensure that all messages that pass through can be converted to Cloud Pub/Sub messages using the + // standard transformation methods. Review comment: added more info. ########## File path: sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/pubsublite/PubsubLiteIO.java ########## @@ -0,0 +1,75 @@ +/* + * 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 org.apache.beam.sdk.io.gcp.pubsublite; + +import com.google.cloud.pubsublite.Message; +import com.google.cloud.pubsublite.SequencedMessage; +import org.apache.beam.sdk.annotations.Experimental; +import org.apache.beam.sdk.io.Read; +import org.apache.beam.sdk.transforms.PTransform; +import org.apache.beam.sdk.transforms.ParDo; +import org.apache.beam.sdk.transforms.SerializableFunction; +import org.apache.beam.sdk.values.PCollection; +import org.apache.beam.sdk.values.PDone; +import org.apache.beam.sdk.values.PInput; +import org.apache.beam.sdk.values.POutput; + +@Experimental +public final class PubsubLiteIO { + private PubsubLiteIO() {} + + private static <InT extends PInput, OutT extends POutput> PTransform<InT, OutT> toTransform( + SerializableFunction<InT, OutT> fn, String name) { + return new PTransform<InT, OutT>(name) { + @Override + public OutT expand(InT input) { + return fn.apply(input); + } + }; + } + + // Read messages from Pub/Sub Lite. These messages may contain duplicates if the publisher + // retried, which the PubsubLiteIO write method will do. Use the dedupe transform to remove these Review comment: obsolete. ########## File path: sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/pubsublite/CloudPubsubTransforms.java ########## @@ -0,0 +1,95 @@ +/* + * 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 org.apache.beam.sdk.io.gcp.pubsublite; + +import static com.google.cloud.pubsublite.cloudpubsub.MessageTransforms.fromCpsPublishTransformer; +import static com.google.cloud.pubsublite.cloudpubsub.MessageTransforms.toCpsPublishTransformer; +import static com.google.cloud.pubsublite.cloudpubsub.MessageTransforms.toCpsSubscribeTransformer; + +import com.google.cloud.pubsublite.Message; +import com.google.cloud.pubsublite.SequencedMessage; +import com.google.cloud.pubsublite.cloudpubsub.KeyExtractor; +import com.google.pubsub.v1.PubsubMessage; +import io.grpc.StatusException; +import org.apache.beam.sdk.transforms.DoFn; +import org.apache.beam.sdk.transforms.PTransform; +import org.apache.beam.sdk.transforms.ParDo; +import org.apache.beam.sdk.values.PCollection; + +// A class providing transforms between Cloud Pub/Sub and Pub/Sub Lite message types. +public final class CloudPubsubTransforms { + private CloudPubsubTransforms() {} + + // Transform a collection of SequencedMessages to Cloud Pub/Sub received PubsubMessages. + public static PTransform<PCollection<? extends SequencedMessage>, PCollection<PubsubMessage>> + toCpsSubscribeTransform() { + return ParDo.of( + new DoFn<SequencedMessage, PubsubMessage>() { + @ProcessElement + public void processElement( + @Element SequencedMessage sequencedMessage, OutputReceiver<PubsubMessage> output) + throws StatusException { + output.output(toCpsSubscribeTransformer().transform(sequencedMessage)); + } + }); + } + + // Transform a collection of Cloud Pub/Sub publishable PubsubMessages (ignoring message_id and Review comment: obsolete. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
