Apache 使用NIFI将XML文件转换为csv时出错

Apache 使用NIFI将XML文件转换为csv时出错,apache,apache-nifi,Apache,Apache Nifi,您好,我正在构建一个将Xml转换为csv的管道, 在Nifi中,首先我将XML数据转换为Json,然后从Json转换为CSV,但我的输出文件不是CSV,有人能帮我吗 myxmldata <?xml version="1.0" encoding="UTF-8"?> <bookstore> <book category="COOKING"> <title lang="en">Everyday Italian</title> &l

您好,我正在构建一个将Xml转换为csv的管道, 在Nifi中,首先我将XML数据转换为Json,然后从Json转换为CSV,但我的输出文件不是CSV,有人能帮我吗

myxmldata

 <?xml version="1.0" encoding="UTF-8"?> 
 <bookstore>
 <book category="COOKING">
 <title lang="en">Everyday Italian</title>
 <author>Giada De Laurentiis</author>
 <year>2005</year>
 <price>30.00</price>
 </book>
 <book category="CHILDREN">
 <title lang="en">Harry Potter</title>
 <author>J K. Rowling</author>
 <year>2005</year>
  <price>29.99</price>
  </book>
   <book category="WEB">
  <title lang="en">XQuery Kick Start</title>
   <author>James McGovern</author>
  <author>Per Bothner</author>
  <author>Kurt Cagle</author>
  <author>James Linn</author>
 <author>Vaidyanathan Nagarajan</author>
 <year>2003</year>
 <price>49.99</price>
  </book>
 <book category="WEB">
  <title lang="en">Learning XML</title>
   <author>Erik T. Ray</author>
   <year>2003</year>
    <price>39.95</price>
   </book>
   </bookstore>
直到这里它很好,但当转换为csv时,我得到了错误的csv数据 将json转换为csv处理器映像

我的csv输出数据如下所示

[{"book":[{"title":"Everyday Italian","author":"Giada De 
 Laurentiis","year":2005,"price":30.0},{"title":"Harry 
 Potter","author":"J K. Rowling","year":2005,"price":29.99}, 
 {"title":"XQuery Kick Start","author":"Vaidyanathan 
  Nagarajan","year":2003,"price":49.99},{"title":"Learning 
  XML","author":"Erik T. Ray","year":2003,"price":39.95}]}]
book
"[MapRecord[{title=Everyday Italian, year=2005, author=Giada De 
Laurentiis, price=30.0}], MapRecord[{title=Harry Potter, year=2005, 
 author=J K. Rowling, price=29.99}], MapRecord[{title=XQuery Kick 
 Start, year=2003, author=Vaidyanathan Nagarajan, price=49.99}], 
 MapRecord[{title=Learning XML, year=2003, author=Erik T. Ray, 
  price=39.95}]]"

这里发生了几件事

首先,如果要更改正在操作的内容,即过滤字段、转换数组等,则只需要JoltTransferMJSON处理器。。如果没有,您可以删除该处理器并使用ConvertRecord直接从XML转换为CSV。您不需要中间的JSON表示

其次,CSV不如预期的原因是,中间JSON将数组作为键的值。应该有XPath表达式,允许您从数组中读取并转换为CSV行。如果您无法构建一个,我建议使用ScriptedRecordReader和/或ScriptedRecordSetWriter将序列化表单转换为复杂的内部格式并返回序列化表单


我建议尝试直接从XML转换为CSV,然后对数据进行更改,除非必要的数据在转换过程中丢失。如果是,可能需要额外的翻译步骤

您的预期结果是什么? XML的转换并不容易,因为书中有许多作者。 也可以将失败重定向到日志消息,以便更好地查看它