Apache 光束管道 Java:记录未按顺序写入目标文件

问题描述

我正在使用 Apache 光束管道将 csv 文件一个容器传输到 Azure 存储中的另一个容器,并且能够成功传输文件,但目标文件中的记录不是按顺序排列的。下面是我用来传输文件代码

String format = LocalDateTime.Now().format(DateTimeFormatter.ofPattern("YYYY_MM_DD_HH_MM_SS3")).toString();

String connectionString = "<<AZURE_STORAGE_CONNECTION_STRING>>"; 
        
PipelineOptions options = PipelineOptionsFactory.create();
options.as(BlobstoreOptions.class).setAzureConnectionString(connectionString);
        
Pipeline p = Pipeline.create(options);
p.apply(TextIO.read().from("azfs://storageaccountname/containername/CSVSample.csv"))
.apply("",FileIO.<String>write().to("azfs://storageaccountname/containername/"+format+"/").withNumShards(1).withSuffix(".csv")
        .via(TextIO.sink()));
p.run().waitUntilFinish();

解决方法

Beam PCollections 未排序。如果您需要顺序,您可以group by a key(在其中施加顺序)并对值集进行排序,将其作为包含换行符的单个字符串元素发出。