Apache Camel - 7 - Camel数据转换

Data transformation overview(数据转换概述)

Apache Camel相关代码已经上传GitHub,需要的自取:GitHub - Apache Camel 完整Demo

如果觉得还行,麻烦点个Star

 

注:下面的例子,都是书中的例子,我没有正式的写过代码

Camel provides many techniques for data transformation, and we’ll cover them shortly. But first we’ll start with an overview of data transformation in Camel.

Data transformation is a broad term that covers two types of transformation:

■ Data format transformation—The data format of the message body is transformed from one form to another. For example, a CSV record is formatted as XML.

■ Data type transformation—The data type of the message body is transformed from one type to another. For example a java.lang.String is transformed into a javax.jms.TextMessage.

Figure 3.1 illustrates the principle of transforming a message body from one form into another. This transformation can involve any combination of format and type transformations. In most cases, the data transformation you’ll face with Camel is format

transformation, where you have to mediate between two protocols. Camel has a builtin type-converter mechanism that can automatically convert between types, which greatly reduces the need for end users to deal with type transformations.

 

Camel提供了许多数据转换技术,我们很快就会介绍。但首先我们将从Camel数据转换的概述开始。

数据转换是一个广泛的术语,涵盖两种类型的转换:

■数据格式转换 - 消息体的数据格式从一种形式转换为另一种形式。例如,CSV记录被格式化为XML。

■数据类型转换 - 消息正文的数据类型从一种类型转换为另一种类型。例如,一个java.lang.String被转换成一个javax.jms.TextMessage。

图3.1说明了将消息体从一种形式转换成另一种形式的原理。这种转换可以涉及格式和类型转换的任何组合。在大多数情况下,你将要面对Camel的数据转换是格式

转换,你必须在两个协议之间进行调解。Camel有一个内置的类型转换机制,可以自动转换类型,大大减少了终端用户处理类型转换的需要。

Camel offers many features for transforming data from one form to another.

Camel 提供了很多功能来将数据从一种形式转换为另一种形式。

 

 

Data transformation with Camel(用Camel进行数据转换)

TRANSFORMING USING A PROCESSOR(使用process处理器进行转换)

 

贴一个书中的样例:

这个转换,是基于Camel的入口

 

import org.apache.camel.Exchange;
import org.apache.camel.Processor;
public class OrderToCsvProcessor implements Processor {
	
	public void process(Exchange exchange) throws Exception {
	
		String custom = exchange.getIn().getBody(String.class);
		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());
				
		}
		
		exchange.getIn().setBody(csv.toString());
	
	}
}

在路由中设置如下:

 

 

from("quartz://report?cron=0+0+6+*+*+?")

.to("http://riders.com/orders/cmd=received&date=yesterday")

.process(new OrderToCsvProcessor())

.to("file://riders/orders?fileName=report-${header.Date}.csv");

 

 

 

TRANSFORMING USING BEANS(使用bean进行转换)

再来看一个例子:

public class OrderToCsvBean {
	
	public static 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();
			
	}
}

 

 

在路由中的设置如下:

 

from("quartz://report?cron=0+0+6+*+*+?")

.to("http://riders.com/orders/cmd=received&date=yesterday")

.bean(new OrderToCsvBean())

.to("file://riders/orders?fileName=report-${header.Date}.csv");

 

 

The first noticeable difference between listings 3.1 and 3.2 is that listing 3.2 doesn’t use any Camel imports. This means your bean is totally independent of the Camel API.

The next difference is that you can name the method signature in listing 3.2—in this case it’s a static method named map.

The method signature defines the contract, which means that the first parameter, (String custom), is the message body you’re going to use for translation. The method returns a String, which means the translated data will be a String type. At runtime, Camel binds to this method signature. We won’t go into any more details here; we’ll cover much more about using beans in chapter 4.

The actual mapping B is the same as with the processor. At the end, you return the mapping output C.

上面两个例子中的第一个明显区别是使用Bean方法不使用任何Camel进口。

这意味着你的bean完全独立于Camel API。

下一个区别是你可以在Bean方法签名 - 在这种情况下,它是一个名为map的静态方法。

方法签名定义了契约,这意味着第一个参数(字符串自定义)是您要用于翻译的消息主体。

该方法返回一个字符串,这意味着翻译的数据将是一个字符串类型。 在运行时,Camel绑定到这个方法签名。 我们不会在这里再详述。 我们将在第四章介绍更多有关使用bean的内容。

实际的映射与处理器相同。 最后,你返回映射输出.

 

 

 

TRANSFORMING USING THE TRANSFORM() METHOD FROM THE JAVA DSL

(使用JAVA DSL中的TRANSFORM()方法进行转换)

 

Transform() is a method in the Java DSL that can be used in Camel routes to transform messages. By allowing the use of expressions, transform() permits great flexibility, and using expressions directly within the DSL can sometimes save time. Let’s look at a little example.

使用JAVA DSL中的TRANSFORM()方法进行转换

Transform()是Java DSL中的一种方法,可用于Camel路由来转换消息。 通过允许使用表达式,transform()允许很大的灵活性,并且在DSL中直接使用表达式有时可以节省时间。 我们来看一个小例子。

 

我们来看一个样例:

 

from("direct:start")

.transform(body().regexReplaceAll("\n", "<br/>"))

.to("mock:result");

 

 

 

What this route does is use the transform() method to tell Camel that the message should be transformed using an expression. Camel provides what is know as the Builder pattern to build expressions from individual expressions. This is done by chaining together method calls, which is the essence of the Builder pattern.

这个路由所做的是使用transform()方法告诉Camel应该使用表达式来转换消息。 Camel提供了Builder模式从构建表达式的知识。 这是通过链接在一起的方法调用完成的,这是Builder模式的本质。

 

 

Using Camel type converters(使用Camel型转换器)

As we mentioned, the Camel type converters are used throughout Camel, often automatically. But you might want to use them to force a specific type to be used in a route, such as before sending data back to a caller or a JMS destination. Let’s look at how to do that.

Suppose you need to route files to a JMS queue using javax.jmx.TextMessage. To do so, you can convert each file to a String, which forces the JMS component to use TextMessage. This is easy to do in Camel—you use the convertBodyTo method, as shown here:

 

正如我们所提到的,转换器通常都是自动使用的。 但是您可能想要使用它们来强制在路由中使用特定的类型,例如在将数据发送回调用方或JMS目的地之前。 让我们看看如何做到这一点。

假设您需要使用javax.jmx.TextMessage将文件路由到JMS队列。 为此,可以将每个文件转换为一个字符串,这将强制JMS组件使用TextMessage。 这在Camel中很容易实现 - 您可以使用convertBodyTo方法,如下所示:

 

from("file://riders/inbox").convertBodyTo(String.class).to("activemq:queue:inbox");

 

 

 

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值