Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Update CloudPubsubChecks to CloudPubsubTransforms (#730)
* feat: Update CloudPubsubChecks to CloudPubsubTransforms Also fix concerns from apache/beam#14917 surrounding direct use of MapElements. Also add dependencyManagement section to beam io pom to fix upper bound deps issues. * fix: Add pubsub proto dep
- Loading branch information
1 parent
759a7f1
commit 2e10924
Showing
7 changed files
with
191 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- see http://www.mojohaus.org/clirr-maven-plugin/examples/ignored-differences.html --> | ||
<differences> | ||
<!-- TODO: Remove on next release --> | ||
<difference> | ||
<differenceType>8001</differenceType> | ||
<className>com/google/cloud/pubsublite/beam/CloudPubsubChecks</className> | ||
</difference> | ||
</differences> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 0 additions & 50 deletions
50
pubsublite-beam-io/src/main/java/com/google/cloud/pubsublite/beam/CloudPubsubChecks.java
This file was deleted.
Oops, something went wrong.
60 changes: 60 additions & 0 deletions
60
pubsublite-beam-io/src/main/java/com/google/cloud/pubsublite/beam/CloudPubsubMessages.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright 2021 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 | ||
* | ||
* 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 com.google.cloud.pubsublite.beam; | ||
|
||
import com.google.protobuf.ByteString; | ||
import java.util.Map; | ||
import org.apache.beam.sdk.io.gcp.pubsub.PubsubMessage; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* A class of utilities for transforming between Cloud Pub/Sub messages and the beam wrapper for | ||
* them. | ||
*/ | ||
class CloudPubsubMessages { | ||
private static final Logger LOG = LoggerFactory.getLogger(CloudPubsubMessages.class); | ||
|
||
private CloudPubsubMessages() {} | ||
|
||
static com.google.pubsub.v1.PubsubMessage toProto(PubsubMessage input) { | ||
com.google.pubsub.v1.PubsubMessage.Builder message = | ||
com.google.pubsub.v1.PubsubMessage.newBuilder() | ||
.setData(ByteString.copyFrom(input.getPayload())); | ||
// TODO(BEAM-8085) this should not be null | ||
Map<String, String> attributes = input.getAttributeMap(); | ||
if (attributes != null) { | ||
message.putAllAttributes(attributes); | ||
} | ||
String messageId = input.getMessageId(); | ||
if (messageId != null) { | ||
message.setMessageId(messageId); | ||
} | ||
return message.build(); | ||
} | ||
|
||
static PubsubMessage fromProto(com.google.pubsub.v1.PubsubMessage proto) { | ||
if (!proto.getOrderingKey().isEmpty()) { | ||
LOG.warn( | ||
"Dropping ordering key for message id `{}` with key `{}`.", | ||
proto.getMessageId(), | ||
proto.getOrderingKey()); | ||
} | ||
return new PubsubMessage( | ||
proto.getData().toByteArray(), proto.getAttributesMap(), proto.getMessageId()); | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
pubsublite-beam-io/src/main/java/com/google/cloud/pubsublite/beam/CloudPubsubTransforms.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Copyright 2020 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 | ||
* | ||
* 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 com.google.cloud.pubsublite.beam; | ||
|
||
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.proto.PubSubMessage; | ||
import com.google.cloud.pubsublite.proto.SequencedMessage; | ||
import org.apache.beam.sdk.io.gcp.pubsub.PubsubMessage; | ||
import org.apache.beam.sdk.transforms.MapElements; | ||
import org.apache.beam.sdk.transforms.PTransform; | ||
import org.apache.beam.sdk.values.PCollection; | ||
import org.apache.beam.sdk.values.TypeDescriptor; | ||
|
||
/** A class providing transforms between Cloud Pub/Sub and Pub/Sub Lite message types. */ | ||
public final class CloudPubsubTransforms { | ||
private CloudPubsubTransforms() {} | ||
/** | ||
* Ensure that all messages that pass through can be converted to Cloud Pub/Sub messages using the | ||
* standard transformation methods in the client library. | ||
* | ||
* <p>Will fail the pipeline if a message has multiple attributes per key. | ||
*/ | ||
public static PTransform<PCollection<PubSubMessage>, PCollection<PubSubMessage>> | ||
ensureUsableAsCloudPubsub() { | ||
return new PTransform<PCollection<PubSubMessage>, PCollection<PubSubMessage>>() { | ||
@Override | ||
public PCollection<PubSubMessage> expand(PCollection<PubSubMessage> input) { | ||
return input.apply( | ||
MapElements.into(TypeDescriptor.of(PubSubMessage.class)) | ||
.via( | ||
message -> { | ||
Object unused = | ||
toCpsPublishTransformer().transform(Message.fromProto(message)); | ||
return message; | ||
})); | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
* Transform messages read from Pub/Sub Lite to their equivalent Cloud Pub/Sub Message that would | ||
* have been read from PubsubIO. | ||
* | ||
* <p>Will fail the pipeline if a message has multiple attributes per map key. | ||
*/ | ||
public static PTransform<PCollection<SequencedMessage>, PCollection<PubsubMessage>> | ||
toCloudPubsubMessages() { | ||
return new PTransform<PCollection<SequencedMessage>, PCollection<PubsubMessage>>() { | ||
@Override | ||
public PCollection<PubsubMessage> expand(PCollection<SequencedMessage> input) { | ||
return input.apply( | ||
MapElements.into(TypeDescriptor.of(PubsubMessage.class)) | ||
.via( | ||
message -> | ||
CloudPubsubMessages.fromProto( | ||
toCpsSubscribeTransformer() | ||
.transform( | ||
com.google.cloud.pubsublite.SequencedMessage.fromProto( | ||
message))))); | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters