如何在 Talend 中实现 tLoop? [英] How to implement tLoop in Talend?

查看:72
本文介绍了如何在 Talend 中实现 tLoop?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Talend 的新手,需要一个示例作业来实现 tLoop.如果作业失败,我想运行 10 次.我查看了文档,但似乎无法弄清楚这一点.

I'm new to Talend and need an example job to implement tLoop. I want to run a job 10 times if it fails. I've looked at the documents, but I can't seem to figure this out.

推荐答案

这个答案有 2 个部分

This answer has 2 sections

  1. 使用 tJava 创建循环

  1. Creating a loop with tJava

将失败的连接重新绑定到数据源 5 次(添加 tJavaFlex)

Retying a failed connection to a data source 5 times (with adding tJavaFlex)

___________________________________

第 1 部分:使用 tJava 创建循环

-----------------------------------------------------------

我只是编写了一个 tJava 组件,然后迭代为 false.像这样

I just write a tJava component and then iterate to false. Like this

第一步:创建一个上下文变量

第 2 步:在 tJava (tJava1) 中编写一些 Java 代码

Step 2: write some java code in tJava (tJava1)

// setting loop flag
context.continueLooping = true;
//log.info("Starting job...");

然后连接On Component Ok

第 3 步:创建 tLoop

在循环条件中放入你的上下文 context.continueLooping第一次迭代时应该为真.

in the loop condition put your context context.continueLooping which should be true by the first iteration.

然后迭代

到下一个 tJava (tJava2)

to the next tJava (tJava2)

if ( ((Integer)globalMap.get("tLoop_1_CURRENT_ITERATION")) == 1)
{
   // code

}
else if(((Integer)globalMap.get("tLoop_1_CURRENT_ITERATION")) == 2) 
{
   // code

}
else if (((Integer)globalMap.get("tLoop_1_CURRENT_ITERATION")) == 3)
{
   // code

}
else if (((Integer)globalMap.get("tLoop_1_CURRENT_ITERATION")) == 4)
{
   // code

}
else if (((Integer)globalMap.get("tLoop_1_CURRENT_ITERATION")) == 5)
{
   // code

   context.continueLooping = false;
  // log.info("DONE");
}

else 
{
   context.continueLooping = false;
  // log.error("out of bounds...");

} 

这个 tJava 为每次迭代运行不同的代码,直到达到 5我使用这个区域来计算内容并将值加载到其他上下文等等.

this tJava runs different code for each iteration till it reaches 5 I use this area to count stuff and load value to other contexts and more.

然后它运行嵌套部分 n 次,直到上下文值设置为 false.

Then it runs the nest part n times till the context value is set to false.

___________________________

第 2 部分:重试失败的连接

___________________________

如果您需要重试数据库连接.

if you need to retry a DB connection.

像这样在 tLoop1 和 tJava2 之间添加一个 tJavaFlex

add a tJavaFlex between tLoop1 and tJava2 like so

并在3个部分添加以下代码开始:

and add the following code in the 3 sections Start:

// start part of your Java code
try{   

主要内容:

// here is the main part of the component,
// a piece of code executed in the row
// loop
if ( ((Integer)globalMap.get("tLoop_1_CURRENT_ITERATION")) > 1)
{
Thread.sleep(10000);
}

结束:

// end of the component, outside/closing the loop
}catch (Exception e) {

if ( ((Integer)globalMap.get("tLoop_1_CURRENT_ITERATION")) > 5)
{
context.continueLooping = false;
}
else
{
System.out.println("Connection failed. Retrying...next");
}


}      

并添加 On Component Ok tJava 与代码以停止循环成功(tJava3)

and add On Component Ok tJava with the code to stop looping on the success (tJava3)

context.continueLooping = false; 

这篇关于如何在 Talend 中实现 tLoop?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆