Dataweave - 转换 CSV 结构

Dataweave - Transform CSV structure

我正在尝试使用 Dataweave 2.0 转换 .CSV 的结构

这是我的输入 csv 示例:

gaID,gender,age,city,state
GA1.3.332,male,20-30,,
GA1.3.1041d,female,30-40,Sao Paulo,Sao Paulo
GA1.3.1041d,,,Sao Paulo

我需要创建这个输出:

GA1.3.332^gender:male;age:20-30
GA1.3.1041d^gender:female;age:30-40;city:Sao Paulo;state:Sao Paulo
GA1.3.1041d^state:Sao Paulo

请注意,当输入属性为 null 时,它们不应出现在输出中,这就是我遇到的问题。

到目前为止我有这段代码,但不完全符合我的需要,因为它写了所有属性,即使是否为 null。

%dw 2.0
output application/csv header=false, separator=';'
---
payload map {
 c1: $.gaID ++ "^" ++ "gender:" ++ $.gender,
 c2: "age:" ++ $.age,
 c3: "city:" ++ $.city,
 c4: "state:" ++ $.state,
 c5: "maritalStatus:" ++ $.martitalStatus,
 c6: "householdIncome:" ++ $.householdIncome,
 c7: "bradSegCustomer:" ++ $.bradSegCustomer,
 c8: "bradCustomer:" ++ $.bradCustomer,
 c9: "BankClientSegment:" ++ $.BankClientSegment,
 c10: "main_account:" ++ $.main_account,
 c11: "occupation:" ++ $.occupation,
 c12: "presenceofChildren:" ++ $.presenceofChildren,
}

我的代码输出:

GA1.3.332^gender:male;age:20-30;city:;state:
GA1.3.1041d^gender:female;age:30-40;city:Sao Paulo;state:Sao Paulo

我将它转换为动态的,除了对 gaID 字段的特殊转换,然后添加条件以使用 filterObject() 过滤具有空值的元素。

%dw 2.0
output application/csv header=false, separator=';'
---
payload map 
    ($ 
      filterObject ((value, key, index) -> !isEmpty(value))
      mapObject((value, key, index) -> 
        (key): if (key as String != "gaID") 
                    (key as String ++ ":" ++ value) 
               else (value ++ "^") 
    )
)

输入:

gaID,gender,age,city,state
GA1.3.332,male,20-30,,
GA1.3.1041d,female,30-40,Sao Paulo,Sao Paulo
GA1.3.1041d,,,Sao Paulo

输出:

GA1.3.332^;gender:male;age:20-30
GA1.3.1041d^;gender:female;age:30-40;city:Sao Paulo;state:Sao Paulo
GA1.3.1041d^;city:Sao Paulo

对于 CSV 格式,您无法删除字段之间的分隔符。如果第一个';'是一个问题,那么我们需要使用字符串输出并根据需要连接字段,尽管我们失去了 CSV 解决方案的一些简单性:

%dw 2.0
output application/java
---
payload map 
    ($ 
        filterObject ((value, key, index) -> !isEmpty(value))
        mapObject((value, key, index) -> 
            (key): if (key as String != "gaID") 
                        (key as String ++ ":" ++ value) 
                else (value ++ "^") 
        )
        pluck ($)
        reduce  ((item, acc="") -> acc ++ item ++";")
    ) reduce ($$ ++ "\n" ++ $)

此脚本在每行末尾提供了一个额外的 ;。如果这是一个问题,那么添加一个函数来删除它应该不难。