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

相关内容

热门资讯

盘点一款!填大坑辅助工具排行,... 盘点一款!填大坑辅助工具排行,wpk刷入池率脚本,大神讲解(揭秘有挂);1.填大坑辅助工具排行 ai...
一起来探讨!决战13水辅助,w... 您好:决战13水辅助这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌特...
一分钟揭秘!丽水双扣茶苑脚本,... 一分钟揭秘!丽水双扣茶苑脚本,xpoker辅助控制,2025教程(有挂解惑)是一款可以让一直输的玩家...
今日百科!腾讯欢乐卡五星辅助器... 今日百科!腾讯欢乐卡五星辅助器,wepoker安装教程,2025新版(有挂神器)是一款可以让一直输的...
玩家必看科普!微信小程序外辅助... 玩家必看科普!微信小程序外辅助工具,pokernow辅助工具,力荐教程(有挂攻略);1、界面简单,没...
一分钟教会你!授权二厅辅助,w... 《一分钟教会你!授权二厅辅助,wpk有那种辅助吗,黑科技教程(果真有挂)》 授权二厅辅助软件透明挂更...
安装程序教程!小程序牵手跑得有... 安装程序教程!小程序牵手跑得有辅助器,pokernow辅助工具,揭秘教程(发现有挂);一、小程序牵手...
大家学习交流!创思维激k软件助... 大家学习交流!创思维激k软件助手,wepoker黑侠破解,2025教程(有挂方法)是一款可以让一直输...
总算清楚!奇迹免费脚本辅助工具... 总算清楚!奇迹免费脚本辅助工具,xpoker辅助工具,系统教程(有挂方式);相信小伙伴都知道这个奇迹...
总算清楚!手游游戏挂机辅助,w... 总算清楚!手游游戏挂机辅助,wpk真的有透视嘛,安装教程(竟然有挂);1、总算清楚!手游游戏挂机辅助...