You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Talend Studio:如何用TMap统计起止日期间的非工作日数量?

Talend统计起止区间内非工作日数量的实现方案

两种方式都可行,具体选择取决于非工作日表的数据量:

一、直接用TMap表达式编辑器实现(适合数据量小的场景)

操作步骤:

  1. 配置TMap输入与Lookup:
    • 将表1作为主输入,表2作为Lookup输入拖入TMap
    • 选中Lookup区的表2,在右侧设置中将Match model改为All matches(确保获取区间内所有匹配的非工作日)
    • 建立关联条件:row2.Non_working_days >= row1.Start_Date && row2.Non_working_days <= row1.End_Date
  2. 配置输出字段:
    • 直接映射row1.Start_Date和row1.End_Date到输出
    • 添加No_of_non_working_days字段,表达式填写Aggregate.count(row2.Non_working_days),该函数会自动统计当前主行对应的匹配非工作日数量(无匹配时返回0)

二、自定义Routine实现(适合数据量大的场景)

当非工作日表数据量较大时,Lookup匹配的开销会增加,可通过全局集合+自定义函数优化:

  1. 加载非工作日数据到全局集合:
    • 先用tFlowToIterate或tAggregateRow将表2的Non_working_days字段聚合为一个List<Date>
    • 用tJavaRow将这个List存入全局变量:globalMap.put("nonWorkingDays", ((List<Date>)globalMap.get("aggregatedNonWorkingDays")));
  2. 编写自定义Routine:
    在Talend的Routines中新建一个类,添加如下方法:
    public static int countNonWorkingDays(Date startDate, Date endDate, List<Date> nonWorkingDays) {
        int count = 0;
        if (nonWorkingDays == null || startDate == null || endDate == null) {
            return count;
        }
        for (Date day : nonWorkingDays) {
            if ((day.after(startDate) || day.equals(startDate)) && (day.before(endDate) || day.equals(endDate))) {
                count++;
            }
        }
        return count;
    }
    
  3. 在TMap中调用Routine:
    输出字段No_of_non_working_days的表达式填写:YourRoutineName.countNonWorkingDays(row1.Start_Date, row1.End_Date, (List<Date>)globalMap.get("nonWorkingDays"))

内容的提问来源于stack exchange,提问作者bobthebilly

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 21:39:20