-
Notifications
You must be signed in to change notification settings - Fork 3.8k
add 'prefixes' support to google input source #8930
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
clintropolis
merged 11 commits into
apache:master
from
clintropolis:google-input-source-list-objects
Dec 5, 2019
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
0b0e0d9
add prefixes support to google input source, making it symmetrical-is…
clintropolis 62b814a
docs
clintropolis 4e5c12e
Merge remote-tracking branch 'upstream/master' into google-input-sour…
clintropolis fcb8192
more better, and tests
clintropolis b69319d
unused
clintropolis 932c906
formatting
clintropolis ff836ff
javadoc
clintropolis abebe26
dependencies
clintropolis f410493
oops
clintropolis 716d60c
review comments
clintropolis 495ee05
better javadoc
clintropolis 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
181 changes: 181 additions & 0 deletions
181
core/src/main/java/org/apache/druid/data/input/impl/CloudObjectInputSource.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,181 @@ | ||
| /* | ||
| * 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.druid.data.input.impl; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||
| import com.google.common.primitives.Ints; | ||
| import org.apache.druid.data.input.AbstractInputSource; | ||
| import org.apache.druid.data.input.InputEntity; | ||
| import org.apache.druid.data.input.InputFormat; | ||
| import org.apache.druid.data.input.InputRowSchema; | ||
| import org.apache.druid.data.input.InputSourceReader; | ||
| import org.apache.druid.data.input.InputSplit; | ||
| import org.apache.druid.data.input.SplitHintSpec; | ||
| import org.apache.druid.utils.CollectionUtils; | ||
|
|
||
| import javax.annotation.Nullable; | ||
| import java.io.File; | ||
| import java.net.URI; | ||
| import java.util.List; | ||
| import java.util.Objects; | ||
| import java.util.stream.Stream; | ||
|
|
||
| public abstract class CloudObjectInputSource<T extends InputEntity> extends AbstractInputSource | ||
| implements SplittableInputSource<CloudObjectLocation> | ||
| { | ||
| private final List<URI> uris; | ||
| private final List<URI> prefixes; | ||
| private final List<CloudObjectLocation> objects; | ||
|
|
||
| public CloudObjectInputSource( | ||
| String scheme, | ||
| @Nullable List<URI> uris, | ||
| @Nullable List<URI> prefixes, | ||
| @Nullable List<CloudObjectLocation> objects | ||
| ) | ||
| { | ||
| this.uris = uris; | ||
| this.prefixes = prefixes; | ||
| this.objects = objects; | ||
|
|
||
| if (!CollectionUtils.isNullOrEmpty(objects)) { | ||
| throwIfIllegalArgs(!CollectionUtils.isNullOrEmpty(uris) || !CollectionUtils.isNullOrEmpty(prefixes)); | ||
| } else if (!CollectionUtils.isNullOrEmpty(uris)) { | ||
| throwIfIllegalArgs(!CollectionUtils.isNullOrEmpty(prefixes)); | ||
| uris.forEach(uri -> CloudObjectLocation.validateUriScheme(scheme, uri)); | ||
| } else if (!CollectionUtils.isNullOrEmpty(prefixes)) { | ||
| prefixes.forEach(uri -> CloudObjectLocation.validateUriScheme(scheme, uri)); | ||
| } else { | ||
| throwIfIllegalArgs(true); | ||
| } | ||
| } | ||
|
|
||
| @JsonProperty | ||
| public List<URI> getUris() | ||
| { | ||
| return uris; | ||
| } | ||
|
|
||
| @JsonProperty | ||
| public List<URI> getPrefixes() | ||
| { | ||
| return prefixes; | ||
| } | ||
|
|
||
| @Nullable | ||
| @JsonProperty | ||
| public List<CloudObjectLocation> getObjects() | ||
| { | ||
| return objects; | ||
| } | ||
|
|
||
| /** | ||
| * Create the correct {@link InputEntity} for this input source given a split on a {@link CloudObjectLocation}. This | ||
| * is called internally by {@link #formattableReader} and operates on the output of {@link #createSplits}. | ||
| */ | ||
| protected abstract T createEntity(InputSplit<CloudObjectLocation> split); | ||
|
|
||
| /** | ||
| * Create a stream of {@link CloudObjectLocation} splits by listing objects that appear under {@link #prefixes} using | ||
| * this input sources backend API. This is called internally by {@link #createSplits} and {@link #estimateNumSplits}, | ||
| * only if {@link #prefixes} is set, otherwise the splits are created directly from {@link #uris} or {@link #objects}. | ||
| * Calling if {@link #prefixes} is not set is likely to either lead to an empty iterator or null pointer exception. | ||
| */ | ||
| protected abstract Stream<InputSplit<CloudObjectLocation>> getPrefixesSplitStream(); | ||
|
|
||
| @Override | ||
| public Stream<InputSplit<CloudObjectLocation>> createSplits( | ||
| InputFormat inputFormat, | ||
| @Nullable SplitHintSpec splitHintSpec | ||
| ) | ||
| { | ||
| if (!CollectionUtils.isNullOrEmpty(objects)) { | ||
| return objects.stream().map(InputSplit::new); | ||
| } | ||
| if (!CollectionUtils.isNullOrEmpty(uris)) { | ||
| return uris.stream().map(CloudObjectLocation::new).map(InputSplit::new); | ||
| } | ||
|
|
||
| return getPrefixesSplitStream(); | ||
| } | ||
|
|
||
| @Override | ||
| public int estimateNumSplits(InputFormat inputFormat, @Nullable SplitHintSpec splitHintSpec) | ||
| { | ||
| if (!CollectionUtils.isNullOrEmpty(objects)) { | ||
| return objects.size(); | ||
| } | ||
|
|
||
| if (!CollectionUtils.isNullOrEmpty(uris)) { | ||
| return uris.size(); | ||
| } | ||
|
|
||
| return Ints.checkedCast(getPrefixesSplitStream().count()); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean needsFormat() | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| protected InputSourceReader formattableReader( | ||
| InputRowSchema inputRowSchema, | ||
| InputFormat inputFormat, | ||
| @Nullable File temporaryDirectory | ||
| ) | ||
| { | ||
| return new InputEntityIteratingReader( | ||
| inputRowSchema, | ||
| inputFormat, | ||
| createSplits(inputFormat, null).map(this::createEntity), | ||
| temporaryDirectory | ||
| ); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) | ||
| { | ||
| if (this == o) { | ||
| return true; | ||
| } | ||
| if (o == null || getClass() != o.getClass()) { | ||
| return false; | ||
| } | ||
| CloudObjectInputSource that = (CloudObjectInputSource) o; | ||
| return Objects.equals(uris, that.uris) && | ||
| Objects.equals(prefixes, that.prefixes) && | ||
| Objects.equals(objects, that.objects); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() | ||
| { | ||
| return Objects.hash(uris, prefixes, objects); | ||
| } | ||
|
|
||
| private void throwIfIllegalArgs(boolean clause) throws IllegalArgumentException | ||
| { | ||
| if (clause) { | ||
| throw new IllegalArgumentException("exactly one of either uris or prefixes or objects must be specified"); | ||
| } | ||
| } | ||
| } | ||
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add javadocs for the new protected methods?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens if prefixes aren't specified for the input source?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated javadoc to mention that this method is called internally by
createSplitsandestimateNumSplits