【问题标题】:How to read CSV files with blank lines and extra headers in Mule dataweave如何在 Mule dataweave 中读取带有空行和额外标题的 CSV 文件
【发布时间】:2016-09-02 18:01:44
【问题描述】:

我的 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 进行常规转换,它将返回 null 值

这是我在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 to ignore?

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

我正面临以下 groovy 的性能问题。 Mule 甚至需要花费太多时间来转换 1 MB 文件。有没有其他解决方案?

【问题讨论】:

    标签: regex csv groovy mule dataweave


    【解决方案1】:

    我使用 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

    【讨论】:

    • 我正面临这个 groovy 的性能问题。有没有其他解决方案?
    【解决方案2】:

    我知道已经很晚了。如果还在等待答案,试试这个。 它仅在 dataweave 中完成。

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

    希望对你有帮助。

    【讨论】:

      【解决方案3】:

      此链接可能会有所帮助

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

      并阅读上述文档中的以下代码 sn-p 以跳过空值

      %output application/xml skipNullOn="everywhere"

      【讨论】:

      • %output application/xml skipNullOn="everywhere" 用于我们正在生成的输出。由于空行,此处数据未加载到转换消息中。这就是它显示空有效负载的原因
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多