Tuesday, 26 April 2016

Fetching Data from Database and Writing to CSV File

Fetching Data from Database and Writing to CSV File
Description: Here I used Database connector of MYSQL which gets Map as output. Then I used Transformer to convert Map to CSV and stored in csv file at path specified.
Flow
MuleConfig.xml
   <jdbc-ee:mysql-data-source name="MySQL_Data_Source" user="root" password="root" url="jdbc:mysql://localhost:3306/test" transactionIsolation="UNSPECIFIED" doc:name="MySQL Data Source"/>
    <jdbc-ee:connector name="Database_Mysql" dataSource-ref="MySQL_Data_Source" validateConnections="true" queryTimeout="-1" pollingFrequency="0" doc:name="Database"/>
    <http:listener-config name="HTTP_Listener_Configuration" host="localhost" port="8089" doc:name="HTTP Listener Configuration"/>
    <db:mysql-config name="MySQL_Configuration" host="localhost" port="3306" user="root" password="root" database="test" doc:name="MySQL Configuration"/>
    <data-mapper:config name="Map_To_CSV" transformationGraphPath="map_to_csv.grf" doc:name="Map_To_CSV"/>
    <flow name="csvtodbFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/" allowedMethods="GET" doc:name="HTTP"/>
        <db:select config-ref="MySQL_Configuration" doc:name="Database">
            <db:dynamic-query/>
        </db:select>
        <logger message="#[payload]" level="INFO" doc:name="Logger"/>
        <data-mapper:transform config-ref="Map_To_CSV" metadata:id="def2d8b9-62a2-442f-8be1-956d11615b04" doc:name="Map To CSV"/>
      <file:outbound-endpoint path="C:/Users/sreddi/Desktop/output" responseTimeout="10000" doc:name="File" outputPattern="swamy.csv"/>
    </flow>
Output:
Getting Data from SalesForce
Description: Here I used salesforce connector to  fetch the data from salesforce  where I get Object. So I used transformer Object to Json and set one logger to see output.
Flow
 
MuleConfiguration.xml
<http:listener-config name="HTTP_Listener_Configuration" host="localhost" port="8081" doc:name="HTTP Listener Configuration"/>
    <sfdc:config name="Salesforce__Basic_authentication" username="khasimm@gmail.com" password="khasim#123" securityToken="AHHE7QziON3yR53WffCUx8M83t" doc:name="Salesforce: Basic authentication"/>
    <flow name="getsfdcaccdataFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/sfdc" allowedMethods="GET" doc:name="HTTP"/>
        <sfdc:query config-ref="Salesforce__Basic_authentication" query="dsql:SELECT BillingAddress,BillingCity,BillingCountry,BillingPostalCode,BillingState,Name FROM Account" doc:name="Salesforce"/>
        <json:object-to-json-transformer doc:name="Object to JSON"/>
        <logger level="INFO" doc:name="Logger" message="#[payload]"/>
Exposing Json REST Service with List of orders
Description:Here  I used RestService with Json as output. With display List of orders
Output
[
  {
    "custmer": "khasim",
    "address": "hyderabad",
    "order-no": 15,
    "bill-amount": "$2500"
  },
  {
    "custmer": "umakanth",
    "address": "Mumbai",
    "order-no": 16,
    "bill-amount": "$4000"
  }
]
Using Choice and Subflows
Description: Based on Language selected it trigger the particular flow.
If English then English Flow, if Hindi then triggers Hindi flow else default will be Telugu Flow
FLOW
 
<http:listener-config name="HTTP_Listener_Configuration" host="localhost" port="8081" doc:name="HTTP Listener Configuration"/>
    <flow name="choiceconnpocFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
        <logger level="INFO" doc:name="Logger" message="english"/>
        <set-variable variableName="language" value="english" doc:name="Variable"/>
        <choice doc:name="Choice">
            <when expression="#[flowVars.language=='english']">
                <set-payload doc:name="Set English  Payload" value="#['English'.toUpperCase()]"/>
                <flow-ref name="choiceconnpocFlow1" doc:name="English flow"/>
            </when>
            <when expression="#[flowVars.language=='hindi']">
                <set-payload doc:name="Set Hindi Payload" value="#['Hindi'.toUpperCase()]"/>
                <flow-ref name="choiceconnpocFlow2" doc:name="Hindi flow"/>
            </when>
            <otherwise>
                <set-variable doc:name="Variable" value="telugu" variableName="language"/>
                <set-payload doc:name="Set Telugu Payload" value="#['Telugu'.toUpperCase()]"/>
                <flow-ref name="choiceconnpocFlow3" doc:name="Telugu Flow"/>
            </otherwise>
        </choice>
    </flow>
    <flow name="choiceconnpocFlow1">
        <poll doc:name="Poll">
            <logger message="English Flow executed -----" level="INFO" doc:name="Logger"/>
        </poll>
        <logger level="INFO" doc:name="Logger"/>
    </flow>
    <flow name="choiceconnpocFlow3">
        <poll doc:name="Poll">
            <logger level="INFO" doc:name="Logger"/>
        </poll>
        <logger level="INFO" doc:name="Logger"/>
    </flow>
    <flow name="choiceconnpocFlow2">
        <poll doc:name="Poll">
            <logger level="INFO" doc:name="Logger"/>
        </poll>
        <logger level="INFO" doc:name="Logger"/>
    </flow>

3 comments:

  1. can you give step by detail for "Fetching Data from Database and Writing to CSV File". i am having an issue with the map to csv. thanks in advance

    ReplyDelete
    Replies
    1. Hi Khasim,

      Amaze! I have been looking Bing for hours because of this and i also in the end think it is in this article! Maybe I recommend you something helps me all the time?
      I have a flow which will read the database file and place in txt file and read another database with the same format and append to the old txt file and finally FTP to SMTP server.
      When I build the logic in local got the expected output each record is written to new new, when I deployed the same code to "cloubhub" its taking the fixed length of 140 char and writing continuously as one line (no line break).
      Not able to determine the difference between Local Vs Cloud hub.
      But great job man, do keep posted with the new updates.

      Cheers,
      Kevin

      Delete
  2. Hi Khasim,

    I love all the posts, I really enjoyed.
    I would like more information about this, because it is very nice., Thanks for sharing.

    I wrote about Kerberos support in MuleSoft, so it seems only natural to discuss the other place where Kerberos fits into the API ecosystem. Not only can Kerberos be used to secure APIs hosted or proxied by MuleSoft, but certain third-party systems can use or even require Kerberos for their security.

    By the way do you have any YouTube videos, would love to watch it. I would like to connect you on LinkedIn, great to have experts like you in my connection (In case, if you don’t have any issues).

    Please keep providing such valuable information.

    Merci Beaucoup,
    Irene Hynes

    ReplyDelete