我在文件中阅读(请参阅下文)。示例文件有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?
发布评论
评论(2)
在 ForEach 范围内,您可以访问计数器
vars.counter
(或您选择的任何名称,因为它是可配置的)。您将需要迭代每个记录块以添加每个记录的位置。您可以使用以下内容:
或者如果您更喜欢使用
update
函数(这会在最后一列添加记录计数器):请记住替换
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:
Or if you prefer to use the
update
function (this will add the record counter at the last column instead though):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 theFor Each
scope and the$$
from themap
) are zero-based.只是另一个解决方法,并在不使用任何外部变量的情况下简化。脚本可以分为两;第一是错误组,第二是成功。
dataweave内联变量:
errorIdx
是启动错误计数器Scuctidx
is 一个用于启动成功计数器的指针这将从索引0到4元素提取:
这将从索引5提取到其余元素:
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.
DataWeave Inline Variables:
errorIdx
is a pointer for starting the error countersuccessIdx
is a pointer for starting the success counterThis will extract from index 0 to 4 element:
This will extract from index 5 to remaining elements: