简体   繁体   English

如何在Mule Dataweave中读取带有空行和多余标题的CSV文件

[英]How to read CSV files with blank lines and extra headers in Mule dataweave

I have CSV having blank line and extra herder below that like this: 我的CSV包含空白行和下面的多余牧民,如下所示:

**blank line**
Available_Date_Feed
productID,availableDate
148305801,2015-08-07T00:00:00.000+0000
160611862,2015-07-29T00:00:00.000+0000
160611715,2015-07-29T00:00:00.000+0000
160342798,2015-07-29T00:00:00.000+0000

I want to read values of productID & availableDate. 我想读取productID和availableDate的值。 If we do regular transformation with dataweave it will return null values 如果我们使用dataweave进行常规转换,它将返回空值

This is the code I have wrote in dataweave: 这是我在dataweave中编写的代码:

%dw 1.0
%input in0 application/csv headers=true
%output application/java
---
payload  map  {
    productID:$.productID,
    availableDate:$.availableDate
}

Returning payload as : 返回有效载荷为:

[{productID=null, availableDate=null}, {productID=null, availableDate=null}, {productID=null, availableDate=null}, {productID=null, availableDate=null}]

Any suggestions here? 有什么建议吗? Can we use Groovy/MEL/regex expression for this? 我们可以为此使用Groovy / MEL / regex表达式吗? How to use Rows to ignore in Dataweave? 如何使用行在Dataweave中忽略?

Can we Skip first 2 lines using groovy/regex? 我们可以使用groovy / regex跳过前2行吗?

I am facing performance issue with the below groovy. 我面临以下常规问题的性能问题。 Mule taking too much time to convert even 1 MB file. ule子花费太多时间来转换甚至1 MB的文件。 Is there any another solution for this? 还有其他解决方案吗?

I have used Groovy to skip the first 2 lines. 我已经使用Groovy跳过了前2行。 This script fetch the comma separated values directly 该脚本直接获取逗号分隔的值

 csvContent = message.payload
def filteredContent = new StringBuffer()
regexPattern = /(\S*),(\S*)/
finder = csvContent =~ regexPattern

(0..<finder.count).each {
    //println "Iteration: ${it+1}:"
    filteredContent.append(finder[it][0])
    filteredContent.append('\n')

}

return filteredContent.toString()

use object to string before groovy 在Groovy之前使用object to string

I know it's late. 我知道已经晚了。 If still waiting for answer, try this. 如果仍在等待答案,请尝试此操作。 It's done in dataweave only. 仅在dataweave中完成。

%dw 1.0
%output application/csv header=false
---
payload[3..-1] map {
    productId:$[0],
    date: $[1]
}

Hope it helps. 希望能帮助到你。

This link might helpful 此链接可能有帮助

https://developer.mulesoft.com/docs/dataweave https://developer.mulesoft.com/docs/dataweave

and read about below code snippet in above doc to skip the null values 并阅读以上文档中的以下代码片段以跳过空值

%output application/xml skipNullOn="everywhere" %output application / xml skipNullOn =“无处不在”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM