-
Notifications
You must be signed in to change notification settings - Fork 4.4k
[BEAM-11659] Add new schema types to Pub/Sub SQL #13980
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
Merged
TheNeuralBit
merged 12 commits into
apache:master
from
dpcollins-google:pubsub_byte_payload
Mar 17, 2021
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
cd38fb9
Add pubsub byte payload handling.
dpcollins-google 749f58c
Add tests
dpcollins-google fa3a904
reformat and fix CheckStyle
dpcollins-google bc9af83
Fix column name
dpcollins-google be060f3
ensure PubsubMessage has correct equals and hashCode implementation
dpcollins-google edd9e0c
Checkstyle
dpcollins-google ed8c8af
assorted fixes
dpcollins-google 337d866
Integration test fixes
dpcollins-google e721218
fix test
dpcollins-google 3ff089b
fix bytes equality check
dpcollins-google a38b840
fix non-serializability of message and byte field nullable
dpcollins-google e37fc55
Only build the serializer when required.
dpcollins-google File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
91 changes: 91 additions & 0 deletions
91
...le-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/pubsub/NestedRowToMessage.java
This file contains hidden or 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,91 @@ | ||
| /* | ||
| * 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.pubsub; | ||
|
|
||
| import static org.apache.beam.sdk.io.gcp.pubsub.PubsubMessageToRow.ATTRIBUTES_FIELD; | ||
| import static org.apache.beam.sdk.io.gcp.pubsub.PubsubMessageToRow.PAYLOAD_FIELD; | ||
| import static org.apache.beam.sdk.io.gcp.pubsub.PubsubSchemaIOProvider.ATTRIBUTE_ARRAY_FIELD_TYPE; | ||
| import static org.apache.beam.sdk.io.gcp.pubsub.PubsubSchemaIOProvider.ATTRIBUTE_MAP_FIELD_TYPE; | ||
| import static org.apache.beam.sdk.util.Preconditions.checkArgumentNotNull; | ||
| import static org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkArgument; | ||
|
|
||
| import java.util.Collection; | ||
| import java.util.Map; | ||
| import org.apache.beam.sdk.schemas.Schema; | ||
| import org.apache.beam.sdk.schemas.Schema.FieldType; | ||
| import org.apache.beam.sdk.schemas.Schema.TypeName; | ||
| import org.apache.beam.sdk.schemas.io.payloads.PayloadSerializer; | ||
| import org.apache.beam.sdk.transforms.SerializableFunction; | ||
| import org.apache.beam.sdk.transforms.SimpleFunction; | ||
| import org.apache.beam.sdk.values.Row; | ||
| import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap; | ||
|
|
||
| class NestedRowToMessage extends SimpleFunction<Row, PubsubMessage> { | ||
| private static final long serialVersionUID = 65176815766314684L; | ||
|
|
||
| private final PayloadSerializer serializer; | ||
| private final SerializableFunction<Row, Map<String, String>> attributesExtractor; | ||
| private final SerializableFunction<Row, byte[]> payloadExtractor; | ||
|
|
||
| @SuppressWarnings("methodref.receiver.bound.invalid") | ||
| NestedRowToMessage(PayloadSerializer serializer, Schema schema) { | ||
| this.serializer = serializer; | ||
| if (schema.getField(ATTRIBUTES_FIELD).getType().equals(ATTRIBUTE_MAP_FIELD_TYPE)) { | ||
| attributesExtractor = NestedRowToMessage::getAttributesFromMap; | ||
| } else { | ||
| checkArgument(schema.getField(ATTRIBUTES_FIELD).getType().equals(ATTRIBUTE_ARRAY_FIELD_TYPE)); | ||
| attributesExtractor = NestedRowToMessage::getAttributesFromArray; | ||
| } | ||
| if (schema.getField(PAYLOAD_FIELD).getType().equals(FieldType.BYTES)) { | ||
| payloadExtractor = NestedRowToMessage::getPayloadFromBytes; | ||
| } else { | ||
| checkArgument(schema.getField(PAYLOAD_FIELD).getType().getTypeName().equals(TypeName.ROW)); | ||
| payloadExtractor = this::getPayloadFromNested; | ||
| } | ||
| } | ||
|
|
||
| private static Map<String, String> getAttributesFromMap(Row row) { | ||
| return ImmutableMap.<String, String>builder() | ||
| .putAll(checkArgumentNotNull(row.getMap(ATTRIBUTES_FIELD))) | ||
| .build(); | ||
| } | ||
|
|
||
| private static Map<String, String> getAttributesFromArray(Row row) { | ||
| ImmutableMap.Builder<String, String> attributes = ImmutableMap.builder(); | ||
| Collection<Row> attributeEntries = checkArgumentNotNull(row.getArray(ATTRIBUTES_FIELD)); | ||
| for (Row entry : attributeEntries) { | ||
| attributes.put( | ||
| checkArgumentNotNull(entry.getString("key")), | ||
| checkArgumentNotNull(entry.getString("value"))); | ||
| } | ||
| return attributes.build(); | ||
| } | ||
|
|
||
| private static byte[] getPayloadFromBytes(Row row) { | ||
| return checkArgumentNotNull(row.getBytes(PAYLOAD_FIELD)); | ||
| } | ||
|
|
||
| private byte[] getPayloadFromNested(Row row) { | ||
| return serializer.serialize(checkArgumentNotNull(row.getRow(PAYLOAD_FIELD))); | ||
| } | ||
|
|
||
| @Override | ||
| public PubsubMessage apply(Row row) { | ||
| return new PubsubMessage(payloadExtractor.apply(row), attributesExtractor.apply(row)); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.