在 Apache Beam 中从 GCS 读取文件
Read a file from GCS in Apache Beam
我需要从 GCS 存储桶中读取文件。我知道我必须使用 GCS API/Client 库,但我找不到任何与之相关的示例。
我在 GCS 文档中提到了这个 link:
GCS Client Libraries。但是实在是没法下手。如果有人可以提供一个真正有帮助的例子。
谢谢
好的。如果您只想从 GCS 读取文件,而不是作为 PCollection 而是作为常规文件,并且如果您在使用 GCS Java 客户端库时遇到问题,您还可以使用 Apache Beam FileSystems API:
首先,您需要确保 pom.xml
在 beam-sdks-java-extensions-google-cloud-platform-core
上有 Maven 依赖项,其中包含 gs://
文件系统的实现:
<dependency>
<groupId>org.apache.beam</groupId>
<artifactId>beam-sdks-java-extensions-google-cloud-platform-core</artifactId>
</dependency>
然后设置文件系统API(它在所有管道中默认设置,但如果您在管道外使用它,则需要手动进行)。
PipelineOptions options = PipelineOptionsFactory.create();
// ...Optionally fill in options such as GCP credentials...
// (see GcpOptions class)
FileSystems.setDefaultPipelineOptions(options);
然后就可以使用了:
ReadableByteChannel chan = FileSystems.open(FileSystems.matchNewResource(
"gs://path/to/your/file", false /* is_directory */));
try (InputStream stream = Channels.newInputStream(chan)) {
// Use regular Java utilities to work with the input stream.
}
我需要从 GCS 存储桶中读取文件。我知道我必须使用 GCS API/Client 库,但我找不到任何与之相关的示例。
我在 GCS 文档中提到了这个 link: GCS Client Libraries。但是实在是没法下手。如果有人可以提供一个真正有帮助的例子。 谢谢
好的。如果您只想从 GCS 读取文件,而不是作为 PCollection 而是作为常规文件,并且如果您在使用 GCS Java 客户端库时遇到问题,您还可以使用 Apache Beam FileSystems API:
首先,您需要确保 pom.xml
在 beam-sdks-java-extensions-google-cloud-platform-core
上有 Maven 依赖项,其中包含 gs://
文件系统的实现:
<dependency>
<groupId>org.apache.beam</groupId>
<artifactId>beam-sdks-java-extensions-google-cloud-platform-core</artifactId>
</dependency>
然后设置文件系统API(它在所有管道中默认设置,但如果您在管道外使用它,则需要手动进行)。
PipelineOptions options = PipelineOptionsFactory.create();
// ...Optionally fill in options such as GCP credentials...
// (see GcpOptions class)
FileSystems.setDefaultPipelineOptions(options);
然后就可以使用了:
ReadableByteChannel chan = FileSystems.open(FileSystems.matchNewResource(
"gs://path/to/your/file", false /* is_directory */));
try (InputStream stream = Channels.newInputStream(chan)) {
// Use regular Java utilities to work with the input stream.
}