mule 4.4数据拖曳附加柜台的文件

发布于 2025-01-19 17:04:10 字数 1436 浏览 0 评论 0 原文

我在文件中阅读(请参阅下文)。示例文件有13行。

A|doe|chemistry|100|A|
B|shea|maths|90|A|
C|baba|physics|80|B|
D|doe|chemistry|100|A|
E|shea|maths|90|A|
F|baba|physics|80|B|
G|doe|chemistry|100|A|
H|shea|maths|90|A|
I|baba|physics|80|B|
J|doe|chemistry|100|A|
K|shea|maths|90|A|
L|baba|physics|80|B|
M|doe|chemistry|100|A|

然后使用每个(批次尺寸5)在这些行上迭代,然后调用REST API 根据REST API响应(成功或失败),我正在为相应的成功 /错误文件编写有效载荷。

我嘲笑了所谓的API,使得第5个记录的第一批记录将失败,其余文件将成功。

在写入成功 /错误文件时,我们正在使用以下转换:

output application/csv quoteValues=true,header=false,separator="|"
---
payload

所有这些都可以。
成功日志文件:

"F"|"baba"|"physics"|"80"|"B"
"G"|"doe"|"chemistry"|"100"|"A"
"H"|"shea"|"maths"|"90"|"A"
"I"|"baba"|"physics"|"80"|"B"
"J"|"doe"|"chemistry"|"100"|"A"
"K"|"shea"|"maths"|"90"|"A"
"L"|"baba"|"physics"|"80"|"B"
"M"|"doe"|"chemistry"|"100"|"A"

错误日志文件:

"A"|"doe"|"chemistry"|"100"|"A"
"B"|"shea"|"maths"|"90"|"A"
"C"|"baba"|"physics"|"80"|"B"
"D"|"doe"|"chemistry"|"100"|"A"
"E"|"shea"|"maths"|"90"|"A"

现在我想做的是将行/行号附加到这些文件中的每个文件中,因此,当它用于生产时,任何正在监视这些文件的人都可以轻松地理解并与原始文件相关。 因此,作为错误日志文件的一个例子(第一个批次失败,是第1至5行),我想将这些数字附加到每个行:

"1"|"A"|"doe"|"chemistry"|"100"|"A"
"2"|"B"|"shea"|"maths"|"90"|"A"
"3"|"C"|"baba"|"physics"|"80"|"B"
"4"|"D"|"doe"|"chemistry"|"100"|"A"
"5"|"E"|"shea"|"maths"|"90"|"A"

不确定我应该在DataWeave中写的是什么来实现这一目标?

I am reading in a file (see below). The example file has 13 rows.

A|doe|chemistry|100|A|
B|shea|maths|90|A|
C|baba|physics|80|B|
D|doe|chemistry|100|A|
E|shea|maths|90|A|
F|baba|physics|80|B|
G|doe|chemistry|100|A|
H|shea|maths|90|A|
I|baba|physics|80|B|
J|doe|chemistry|100|A|
K|shea|maths|90|A|
L|baba|physics|80|B|
M|doe|chemistry|100|A|

Then iterating over these rows using a for each ( batch size 5 ) and then calling a REST API
Depending on REST API response ( success or failure ) I am writing payloads to respective success / error files.

I have mocked the called API such that first batch of 5 records will fail and rest of the files will succeed.

While writing to success / error files am using the following transformation :

output application/csv quoteValues=true,header=false,separator="|"
---
payload

All of this works fine.
Success log file:

"F"|"baba"|"physics"|"80"|"B"
"G"|"doe"|"chemistry"|"100"|"A"
"H"|"shea"|"maths"|"90"|"A"
"I"|"baba"|"physics"|"80"|"B"
"J"|"doe"|"chemistry"|"100"|"A"
"K"|"shea"|"maths"|"90"|"A"
"L"|"baba"|"physics"|"80"|"B"
"M"|"doe"|"chemistry"|"100"|"A"

Error log file:

"A"|"doe"|"chemistry"|"100"|"A"
"B"|"shea"|"maths"|"90"|"A"
"C"|"baba"|"physics"|"80"|"B"
"D"|"doe"|"chemistry"|"100"|"A"
"E"|"shea"|"maths"|"90"|"A"

Now what I want to do is append the row/line number to each of these files so when this goes to production , whoever is monitoring these files can easily understand and correlate with the original file .
So as an example in case of error log file ( the first batch failed which is rows 1 to 5 ) I want to append these numbers to each of the rows:

"1"|"A"|"doe"|"chemistry"|"100"|"A"
"2"|"B"|"shea"|"maths"|"90"|"A"
"3"|"C"|"baba"|"physics"|"80"|"B"
"4"|"D"|"doe"|"chemistry"|"100"|"A"
"5"|"E"|"shea"|"maths"|"90"|"A"

Not sure what I should write in DataWeave to achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你是年少的欢喜 2025-01-26 17:04:10

在 ForEach 范围内,您可以访问计数器 vars.counter (或您选择的任何名称,因为它是可配置的)。
您将需要迭代每个记录块以添加每个记录的位置。您可以使用以下内容:

%dw 2.0
output application/csv quoteValues=true,header=false,separator="|"
var batchSize = 5
---
payload map ({
    counter: batchSize * (vars.counter - 1) + ($ + 1)
} ++ $
)

或者如果您更喜欢使用 update 函数(这会在最后一列添加记录计数器):

%dw 2.0
output application/csv quoteValues=true,header=false,separator="|"
var batchSize = 5
---
payload map (
    $ update {
        case .counter! -> batchSize * (vars.counter - 1) + ($ + 1)
    }
)

请记住替换 batchSize 变量从此代码中使用与 ForEach 范围中使用的相同值(如果它是参数化的,那就更好了)。

编辑 1 -
澄清: - 1+ 1 是因为两个索引(来自 For Each 范围的计数器和 $$map 中的 code> )是从零开始的。

Inside the ForEach scope, you have access to the counter vars.counter (or whatever name you've chosen since it's configurable).
You will need to iterate over each chunk of records for adding the position for each one. You can use something like:

%dw 2.0
output application/csv quoteValues=true,header=false,separator="|"
var batchSize = 5
---
payload map ({
    counter: batchSize * (vars.counter - 1) + ($ + 1)
} ++ $
)

Or if you prefer to use the update function (this will add the record counter at the last column instead though):

%dw 2.0
output application/csv quoteValues=true,header=false,separator="|"
var batchSize = 5
---
payload map (
    $ update {
        case .counter! -> batchSize * (vars.counter - 1) + ($ + 1)
    }
)

Remember to replace the batchSize variable from this code with the same value you're using in the ForEach scope (if it's parameterised, it would be better).

Edit 1 -
Clarification: the - 1 and + 1 are because both indexes (the counter from the For Each scope and the $$ from the map) are zero-based.

八巷 2025-01-26 17:04:10

只是另一个解决方法,并在不使用任何外部变量的情况下简化。脚本可以分为两;第一是错误组,第二是成功。

%dw 2.0
output application/csv quoteValues=true,header=false,separator="|"
// Will be used for creating a counter for Error group
var errorIdx = 1  
 // Will be used for creating a counter for Success group
var successIdx = 6
---
//errorItems for the first 5 rows
(payload[0 to 4] map (items,idx) -> (({"0":(idx) + errorIdx} ++ items))) 

++

//successItems from 6 and remaining items.
(payload[5 to -1] map (items,idx) -> (({"0":(idx) + successIdx} ++ items))) 

dataweave内联变量:

  • 的指针
  • errorIdx 是启动错误计数器 Scuctidx is 一个用于启动成功计数器的指针

这将从索引0到4元素提取:

payload[0 to 4]

这将从索引5提取到其余元素:

payload[5 to -1]

Just another workaround and to simplify without using any external variables. The script can be split into two; 1st is for Error group and 2nd is for Success.

%dw 2.0
output application/csv quoteValues=true,header=false,separator="|"
// Will be used for creating a counter for Error group
var errorIdx = 1  
 // Will be used for creating a counter for Success group
var successIdx = 6
---
//errorItems for the first 5 rows
(payload[0 to 4] map (items,idx) -> (({"0":(idx) + errorIdx} ++ items))) 

++

//successItems from 6 and remaining items.
(payload[5 to -1] map (items,idx) -> (({"0":(idx) + successIdx} ++ items))) 

DataWeave Inline Variables:

  • errorIdx is a pointer for starting the error counter
  • successIdx is a pointer for starting the success counter

This will extract from index 0 to 4 element:

payload[0 to 4]

This will extract from index 5 to remaining elements:

payload[5 to -1]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文