2014-12-03 42 views
0

我有一個使用DataMapper配置將XML映射到JSON的mule流,因爲我有一個要求查找CSV文件,其中包含例如兩列和四行以映射到其中一個JSON輸出字段。Mule - CSV查找表

下面是我的樣本CSV文件的內容:

Name,Contact Ram,111-222 Kumar,222-333 John,333-444

我有例外下面,同時運行的DataMapper:

Element [CSV:CSV]-Pre-Execution of lookup table [CSV:CSV]failed Error when parsing record #2 field Name Caused by: java.lang.RuntimeException: Parsing error: Unexpected record delimiter, probably record has too few fields.

當CSV文件只包含一行並無異常。

任何人都可以提供一種解決問題的方法嗎?提前致謝。

編輯:請找到DataMapper的配置

<mule> 
<data-mapper:config name="XML_To_JSON_1" transformationGraphPath="xml_to_json_1.grf" doc:name="XML_To_JSON" /> 
    <flow name="mule-csv-lookupFlow1" doc:name="mule-csv-lookupFlow1"> 
     <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="9090" doc:name="HTTP" /> 
     <data-mapper:transform config-ref="XML_To_JSON_1" doc:name="XML To JSON" /> 
    </flow> 
</mule> 

下面是.grf文件

<?xml version="1.0" encoding="UTF-8"?><Graph __version="3.5.0" author="pradeep" created="Wed Dec 03 13:06:48 IST 2014" description="XML To JSON" guiVersion="3.4.4.P" id="1417593604462" licenseCode="Unlicensed" licenseType="Unknown" modified="Wed Dec 03 13:06:48 IST 2014" modifiedBy="pradeep" name="XML_To_JSON" preview-file="${Project_Home}/src/main/resources/employee.xml" revision="1.0" showComponentDetails="false"> 
<Global> 
<Metadata __index="0" __referenceCounter="1" __sourcePath="{}/object/contact" _dataStructure="SINGLE_DIMENSIONAL_COLLECTION" _id="__id" _metadataParentId="4430d2fe-5b06-4e39-b259-492c325fe164" _parent_id="__parent_id" _type="Output" id="f11ab87d-ec49-4034-a277-1e96a57b7925"> 
<Record fieldDelimiter="," name="contact" recordDelimiter="\n\\|\r\n\\|\r" type="delimited"> 
<Field __artificialType="_parent_id" __systemManaged="true" name="__parent_id" type="string"/> 
<Field __artificialType="_id" __systemManaged="true" name="__id" type="string"/> 
<Field __index="0" __sourcePath="{}/object/contact/name" containerType="SINGLE" label="name" name="name" type="string"/> 
<Field __index="1" __sourcePath="{}/object/contact/phone" containerType="SINGLE" label="phone" name="phone" type="string"/> 
</Record> 
</Metadata> 
<Metadata _type="Lookup" id="963db4af-a7a1-428c-be6b-39af261e93d4"> 
<Record fieldDelimiter="," name="CSV" recordDelimiter="\n\\|\r\n\\|\r" type="delimited"> 
<Field containerType="SINGLE" label="firstname" name="firstname" size="10" type="string"/> 
<Field containerType="SINGLE" eofAsDelimiter="true" label="phone" name="phone" size="10" type="string"/> 
</Record> 
</Metadata> 
<Metadata __index="0" __referenceCounter="1" __schemaType="employeeType" __sourcePath="{}/employees/employee" _dataStructure="SINGLE_DIMENSIONAL_COLLECTION" _id="__id" _metadataParentId="80f1c157-e81d-439d-afa8-e475de2dfe36" _parent_id="__parent_id" _type="Input" id="019cda9c-1078-4d00-bdcc-f81a59df5292"> 
<Record fieldDelimiter="," name="employee" recordDelimiter="\n\\|\r\n\\|\r" type="delimited"> 
<Field __artificialType="_parent_id" __systemManaged="true" name="__parent_id" type="string"/> 
<Field __artificialType="_id" __systemManaged="true" name="__id" type="string"/> 
<Field __index="0" __schemaType="employeeType" __sourcePath="{}/employees/employee/@firstname" containerType="SINGLE" label="firstname" name="firstname" type="string"/> 
<Field __index="1" __schemaType="employeeType" __sourcePath="{}/employees/employee/@lastname" containerType="SINGLE" label="lastname" name="lastname" type="string"/> 
<Field __index="2" __schemaType="employeeType" __sourcePath="{}/employees/employee/@gender" containerType="SINGLE" label="gender" name="gender" type="string"/> 
<Field __index="3" __schemaType="employeeType" __sourcePath="{}/employees/employee/@phone" containerType="SINGLE" label="phone" name="phone" type="string"/> 
<Field __index="4" __schemaType="employeeType" __sourcePath="{}/employees/employee/@text()" containerType="SINGLE" label="text()" name="text" type="string"/> 
</Record> 
</Metadata> 
<Metadata __filteredMetadata="false" __index="0" __referenceCounter="1" __schemaType="employeesType" __sourcePath="{}/employees" _dataStructure="OBJECT" _id="__id" _type="Input" id="80f1c157-e81d-439d-afa8-e475de2dfe36"> 
<Record fieldDelimiter="," name="employees" recordDelimiter="\n\\|\r\n\\|\r" type="delimited"> 
<Field __artificialType="_id" __systemManaged="true" name="__id" type="string"/> 
</Record> 
</Metadata> 
<Metadata __index="0" __referenceCounter="1" __sourcePath="{}/object" _dataStructure="OBJECT" _id="__id" _type="Output" id="4430d2fe-5b06-4e39-b259-492c325fe164"> 
<Record fieldDelimiter="," name="object" recordDelimiter="\n\\|\r\n\\|\r" type="delimited"> 
<Field __artificialType="_id" __systemManaged="true" name="__id" type="string"/> 
</Record> 
</Metadata> 
<LookupTable fileURL="D:/1_Ram/DEV/Workspace/mule-workspace/mule-datamapper-lookup/src/main/resources/staff.txt" id="CSV" key="phone" metadata="963db4af-a7a1-428c-be6b-39af261e93d4" name="CSV" type="simpleLookup"> 
<attr name="lookupType"><![CDATA[CSV]]></attr> 
</LookupTable> 
<Dictionary> 
<Entry id="DictionaryEntry0" input="true" name="inputPayload" output="false" type="object"/> 
<Entry id="DictionaryEntry1" input="false" name="outputPayload" output="true" type="object"/> 
</Dictionary> 
</Global> 
<Phase number="0"> 
<Node enabled="enabled" guiName="Foreach 'employees' -&gt; 'object'" guiX="460" guiY="20" id="FOREACH_EMPLOYEES_OBJECT" transformClass="com.mulesoft.datamapper.transform.MelRecordTransform" type="REFORMAT"> 
<attr name="melScript"><![CDATA[//MEL 
//START -> DO NOT REMOVE 
output.__id = input.__id; 
//END -> DO NOT REMOVE 
]]></attr> 
</Node> 
<Node enabled="enabled" guiName="Foreach 'employee' -&gt; 'contact'" guiX="460" guiY="120" id="FOREACH_EMPLOYEE_CONTACT" transformClass="com.mulesoft.datamapper.transform.MelRecordTransform" type="REFORMAT"> 
<attr name="melScript"><![CDATA[//MEL 
//START -> DO NOT REMOVE 
output.__id = input.__id; 
output.__parent_id = input.__parent_id; 
//END -> DO NOT REMOVE 
output.phone = input.phone + (isnull(lookup("CSV").get([input.firstname])) ? null : lookup("CSV").get([input.firstname]).phone); 
output.name = input.lastname + ' ' + input.firstname;]]></attr> 
</Node> 
<Node cacheInMemory="true" charset="UTF-8" enabled="enabled" fileURL="dict:outputPayload" guiName="JSON WRITER" guiX="900" guiY="20" id="JSON_WRITER0" type="JSON_WRITER"> 
<attr name="mapping"><![CDATA[<?xml version="1.0" encoding="UTF-8"?> 
<object xmlns:clover="http://www.cloveretl.com/ns/xmlmapping" clover:inPort="0"> 
    <clover:collection clover:name="contact"> 
    <item clover:inPort="1" clover:key="__parent_id" clover:parentKey="__id"> 
     <name>$1.name</name> 
     <phone>$1.phone</phone> 
    </item> 
    </clover:collection> 
</object>]]></attr> 
<attr name="__isJsonSchema"><![CDATA[false]]></attr> 
<attr name="_data_format"><![CDATA[JSON]]></attr> 
<attr name="jsonSchemaURL"><![CDATA[./src/main/resources/contacts.json]]></attr> 
</Node> 
<Node charset="UTF-8" dataPolicy="strict" enabled="enabled" fileURL="dict:inputPayload" guiName="XML READER" guiX="20" guiY="20" id="XML_READER0" type="XML_READER"> 
<attr name="xmlSchemaURL"><![CDATA[./src/main/resources/employees.xsd]]></attr> 
<attr name="mapping"><![CDATA[<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<Context outPort="0" sequenceField="__id" xpath="/employees"> 
    <Context generatedKey="__parent_id" outPort="1" parentKey="__id" sequenceField="__id" xpath="employee"> 
    <Mapping cloverField="firstname" trim="true" xpath="@firstname"/> 
    <Mapping cloverField="lastname" trim="true" xpath="@lastname"/> 
    <Mapping cloverField="gender" trim="true" xpath="@gender"/> 
    <Mapping cloverField="phone" trim="true" xpath="@phone"/> 
    <Mapping cloverField="text" trim="true" xpath="text()"/> 
    </Context> 
</Context> 
]]></attr> 
<attr name="_data_format"><![CDATA[XML]]></attr> 
<attr name="__rootGlobalElementName"><![CDATA[employees]]></attr> 
</Node> 
<Edge debugMode="true" fromNode="FOREACH_EMPLOYEES_OBJECT:0" guiBendpoints="" id="Edge1" inPort="Port 0 (in)" metadata="4430d2fe-5b06-4e39-b259-492c325fe164" outPort="Port 0 (out)" toNode="JSON_WRITER0:0"/> 
<Edge debugMode="true" fromNode="FOREACH_EMPLOYEE_CONTACT:0" guiBendpoints="" id="Edge3" inPort="Port 1 (in)" metadata="f11ab87d-ec49-4034-a277-1e96a57b7925" outPort="Port 0 (out)" toNode="JSON_WRITER0:1"/> 
<Edge debugMode="true" fromNode="XML_READER0:0" guiBendpoints="" id="Edge0" inPort="Port 0 (in)" metadata="80f1c157-e81d-439d-afa8-e475de2dfe36" outPort="Port 0 (out)" toNode="FOREACH_EMPLOYEES_OBJECT:0"/> 
<Edge debugMode="true" fromNode="XML_READER0:1" guiBendpoints="" id="Edge2" inPort="Port 0 (in)" metadata="019cda9c-1078-4d00-bdcc-f81a59df5292" outPort="Port 1 (out)" toNode="FOREACH_EMPLOYEE_CONTACT:0"/> 
</Phase> 
</Graph> 
+0

您能分享您的數據映射器配置嗎? – 2014-12-03 13:39:15

回答

1

我注意到你的.grf文件中的下列問題:
1. CSV記錄字段是名字和電話,而在您的實際csv文件中,他們是名稱和聯繫人(我假設您的原始文章中的CSV示例與staff.txt文件中的內容匹配)。
2.查找表鍵是電話,而您根據手機映射中的名稱查找

lookup("CSV").get([input.firstname]).phone