Google cloud storage 云发布/订阅到GCS,每个元素写入(数据流管道)

Google cloud storage 云发布/订阅到GCS,每个元素写入(数据流管道),google-cloud-storage,google-cloud-dataflow,publish-subscribe,apache-beam,Google Cloud Storage,Google Cloud Dataflow,Publish Subscribe,Apache Beam,如何在每次收到来自Pubsub的消息时向GCS写入,它会进行窗口写入,但不会按元素写入。关于这件事的任何提示都非常感谢 示例链接() 运行此示例代码后,它将写入发送到GCS的发布子消息。但当持续时间设置为1分钟时,它会保存所有消息,然后在一分钟后写入一个文件,但我希望它将每个消息写入不同的文件 如果每条消息需要一个文件,一个选项是创建如下简单转换: package com.myapp.dataflow.transform; import org.apache.beam.sdk.transfor

如何在每次收到来自Pubsub的消息时向GCS写入,它会进行窗口写入,但不会按元素写入。关于这件事的任何提示都非常感谢

示例链接()


运行此示例代码后,它将写入发送到GCS的发布子消息。但当持续时间设置为1分钟时,它会保存所有消息,然后在一分钟后写入一个文件,但我希望它将每个消息写入不同的文件

如果每条消息需要一个文件,一个选项是创建如下简单转换:

package com.myapp.dataflow.transform;

import org.apache.beam.sdk.transforms.DoFn;
import com.google.cloud.storage.*;
import static java.nio.charset.StandardCharsets.UTF_8;

public class StringToGcsFile extends DoFn<String, Blob> {
    private Storage storage;
    private String bucketName = "my-bucket";

    @Setup
    public void setup() {
        storage = StorageOptions.getDefaultInstance().getService();
    }

    @ProcessElement
    public void processElement(ProcessContext c) {
        // consider some strategy for object names, UUID or something
        String blobName = "my_blob_name";

        // Upload a blob to the bucket
        BlobId blobId = BlobId.of(bucketName, blobName);
        BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build();
        Blob blob = storage.create(blobInfo, c.element().getBytes(UTF_8));

        c.output(blob);
    }
}
package com.myapp.dataflow.transform;
导入org.apache.beam.sdk.transforms.DoFn;
导入com.google.cloud.storage。*;
导入静态java.nio.charset.StandardCharsets.UTF_8;
公共类StringToGcsFile扩展了DoFn{
专用存储;
私有字符串bucketName=“我的bucket”;
@设置
公共作废设置(){
storage=StorageOptions.getDefaultInstance().getService();
}
@过程元素
公共void processElement(ProcessContext c){
/考虑对象名称、UUID或某事的一些策略
String blobName=“我的blob\u名字”;
//将一个blob上传到bucket
BlobId BlobId=BlobId.of(bucketName,blobName);
BlobInfo BlobInfo=BlobInfo.newBuilder(blobId).setContentType(“text/plain”).build();
Blob Blob=storage.create(blobInfo,c.element().getBytes(UTF_8));
c、 输出(blob);
}
}
Maven依赖项:

<dependency>
    <groupId>com.google.cloud</groupId>
    <artifactId>google-cloud-storage</artifactId>
    <version>1.35.0</version>
</dependency>

com.google.cloud
谷歌云存储
1.35.0
您可以创建一个用于自动执行此操作的。云功能可以通过4个不同的事件来实现。其中之一是。 如果您想测试一个示例,请参考以下内容


您应该编写代码以正确地将每条消息重定向到所需的GCS,例如基于发布/订阅主题

我使用processElement实现了相同的功能

下面是示例代码

管道步骤:

pipeline_object.apply("Manually write events to GCS", ParDo.of(new Write_to_GCS()));
@SuppressWarnings("serial")
static class Write_to_GCS extends DoFn<KV<String, String>, TextIO.Write> {
    @ProcessElement
    public void processElement(ProcessContext c) throws JSONException {

        // Fetch text you need to write into file
        String output_string = c.element().getValue();

        // Create your service object
        Storage storage = StorageOptions.getDefaultInstance().getService();

        // Upload a blob to the newly created bucket
        BlobId blobId = BlobId.of(GCS_BUCKET_NAME, STORAGE_FILE_PATH);
        BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build();
        @SuppressWarnings("unused")
        Blob blob = storage.create(blobInfo, event_string.getBytes(UTF_8));
    }
}
ProcessElement函数:

pipeline_object.apply("Manually write events to GCS", ParDo.of(new Write_to_GCS()));
@SuppressWarnings("serial")
static class Write_to_GCS extends DoFn<KV<String, String>, TextIO.Write> {
    @ProcessElement
    public void processElement(ProcessContext c) throws JSONException {

        // Fetch text you need to write into file
        String output_string = c.element().getValue();

        // Create your service object
        Storage storage = StorageOptions.getDefaultInstance().getService();

        // Upload a blob to the newly created bucket
        BlobId blobId = BlobId.of(GCS_BUCKET_NAME, STORAGE_FILE_PATH);
        BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build();
        @SuppressWarnings("unused")
        Blob blob = storage.create(blobInfo, event_string.getBytes(UTF_8));
    }
}
@SuppressWarnings(“串行”)
静态类写入到GCS扩展了DoFn{
@过程元素
public void processElement(ProcessContext c)抛出JSONException{
//获取需要写入文件的文本
字符串输出_String=c.element().getValue();
//创建您的服务对象
Storage Storage=StorageOptions.getDefaultInstance().getService();
//将blob上载到新创建的bucket
BlobId BlobId=BlobId.of(GCS\u BUCKET\u名称、存储\u文件\u路径);
BlobInfo BlobInfo=BlobInfo.newBuilder(blobId).setContentType(“text/plain”).build();
@抑制警告(“未使用”)
Blob Blob=storage.create(blobInfo,event_string.getBytes(UTF_8));
}
}
您需要在pom.xml中包含以下依赖项

<dependency>
    <groupId>com.google.cloud</groupId>
    <artifactId>google-cloud-storage</artifactId>
    <version>1.37.1</version>
</dependency>

com.google.cloud
谷歌云存储
1.37.1
这段代码将要做的是创建一个gcs存储服务对象,并将一个blob写入指定的路径