我有一个用于查询SQL服务器数据库的mule流,然后将响应转换为JSON,然后将JSON逗号分隔为文件。
我的问题是写入文件中列的顺序与查询中字段的顺序不匹配。如何定义查询答案中列的顺序?
JSON到CSV的转换如下:
public class Bean2CSV {
/**
* @param args
* @throws JSONException
*/
public String conv2csv(Object input) throws JSONException {
if (!input.equals(null)){
String inputstr = (String)input;
JSONArray jsonArr = new JSONArray(inputstr);
String csv = CDL.toString(jsonArr);
csv = csv.replace(',', ';');
System.out.println(inputstr); //csv
System.out.println(csv); //csv
return csv;
} else return "";}
}
这是流程
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:json="http://www.mulesoft.org/schema/mule/json"
xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns:file="http://www.mulesoft.org/schema/mule/file"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.mulesoft.org/schema/mule/jdbc"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:core="http://www.mulesoft.org/schema/mule/core" version="CE-3.4.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/json
http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-
http.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.mulesoft.org/schema/mule/jdbc http://www.mulesoft.org/schema/mule/jdbc/current/mule-jdbc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring- beans-current.xsd
http://www.mulesoft.org/schema/mule/core
http://www.mulesoft.org/schema/mule/core/current/mule.xsd">
<context:property-placeholder location="classpath:cognos_import.properties" ignore-resource-not-found="true"/>
<spring:beans>
<spring:bean id="dataSource" name="dataSource" class="org.enhydra.jdbc.standard.StandardDataSource" destroy-method="shutdown">
<spring:property name="driverName" value="net.sourceforge.jtds.jdbc.Driver"/>
<spring:property name="user" value="${dbusername}"/>
<spring:property name="password" value="${dbpassword}"/>
</spring:bean>
<spring:bean id="changeDB" class="ChangeDatabase">
<spring:property name="serverip" value="${dbserverip}"/>
<spring:property name="serverport" value="${dbserverport}"/>
<spring:property name="dbprefix" value="${dbprefix}"/>
</spring:bean>
</spring:beans>
<jdbc:connector name="db_conn" dataSource-ref="dataSource" validateConnections="false" pollingFrequency="5000" doc:name="Database">
<jdbc:query key="readbal" value="SELECT #[header:INBOUND:company] as company, AcNo as Konto , R2 as Avd, #[header:INBOUND:period] as Period, sum(AcAm) as Saldo FROM AcTr WHERE (AcYrPr <= #[header:INBOUND:period]) and (AcNo < 3000) GROUP BY AcNo,R2 ORDER BY AcNo,R2;"/>
<jdbc:query key="readres" value="SELECT #[header:INBOUND:company] as company, AcNo as Konto , R2 as Avd, #[header:INBOUND:period] as Period, sum(AcAm) as Saldo FROM AcTr WHERE (AcYrPr <= #[header:INBOUND:period]) and (AcYrPr >= #[header:INBOUND:starttid]) and (AcNo >= 3000) GROUP BY AcNo,R2 ORDER BY AcNo,R2"/>
<jdbc:query key="readiclr" value="SELECT #[header:INBOUND:company] as company, AcTr.AcNo as Konto , AcTr.R2 as Avd, #[header:INBOUND:period] as Period, sum(AcTr.AcAm) as Saldo,sum(AcTr.CurAm) as ValutaBel, AcTr.Cur as Valuta, AcTr.R1 as FtgID FROM AcTr LEFT JOIN Actor ON AcTr.R1=Actor.R1 WHERE (AcTr.AcYrPr <= #[header:INBOUND:period]) and (AcTr.AcYrPr >= #[header:INBOUND:starttid]) and Actor.SAcSet=2 and (AcTr.AcNo=3019 or AcTr.AcNo=3099 or AcTr.AcNo=3199 or AcTr.AcNo=3299 or AcTr.AcNo=3499 or AcTr.AcNo=3519 or AcTr.AcNo=3599 or AcTr.AcNo=3699 or AcTr.AcNo=3799 or AcTr.AcNo=3919 or AcTr.AcNo=3999 or AcTr.AcNo=4299 or AcTr.AcNo=4399 or AcTr.AcNo=4599 or AcTr.AcNo=4699 or AcTr.AcNo=4799 or AcTr.AcNo=5099 or AcTr.AcNo=5299 or AcTr.AcNo=5499 or AcTr.AcNo=5699 or AcTr.AcNo=5799 or AcTr.AcNo=5999 or AcTr.AcNo=6099 or AcTr.AcNo=6399 or AcTr.AcNo=6499 or AcTr.AcNo=6999 or AcTr.AcNo=7999 or AcTr.AcNo=8399 or AcTr.AcNo=8499) and AcTr.Sup<>0 GROUP BY AcTr.AcNo,AcTr.R2,AcTr.R1,AcTr.Cur"/>
<jdbc:query key="readickr" value="SELECT #[header:INBOUND:company] as company, AcTr.AcNo as Konto , AcTr.R2 as Avd, #[header:INBOUND:period] as Period, sum(AcTr.AcAm) as Saldo,sum(AcTr.CurAm) as ValutaBel, AcTr.Cur as Valuta, AcTr.R1 as FtgID FROM AcTr LEFT JOIN Actor ON AcTr.R1=Actor.R1 WHERE (AcTr.AcYrPr <= #[header:INBOUND:period]) and (AcTr.AcYrPr >= #[header:INBOUND:starttid]) and Actor.CAcSet=2 and (AcTr.AcNo=3019 or AcTr.AcNo=3099 or AcTr.AcNo=3199 or AcTr.AcNo=3299 or AcTr.AcNo=3499 or AcTr.AcNo=3519 or AcTr.AcNo=3599 or AcTr.AcNo=3699 or AcTr.AcNo=3799 or AcTr.AcNo=3919 or AcTr.AcNo=3999 or AcTr.AcNo=4299 or AcTr.AcNo=4399 or AcTr.AcNo=4599 or AcTr.AcNo=4699 or AcTr.AcNo=4799 or AcTr.AcNo=5099 or AcTr.AcNo=5299 or AcTr.AcNo=5499 or AcTr.AcNo=5699 or AcTr.AcNo=5799 or AcTr.AcNo=5999 or AcTr.AcNo=6099 or AcTr.AcNo=6399 or AcTr.AcNo=6499 or AcTr.AcNo=6999 or AcTr.AcNo=7999 or AcTr.AcNo=8399 or AcTr.AcNo=8499) and AcTr.Cust<>0 GROUP BY AcTr.AcNo,AcTr.R2,AcTr.R1,AcTr.Cur"/>
<jdbc:query key="readiclb" value="SELECT #[header:INBOUND:company] as company, AcTr.AcNo as Konto, AcTr.R2 as Avd, #[header:INBOUND:period] as Period, sum(AcTr.AcAm) as Saldo, sum(AcTr.CurAm) as ValutaBel, AcTr.Cur as Valuta, AcTr.R1 as FtgID FROM AcTr LEFT JOIN Actor ON AcTr.R1=Actor.R1 WHERE (AcTr.AcYrPr <= #[header:INBOUND:period]) and Actor.SAcSet=2 and AcTr.Sup<>0 and (AcTr.AcNo=1511 or AcTr.AcNo=2441) GROUP BY AcTr.AcNo,AcTr.R2,AcTr.R1,AcTr.Cur"/>
<jdbc:query key="readickb" value="SELECT #[header:INBOUND:company] as company, AcTr.AcNo as Konto, AcTr.R2 as Avd, #[header:INBOUND:period] as Period, sum(AcTr.AcAm) as Saldo, sum(AcTr.CurAm) as ValutaBel, AcTr.Cur as Valuta, AcTr.R1 as FtgID FROM AcTr LEFT JOIN Actor ON AcTr.R1=Actor.R1 WHERE (AcTr.AcYrPr <= #[header:INBOUND:period]) and Actor.CAcSet=2 and AcTr.Cust<>0 and (AcTr.AcNo=1511 or AcTr.AcNo=2441) GROUP BY AcTr.AcNo,AcTr.R2,AcTr.R1,AcTr.Cur"/>
</jdbc:connector>
<file:connector name="output" outputPattern=" #[function:datestamp:dd-MM-yy-hh-mm-ss]#[header:INBOUND:company].txt" autoDelete="false" outputAppend="true" streaming="false" validateConnections="false" doc:name="File"/>
<message-properties-transformer name="delete-content-type-header" doc:name="Message Properties">
<delete-message-property key="Content-type"/>
</message-properties-transformer>
<message-properties-transformer name="add-csv-content-type-header" doc:name="Message Properties">
<add-message-property key="Content-Type" value="text/csv"/>
</message-properties-transformer>
<message-properties-transformer name="add-filename" doc:name="Message Properties">
<add-message-property key="Content-Disposition" value="attachment; filename=testfil.txt"/>
</message-properties-transformer>
<flow name="CD-test1Flow1" doc:name="CD-test1Flow1">
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8082" doc:name="HTTP"/>
<not-filter doc:name="Not">
<wildcard-filter pattern="*favicon*" caseSensitive="true"/>
</not-filter>
<logger message="Log1 #[message.getPayload()]! " level="INFO" doc:name="Logger1"/>
<http:body-to-parameter-map-transformer doc:name="Body to Parameter Map"/>
<component class="ValidationService3x" doc:name="Java"/>
<echo-component doc:name="Echo"/>
<set-variable variableName="Filename" value=" #[function:datestamp:dd-MM-yy-hh-mm-ss]-#[header:INBOUND:company]-#[header:INBOUND:period]" doc:name="FilenameVar"/>
<component doc:name="Java">
<spring-object bean="changeDB"/>
</component>
<all doc:name="All">
<processor-chain>
<jdbc:outbound-endpoint exchange-pattern="request-response" queryKey="readbal" connector-ref="db_conn" doc:name="DB"/>
<expression-filter expression="payload.size() > 0" doc:name="not empty"/>
<json:object-to-json-transformer doc:name="Object to JSON"/>
<component class="Bean2CSV" doc:name="Java"/>
<echo-component doc:name="Echo"/>
<file:outbound-endpoint path="${outputfolder}" outputPattern="#[flowVars['Filename']]-bal.csv" connector-ref="output" doc:name="File"/>
</processor-chain>
<processor-chain>
<jdbc:outbound-endpoint exchange-pattern="request-response" queryKey="readres" connector-ref="db_conn" doc:name="DB100-ickr"/>
<expression-filter expression="payload.size() > 0" doc:name="not empty"/>
<json:object-to-json-transformer doc:name="Object to JSON"/>
<component class="Bean2CSV" doc:name="Java"/>
<echo-component doc:name="Echo"/>
<file:outbound-endpoint path="${outputfolder}" outputPattern="#[flowVars['Filename']]-res.csv" connector-ref="output" doc:name="File"/>
</processor-chain>
<processor-chain>
<jdbc:outbound-endpoint exchange-pattern="request-response" queryKey="readiclb" connector-ref="db_conn" doc:name="DB100-ickr"/>
<expression-filter expression="payload.size() > 0" doc:name="not empty"/>
<json:object-to-json-transformer doc:name="Object to JSON"/>
<component class="Bean2CSV" doc:name="Java"/>
<echo-component doc:name="Echo"/>
<file:outbound-endpoint path="${outputfolder}" outputPattern="#[flowVars['Filename']]-icb.csv" connector-ref="output" doc:name="File"/>
</processor-chain>
<processor-chain>
<jdbc:outbound-endpoint exchange-pattern="request-response" queryKey="readickb" connector-ref="db_conn" doc:name="DB100-ickr"/>
<expression-filter expression="payload.size() > 0" doc:name="not empty"/>
<json:object-to-json-transformer doc:name="Object to JSON"/>
<component class="Bean2CSVnotfirstline" doc:name="Java"/>
<echo-component doc:name="Echo"/>
<file:outbound-endpoint path="${outputfolder}" outputPattern="#[flowVars['Filename']]-icb.csv" connector-ref="output" doc:name="File"/>
</processor-chain>
<processor-chain>
<jdbc:outbound-endpoint exchange-pattern="request-response" queryKey="readiclr" connector-ref="db_conn" doc:name="DB100-ickr"/>
<expression-filter expression="payload.size() > 0" doc:name="not empty"/>
<json:object-to-json-transformer doc:name="Object to JSON"/>
<component class="Bean2CSV" doc:name="Java"/>
<echo-component doc:name="Echo"/>
<file:outbound-endpoint path="${outputfolder}" outputPattern="#[flowVars['Filename']]-icr.csv" connector-ref="output" doc:name="File"/>
</processor-chain>
<processor-chain>
<jdbc:outbound-endpoint exchange-pattern="request-response" queryKey="readickr" connector-ref="db_conn" doc:name="DB100-ickr"/>
<expression-filter expression="payload.size() > 0" doc:name="not empty"/>
<json:object-to-json-transformer doc:name="Object to JSON"/>
<component class="Bean2CSVnotfirstline" doc:name="Java"/>
<echo-component doc:name="Echo"/>
<file:outbound-endpoint path="${outputfolder}" outputPattern="#[flowVars['Filename']]-icr.csv" connector-ref="output" doc:name="File"/>
</processor-chain>
</all>
<set-payload value="Nu är 100 tankat till #[flowVars['Filename']] " doc:name="Set Payload"/>
<http:response-builder doc:name="HTTP Response Builder"/>
</flow>