Camel Data Conversion

Source: Internet
Author: User
Tags apache camel

During system integration, an essential task is to convert data from one format to another, and then send the converted format to the target system:

The message translator provided by camel can be divided:

■ Using a processor
■ Using beans
■ Using <transform>

1. The example of getting started with Apache camel framework using processor has already introduced .blog.csdn.net/kkdelta/article/details/7231640

By implementing the processor method process (exchange), the common step is to get data from the inbound message, convert the data, and put it in the outbound message.

Xxx DATA = exchange. getin (). getbody (XXX. Class );

// Conversion

Exchange. getout (). setbody (yyyobject );

2. Using Bean, write a Java Bean as follows:

Public class ordertocsvbean {
Public String map (string custom ){
String id = custom. substring (0, 9 );
String customerid = custom. substring (10, 19 );
String date = custom. substring (20, 29 );
String items = custom. substring (30 );
String [] itemids = items. Split ("@");

Stringbuilder CSV = new stringbuilder ();
CSV. append (Id. Trim ());
CSV. append (","). append (date. Trim ());
CSV. append (","). append (customerid. Trim ());
For (string item: itemids ){
CSV. append (","). append (item. Trim ());
}
Return CSV. tostring ();
}
}

The code in route is as follows:

From ("file: D:/temp/inbox1 /? Delay= 30000 "). Bean (New ordertocsvbean (). To (" file: D:/temp/outbox1 ");

Camel has a set of algorithms to choose which method to call in bean (for example, whether the method name is set through camelbeanmethodname in the message header, and whether the bean has only one method, whether @ handler annotation is added to a method). The bean here has only one map method, and camel will call this method.

3. Use the defined transform method in route, which is rarely used.

From ("Direct: Start"). Transform (body (). regexreplaceall ("\ n", "<br/>"). To ("mock: result ");

4. Camel integration parses many common data formats: the parsed results can be obtained by calling the unmarshal method.

CSV:

From ("file: D:/temp/inbox1 /? Delay = 30000 "pai.unmarshal().csv (). Process (processor). To (" file: D:/temp/outbox1 ");

The parsed data can be directly obtained in processor:

Public class csvprocessor implements processor {
@ Override
Public void process (exchange) throws exception {
System. Out. println ("process ...");
List csvdata = (list) Exchange. getin (). getbody ();
Stringbuffer strbf = new stringbuffer ();
If (csvdata. Get (0). getclass (). Equals (string. Class) {// single line CSV
Strbf. append (parseorder (csvdata ));
} Else {// multiple line CSV
For (list <string> csvorder: (list <string>) csvdata ){
Strbf. append (parseorder (csvorder ));
}
}
Exchange. getout (). setbody (strbf. tostring ());
}

Private string parseorder (list <string> csvdata ){
System. Out. println (csvdata. Get (0) + "--" + csvdata. Get (1) + "--" + csvdata. Get (2 ));
Return csvdata. Get (0) + "--" + csvdata. Get (1) + "--" + csvdata. Get (2 );
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.