我正在生成一个csv文件,准备发送到一个SFTP的位置,这个文件是按照预期创建的,除了额外的/nis有什么办法可以去除它?
输入。
[
{
"reference": "TPL",
"newId": "07223455",
"dateCreated": "020200509",
"error": "000"
},
{
"reference": "TIN",
"newId": "07123455",
"dateCreated": "020200509",
"error": "000"
},
{
"reference": "THW",
"newId": "05550910",
"dateCreated": "020200509",
"error": "000"
},
{
"reference": "THW",
"newId": "05533112",
"dateCreated": "020200509",
"error": "000"
},
{
"reference": "TSB",
"newId": "05567865",
"dateCreated": "020200509",
"error": "000"
}
]
Dataweave。
%dw 2.0
output application/csv header = false
import * from dw::core::Strings
---
payload filter ( $.error == '000' ) map (
values:
rightPad($.reference, 16)
++ rightPad('0', 1)
++ rightPad($.newId, 9)
++ rightPad('0', 1)
++ rightPad($.dateCreated,9)
++ rightPad("", 16)
++ rightPad("P",1)
++ rightPad("", 103)
)
结果:
TPL 07223455 020200509 P
TIN 07123455 020200509 P
THW 05550910 020200509 P
THW 05533112 020200509 P
TSB 05567865 020200709 P
TSB 05533112 020200509 P
我尝试删除最后一行
result[0 to sizeOf(result) -1]
但每行末尾都默认添加了"\n"。
你可能想试试这个。
%dw 2.0
output application/flatfile schemaPath="schema.ffd"
var data = [
{
"reference": "TPL",
"newId": "07223455",
"dateCreated": "020200509",
"error": "000"
},
{
"reference": "TIN",
"newId": "07123455",
"dateCreated": "020200509",
"error": "000"
},
{
"reference": "THW",
"newId": "05550910",
"dateCreated": "020200509",
"error": "000"
},
{
"reference": "THW",
"newId": "05533112",
"dateCreated": "020200509",
"error": "000"
},
{
"reference": "TSB",
"newId": "05567865",
"dateCreated": "020200509",
"error": "000"
}
]
---
data map {($ - "error"), p: "P"}
你必须在你的classpath下创建一个文件(我是在你的classpath下创建的) src/main/resources
),包含模式,我把它称为 schema.ffd
你可以看到下面的内容。
form: FIXEDWIDTH
name: my-flat-file
values:
- { name: 'reference', type: String, length: 16 }
- { name: 'newId', type: String, length: 10 }
- { name: 'dateCreated', type: String, length: 25 }
- { name: 'p', type: String, length: 104 }
我得到了以下的输出。
TPL 07223455 020200509 P
TIN 07123455 020200509 P
THW 05550910 020200509 P
THW 05533112 020200509 P
TSB 05567865 020200509 P
你可以一直这样做。
%dw 2.0
output text/plain
import * from dw::core::Strings
---
write(payload filter ( $.error == '000' ) map (
values:
rightPad($.reference, 16)
++ rightPad('0', 1)
++ rightPad($.newId, 9)
++ rightPad('0', 1)
++ rightPad($.dateCreated,9)
++ rightPad("", 16)
++ rightPad("P",1)
++ rightPad("", 103)
), "application/csv", { header: false})[0 to -2]
编辑包括关闭头部。