如何在Mule数据编织中读取带有空行和额外标题的CSV文件

时间:2015-10-28 04:57:33

标签: regex csv groovy mule dataweave

我的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

我想读取productID&的值availableDate。如果我们使用dataweave进行常规转换,它将返回空值

这是我在dataweave中编写的代码:

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

将有效负载返回为:

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

这里有什么建议吗? 我们可以使用Groovy / MEL / regex表达式吗? 如何在Dataweave中使用Rows忽略?

我们可以使用groovy / regex跳过前两行吗?

我正面临着以下常规的性能问题。骡子花了太多时间来转换甚至1 MB的文件。还有其他解决办法吗?

3 个答案:

答案 0 :(得分:0)

我使用Groovy跳过前两行。此脚本直接获取逗号分隔值

 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()

在groovy之前使用object to string

答案 1 :(得分:0)

我知道它已经很晚了。如果还在等待回答,试试这个。 它仅在数据编织中完成。

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

希望它有所帮助。

答案 2 :(得分:-1)

此链接可能有用

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

并阅读以上doc中的以下代码段以跳过空值

%输出应用程序/ xml skipNullOn =“无处不在”