Skip to content

Commit 041d5d4

Browse files
committed
Modify the base class to use GCS client instead of GCS FileSystems. This is a more lightweight dependency for the expansion service.
1 parent 2f1fafe commit 041d5d4

4 files changed

Lines changed: 29 additions & 442 deletions

File tree

sdks/java/extensions/kafka-factories/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ dependencies {
3232
implementation 'com.google.cloud:google-cloud-secretmanager:2.72.0'
3333
implementation library.java.slf4j_api
3434
implementation library.java.vendored_guava_32_1_2_jre
35-
35+
implementation 'com.google.cloud:google-cloud-storage:2.57.0'
3636
// ------------------------- TEST DEPENDENCIES -------------------------
3737
testImplementation 'org.apache.kafka:kafka-clients:3.9.0'
3838
testImplementation library.java.junit

sdks/java/extensions/kafka-factories/src/main/java/org/apache/beam/sdk/extensions/kafka/factories/FileAwareFactoryFn.java

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,21 @@
2020
import com.google.cloud.secretmanager.v1.AccessSecretVersionResponse;
2121
import com.google.cloud.secretmanager.v1.SecretManagerServiceClient;
2222
import com.google.cloud.secretmanager.v1.SecretVersionName;
23-
import java.io.File;
23+
import com.google.cloud.storage.Blob;
24+
import com.google.cloud.storage.BlobId;
25+
import com.google.cloud.storage.Storage;
26+
import com.google.cloud.storage.StorageException;
27+
import com.google.cloud.storage.StorageOptions;
2428
import java.io.IOException;
25-
import java.nio.channels.FileChannel;
26-
import java.nio.channels.ReadableByteChannel;
2729
import java.nio.charset.StandardCharsets;
2830
import java.nio.file.Files;
2931
import java.nio.file.Path;
3032
import java.nio.file.Paths;
31-
import java.nio.file.StandardOpenOption;
3233
import java.util.HashMap;
33-
import java.util.HashSet;
3434
import java.util.Map;
35-
import java.util.Set;
3635
import java.util.concurrent.ConcurrentHashMap;
3736
import java.util.regex.Matcher;
3837
import java.util.regex.Pattern;
39-
import org.apache.beam.sdk.io.FileSystems;
4038
import org.apache.beam.sdk.transforms.SerializableFunction;
4139
import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Preconditions;
4240
import org.slf4j.Logger;
@@ -141,27 +139,33 @@ public T apply(Map<String, Object> config) {
141139
*/
142140
protected static synchronized String downloadGcsFile(String gcsFilePath, String outputFileString)
143141
throws IOException {
144-
// create the file only if it doesn't exist
145-
if (!new File(outputFileString).exists()) {
146-
Path outputFilePath = Paths.get(outputFileString);
147-
Path parentDir = outputFilePath.getParent();
148-
if (parentDir != null) {
149-
Files.createDirectories(parentDir);
142+
Path outputFilePath = Paths.get(outputFileString);
143+
// 1. Check if the file already exists to avoid doing duplicate work
144+
if (Files.exists(outputFilePath)) {
145+
System.out.println("File " + outputFileString + " already exists, skipping download.");
146+
return outputFileString;
147+
}
148+
if (outputFilePath.getParent() != null) {
149+
Files.createDirectories(outputFilePath.getParent());
150+
}
151+
152+
try {
153+
BlobId blobId = BlobId.fromGsUtilUri(gcsFilePath);
154+
Storage storage = StorageOptions.getDefaultInstance().getService();
155+
Blob blob = storage.get(blobId);
156+
if (blob == null) {
157+
throw new IOException("File not found at GCS path: " + gcsFilePath);
150158
}
151159

152-
LOG.info("Staging GCS file [{}] to [{}]", gcsFilePath, outputFileString);
153-
Set<StandardOpenOption> options = new HashSet<>(2);
154-
options.add(StandardOpenOption.CREATE);
155-
options.add(StandardOpenOption.WRITE);
160+
// 2. Download the blob's content to the specified local file
161+
blob.downloadTo(outputFilePath);
156162

157-
// Copy the GCS file into a local file and will throw an I/O exception in case file not found.
158-
try (ReadableByteChannel readerChannel =
159-
FileSystems.open(FileSystems.matchSingleFileSpec(gcsFilePath).resourceId())) {
160-
try (FileChannel writeChannel = FileChannel.open(outputFilePath, options)) {
161-
writeChannel.transferFrom(readerChannel, 0, Long.MAX_VALUE);
162-
}
163-
}
163+
System.out.println("Downloaded " + gcsFilePath + " to " + outputFileString);
164+
} catch (StorageException e) {
165+
// Catch the StorageException and re-throw as a RuntimeException
166+
throw new RuntimeException("Failed to download file from GCS: " + e.getMessage(), e);
164167
}
168+
165169
return outputFileString;
166170
}
167171

sdks/java/extensions/kafka-factories/src/test/java/org/apache/beam/sdk/io/kafka/file/aware/factories/FileAwareFactoryFnTest.java

Lines changed: 0 additions & 269 deletions
This file was deleted.

0 commit comments

Comments
 (0)