ApacheBeam管道Java:记录未按顺序写入目标文件。
创始人
2024-09-05 11:30:06
0

问题源于具有并行化和异步处理功能的Apache Beam框架可能无法保证数据流在目标文件中以正确的顺序写入。为了解决这个问题,可以使用有序写入器(OrderedWriter)来确保记录按正确的顺序写入目标文件。以下是一个Java代码示例,演示如何使用有序写入器来解决此问题:

PCollection records = ...; // input PCollection
PCollectionView filenamesView = ...; // PCollectionView of the filenames to write to
final TupleTag doneTag = new TupleTag<>();
//Create a new PCollection by assigning a unique, increasing ID to each element
PCollection> keyedRecords =
        records.apply("AddUniqueIds", WithKeys.of((Void) null)).setCoder(KvCoder.of(VarIntCoder.of(), MyRecordCoder.of()));
//Group all elements assigned with the same key (null), and sort all records by id.
PCollection sortedRecords =
        PCollectionList.of(keyedRecords.apply(GroupByKey.create()))
                .apply(ParDo.of(new DoFn>, MyRecord>() {
                    @ProcessElement
                    public void processElement(ProcessContext context) {
                        List sorted = new ArrayList<>();
                        for (MyRecord r : context.element().getValue()) {
                            sorted.add(r);
                        }
                        Collections.sort(sorted, new Comparator() {
                            @Override
                            public int compare(MyRecord o1, MyRecord o2) {
                                // Assumes that MyRecord has a method that returns its id as an int.
                                return Long.compare(o1.getId(), o2.getId());
                            }
                        });
                        for (MyRecord r : sorted) {
                            context.output(r);
                        }
                    }
                })).setCoder(MyRecordCoder.of());
// Create a new representation of the input PCollection where each element is a tuple containing
// the filename and the record it belongs to.
// For example, if the input file was records 0, 1, 2, 3, 4, 5, 6, the output would be:
// ("file0", record 0), ("file1", record 1), ("file2", record 2), ("file3", record 3), ...
final PCollection> keyedOutput = sortedRecords
        .apply("AssignFilename", ParDo.of(new DoFn>() {
            @ProcessElement
            public void processElement(ProcessContext context) {
                List filenames = context.sideInput

相关内容

热门资讯

重磅来袭!决战13水辅助,we... 重磅来袭!决战13水辅助,wepoker透视是真的吗,专业教程(有挂规律);是一款可以让一直输的玩家...
分享实测!万能脚本辅助器,so... 分享实测!万能脚本辅助器,sohoo poker辅助,玩家教程(有挂技巧);万能脚本辅助器中的10万...
必知教程!阿拉斗牌辅助,xpo... 必知教程!阿拉斗牌辅助,xpoker透视辅助,辅助教程(确实有挂);小薇(透视辅助)致您一封信;亲爱...
记者揭秘!边锋干瞪眼辅助,we... 记者揭秘!边锋干瞪眼辅助,wepoker辅助器有哪些功能,细节揭秘(果真有挂);详细边锋干瞪眼辅助攻...
推荐十款!微信小程序功夫川麻小... 您好:微信小程序功夫川麻小程序辅助这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现...
发现玩家!全民牛牛拼三张开挂,... 发现玩家!全民牛牛拼三张开挂,wepoker底牌透视,安装教程(存在有挂);最新版2026是一款经典...
发现玩家!老k游戏辅助,hhp... 发现玩家!老k游戏辅助,hhpoker外挂靠谱吗,新2025版(有挂透视)是一款可以让一直输的玩家,...
分享一款!新超凡大厅辅助,hh... 分享一款!新超凡大厅辅助,hhpoker可以控制吗,切实教程(有挂辅助);1、在新超凡大厅辅助ai机...
程序员教你!微信辅助工具,哈糖... 程序员教你!微信辅助工具,哈糖大菠萝攻略,细节方法(真的有挂);1、这是跨平台的微信辅助工具黑科技,...
一分钟了解!广东雀神挂件脚本开... 一分钟了解!广东雀神挂件脚本开挂,pokemmo辅助脚本,wpk教程(有挂分享);【福星临门,好运相...