-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Add FileIO implementation for Google Cloud Storage #3711
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
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
103 changes: 103 additions & 0 deletions
103
gcp/src/main/java/org/apache/iceberg/gcp/GCPProperties.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,103 @@ | ||
| /* | ||
| * 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.iceberg.gcp; | ||
|
|
||
| import java.io.Serializable; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
|
|
||
| public class GCPProperties implements Serializable { | ||
| // Service Options | ||
| public static final String GCS_PROJECT_ID = "gcs.project-id"; | ||
| public static final String GCS_CLIENT_LIB_TOKEN = "gcs.client-lib-token"; | ||
| public static final String GCS_SERVICE_HOST = "gcs.service.host"; | ||
|
|
||
| // GCS Configuration Properties | ||
| public static final String GCS_DECRYPTION_KEY = "gcs.decryption-key"; | ||
| public static final String GCS_ENCRYPTION_KEY = "gcs.encryption-key"; | ||
| public static final String GCS_USER_PROJECT = "gcs.user-project"; | ||
|
|
||
| public static final String GCS_CHANNEL_READ_CHUNK_SIZE = "gcs.channel.read.chunk-size-bytes"; | ||
| public static final String GCS_CHANNEL_WRITE_CHUNK_SIZE = "gcs.channel.write.chunk-size-bytes"; | ||
|
|
||
| private String projectId; | ||
| private String clientLibToken; | ||
| private String serviceHost; | ||
|
|
||
| private String gcsDecryptionKey; | ||
| private String gcsEncryptionKey; | ||
| private String gcsUserProject; | ||
|
|
||
| private Integer gcsChannelReadChunkSize; | ||
| private Integer gcsChannelWriteChunkSize; | ||
|
|
||
| public GCPProperties() { | ||
| } | ||
|
|
||
| public GCPProperties(Map<String, String> properties) { | ||
| projectId = properties.get(GCS_PROJECT_ID); | ||
| clientLibToken = properties.get(GCS_CLIENT_LIB_TOKEN); | ||
| serviceHost = properties.get(GCS_SERVICE_HOST); | ||
|
|
||
| gcsDecryptionKey = properties.get(GCS_DECRYPTION_KEY); | ||
| gcsEncryptionKey = properties.get(GCS_ENCRYPTION_KEY); | ||
| gcsUserProject = properties.get(GCS_USER_PROJECT); | ||
|
|
||
| if (properties.containsKey(GCS_CHANNEL_READ_CHUNK_SIZE)) { | ||
| gcsChannelReadChunkSize = Integer.parseInt(properties.get(GCS_CHANNEL_READ_CHUNK_SIZE)); | ||
| } | ||
|
|
||
| if (properties.containsKey(GCS_CHANNEL_WRITE_CHUNK_SIZE)) { | ||
| gcsChannelWriteChunkSize = Integer.parseInt(properties.get(GCS_CHANNEL_WRITE_CHUNK_SIZE)); | ||
| } | ||
| } | ||
|
|
||
| public Optional<Integer> channelReadChunkSize() { | ||
| return Optional.ofNullable(gcsChannelReadChunkSize); | ||
| } | ||
|
|
||
| public Optional<Integer> channelWriteChunkSize() { | ||
| return Optional.ofNullable(gcsChannelWriteChunkSize); | ||
| } | ||
|
|
||
| public Optional<String> clientLibToken() { | ||
| return Optional.ofNullable(clientLibToken); | ||
| } | ||
|
|
||
| public Optional<String> decryptionKey() { | ||
| return Optional.ofNullable(gcsDecryptionKey); | ||
| } | ||
|
|
||
| public Optional<String> encryptionKey() { | ||
| return Optional.ofNullable(gcsEncryptionKey); | ||
| } | ||
|
|
||
| public Optional<String> projectId() { | ||
| return Optional.ofNullable(projectId); | ||
| } | ||
|
|
||
| public Optional<String> serviceHost() { | ||
| return Optional.ofNullable(serviceHost); | ||
| } | ||
|
|
||
| public Optional<String> userProject() { | ||
| return Optional.ofNullable(gcsUserProject); | ||
| } | ||
| } |
76 changes: 76 additions & 0 deletions
76
gcp/src/main/java/org/apache/iceberg/gcp/gcs/BaseGCSFile.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,76 @@ | ||
| /* | ||
| * 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.iceberg.gcp.gcs; | ||
|
|
||
| import com.google.cloud.storage.Blob; | ||
| import com.google.cloud.storage.BlobId; | ||
| import com.google.cloud.storage.Storage; | ||
| import java.net.URI; | ||
| import org.apache.iceberg.gcp.GCPProperties; | ||
|
|
||
| abstract class BaseGCSFile { | ||
| private final Storage storage; | ||
| private final GCPProperties gcpProperties; | ||
| private final BlobId blobId; | ||
| private Blob metadata; | ||
|
|
||
| BaseGCSFile(Storage storage, BlobId blobId, GCPProperties gcpProperties) { | ||
| this.storage = storage; | ||
| this.blobId = blobId; | ||
| this.gcpProperties = gcpProperties; | ||
| } | ||
|
|
||
| public String location() { | ||
| return blobId.toGsUtilUri(); | ||
| } | ||
|
|
||
| Storage storage() { | ||
| return storage; | ||
| } | ||
|
|
||
| URI uri() { | ||
| return URI.create(blobId.toGsUtilUri()); | ||
| } | ||
|
|
||
| BlobId blobId() { | ||
| return blobId; | ||
| } | ||
|
|
||
| protected GCPProperties gcpProperties() { | ||
| return gcpProperties; | ||
| } | ||
|
|
||
| public boolean exists() { | ||
| return getBlob() != null; | ||
| } | ||
|
|
||
| protected Blob getBlob() { | ||
| if (metadata == null) { | ||
| metadata = storage.get(blobId); | ||
| } | ||
|
|
||
| return metadata; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return blobId.toString(); | ||
| } | ||
| } |
125 changes: 125 additions & 0 deletions
125
gcp/src/main/java/org/apache/iceberg/gcp/gcs/GCSFileIO.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,125 @@ | ||
| /* | ||
| * 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.iceberg.gcp.gcs; | ||
|
|
||
| import com.google.cloud.storage.BlobId; | ||
| import com.google.cloud.storage.Storage; | ||
| import com.google.cloud.storage.StorageOptions; | ||
| import java.util.Map; | ||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
| import org.apache.iceberg.gcp.GCPProperties; | ||
| import org.apache.iceberg.io.FileIO; | ||
| import org.apache.iceberg.io.InputFile; | ||
| import org.apache.iceberg.io.OutputFile; | ||
| import org.apache.iceberg.util.SerializableSupplier; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| /** | ||
| * FileIO Implementation backed by Google Cloud Storage (GCS) | ||
| * <p> | ||
| * Locations follow the conventions used by | ||
| * {@link com.google.cloud.storage.BlobId#fromGsUtilUri(String) BlobId.fromGsUtilUri} | ||
|
danielcweeks marked this conversation as resolved.
|
||
| * that follow the convention <pre>{@code gs://<bucket>/<blob_path>}</pre> | ||
| * <p> | ||
| * See <a href="https://cloud.google.com/storage/docs/folders#overview">Cloud Storage Overview</a> | ||
| */ | ||
| public class GCSFileIO implements FileIO { | ||
| private static final Logger LOG = LoggerFactory.getLogger(GCSFileIO.class); | ||
|
|
||
| private SerializableSupplier<Storage> storageSupplier; | ||
| private GCPProperties gcpProperties; | ||
| private transient Storage storage; | ||
| private final AtomicBoolean isResourceClosed = new AtomicBoolean(false); | ||
|
|
||
| /** | ||
| * No-arg constructor to load the FileIO dynamically. | ||
| * <p> | ||
| * All fields are initialized by calling {@link GCSFileIO#initialize(Map)} later. | ||
| */ | ||
| public GCSFileIO() { | ||
| } | ||
|
|
||
| /** | ||
| * Constructor with custom storage supplier and GCP properties. | ||
| * <p> | ||
| * Calling {@link GCSFileIO#initialize(Map)} will overwrite information set in this constructor. | ||
| * | ||
| * @param storageSupplier storage supplier | ||
| * @param gcpProperties gcp properties | ||
| */ | ||
| public GCSFileIO(SerializableSupplier<Storage> storageSupplier, GCPProperties gcpProperties) { | ||
| this.storageSupplier = storageSupplier; | ||
| this.gcpProperties = gcpProperties; | ||
| } | ||
|
|
||
| @Override | ||
| public InputFile newInputFile(String path) { | ||
| return GCSInputFile.fromLocation(path, client(), gcpProperties); | ||
| } | ||
|
|
||
| @Override | ||
| public OutputFile newOutputFile(String path) { | ||
| return GCSOutputFile.fromLocation(path, client(), gcpProperties); | ||
| } | ||
|
|
||
| @Override | ||
| public void deleteFile(String path) { | ||
| // There is no specific contract about whether delete should fail | ||
| // and other FileIO providers ignore failure. Log the failure for | ||
| // now as it is not a required operation for Iceberg. | ||
| if (!client().delete(BlobId.fromGsUtilUri(path))) { | ||
|
danielcweeks marked this conversation as resolved.
|
||
| LOG.warn("Failed to delete path: {}", path); | ||
| } | ||
| } | ||
|
|
||
| private Storage client() { | ||
| if (storage == null) { | ||
| storage = storageSupplier.get(); | ||
| } | ||
| return storage; | ||
| } | ||
|
|
||
| @Override | ||
| public void initialize(Map<String, String> properties) { | ||
| this.gcpProperties = new GCPProperties(properties); | ||
|
|
||
| this.storageSupplier = () -> { | ||
| StorageOptions.Builder builder = StorageOptions.newBuilder(); | ||
|
|
||
| gcpProperties.projectId().ifPresent(builder::setProjectId); | ||
| gcpProperties.clientLibToken().ifPresent(builder::setClientLibToken); | ||
| gcpProperties.serviceHost().ifPresent(builder::setHost); | ||
|
|
||
| return builder.build().getService(); | ||
| }; | ||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
| // handles concurrent calls to close() | ||
| if (isResourceClosed.compareAndSet(false, true)) { | ||
| if (storage != null) { | ||
| // GCS Storage does not appear to be closable, so release the reference | ||
| storage = null; | ||
| } | ||
| } | ||
| } | ||
| } | ||
46 changes: 46 additions & 0 deletions
46
gcp/src/main/java/org/apache/iceberg/gcp/gcs/GCSInputFile.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,46 @@ | ||
| /* | ||
| * 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.iceberg.gcp.gcs; | ||
|
|
||
| import com.google.cloud.storage.BlobId; | ||
| import com.google.cloud.storage.Storage; | ||
| import org.apache.iceberg.gcp.GCPProperties; | ||
| import org.apache.iceberg.io.InputFile; | ||
| import org.apache.iceberg.io.SeekableInputStream; | ||
|
|
||
| public class GCSInputFile extends BaseGCSFile implements InputFile { | ||
| public static GCSInputFile fromLocation(String location, Storage storage, GCPProperties gcpProperties) { | ||
| return new GCSInputFile(storage, BlobId.fromGsUtilUri(location), gcpProperties); | ||
| } | ||
|
|
||
| GCSInputFile(Storage storage, BlobId blobId, GCPProperties gcpProperties) { | ||
| super(storage, blobId, gcpProperties); | ||
| } | ||
|
|
||
| @Override | ||
| public long getLength() { | ||
| return getBlob().getSize(); | ||
| } | ||
|
|
||
| @Override | ||
| public SeekableInputStream newStream() { | ||
| return new GCSInputStream(storage(), blobId(), gcpProperties()); | ||
| } | ||
| } |
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.