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

如何根据传入的文件名参数选择执行不同的Pentaho转换/作业

Handling Filename-Based Job Branching in Pentaho (Simulating Switch Case)

Hey, great question! Since Pentaho's native switch case is only available inside transformations, we need to build branching logic at the job level to route execution based on your incoming filename parameter. Let's break this down into actionable, easy-to-follow steps:

1. First, Set Up Your Job to Accept the Filename Parameter

First, make sure your Pentaho Job can receive the filename from your Java Web app:

  • Open your job, go to Edit > Job Settings
  • Switch to the Parameters tab, add a parameter (I'll call it INPUT_FILENAME here) – you can set a default empty value if needed, then save the settings.

2. Simulate Switch Case with Job Entry Conditions

Pentaho Jobs don't have a built-in switch structure, but we can replicate the same logic using multiple condition-based job entries. Here are two reliable methods:

Method 1: Use Evaluate JavaScript (Flexible for Complex Logic)

This is my go-to for handling pattern matches or complex filename rules:

  • Drag an Evaluate JavaScript job entry onto your canvas
  • Double-click to configure it, then write logic to check the filename parameter:
    const filename = "${INPUT_FILENAME}";
    // Match by file extension, exact name, or any custom pattern
    if (filename.endsWith(".csv")) {
      return true; // Triggers the CSV transformation branch
    } else if (filename.endsWith(".xlsx")) {
      return true; // Triggers the Excel transformation branch
    } else if (filename.includes("daily_snapshot")) {
      return true; // Triggers a special report branch
    } else {
      return false; // Default case for unhandled files
    }
    
  • Connect this entry to the corresponding transformation job entry when it returns true, and wire up a default branch (like an error handler or generic processing) for the false case.

Method 2: Use Compare Values Condition (Simple Exact Matches)

If you only need to match exact filenames, this is more straightforward:

  • For each filename you need to handle, drag a Condition - Compare Values entry to the canvas
  • Configure each condition:
    • Left value: ${INPUT_FILENAME}
    • Operator: equals (or contains/matches regex if you need fuzzy matching)
    • Right value: The exact filename you want to match (e.g., customer_export.csv)
  • Connect each condition entry to its corresponding transformation. Add a dummy entry as a catch-all for unhandled filenames, linking it to your default processing logic.

3. Pass the Filename from Your Java Web App

When triggering the Pentaho Job from your Java app, make sure to pass the filename parameter. Here's how you might do it:

  • Command Line Example (if executing via shell):
    kitchen.sh -file=/path/to/your/main_job.kjb -param:INPUT_FILENAME=your_target_file.csv
    
  • Java API Example:
    import org.pentaho.di.job.JobExecutor;
    import java.util.HashMap;
    import java.util.Map;
    
    public class PentahoJobTrigger {
        public static void main(String[] args) {
            JobExecutor executor = new JobExecutor();
            Map<String, String> jobParams = new HashMap<>();
            jobParams.put("INPUT_FILENAME", "your_target_file.csv");
            // Execute the job with parameters
            executor.execute("/path/to/your/main_job.kjb", jobParams);
        }
    }
    

4. Pro Tip: Encapsulate Logic in Sub-Jobs

If each filename's processing logic is complex, wrap each transformation into a separate sub-job. This keeps your main job clean and makes it far easier to maintain individual branches later on.


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 11:47:24