简体   繁体   English

Apache Camel 数据格式与类型转换器

[英]Apache Camel Data Format vs Type Converter

I read through Camel book, but can't really understand how to differentiate this two, they looks like the same, which both trying to convert a data type to another.我通读了骆驼书,但无法真正理解如何区分这两者,它们看起来相同,都试图将数据类型转换为另一种数据类型。 Anyone can further elaborate in which situation I should use specific one over another?任何人都可以进一步详细说明在哪种情况下我应该使用特定的一种而不是另一种? And any difference between these two?这两者之间有什么区别吗?

Data format数据格式

http://camel.apache.org/data-format.html http://camel.apache.org/data-format.html

Pseudo example:伪示例:

from("file://riders/inbox")
.marshal().csv()
.to("activemq:queue:inbox");

Type Converter类型转换器

http://camel.apache.org/type-converter.html http://camel.apache.org/type-converter.html

Pseudo example:伪示例:

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

If you want to convert a type to any other type, use a Type Converter.如果要将类型转换为任何其他类型,请使用类型转换器。 For example, you can convert a Cat class to a Dog class using Type Converter.例如,您可以使用类型转换器将 Cat 类转换为 Dog 类。

Marshal / Unmarshal came from the serializeable school. Marshal / Unmarshal 来自可序列化的学校。 Basically, you can map any data structure in memory to a stream of bytes (probably to save it in disk, for example).基本上,您可以将内存中的任何数据结构映射到字节流(例如,可能将其保存在磁盘中)。 Apache Camel mirrors the same (though they have expanded that to some commonly used formats like XML, HL7 etc). Apache Camel 反映了相同的内容(尽管他们已将其扩展到一些常用格式,如 XML、HL7 等)。 Those are also strings (mostly) or types which can be easily represented as a stream of bytes (protobuf for example) which can be easily serialized.这些也是字符串(主要是)或类型,可以轻松表示为可以轻松序列化的字节流(例如 protobuf)。

In Apache Camel, if you notice, if you use marshal/unmarshal, it is not generic.在 Apache Camel 中,如果您注意到,如果您使用 marshal/unmarshal,它不是通用的。 The input or output types are fixed - means you cannot convert from a type to any type.输入或输出类型是固定的 - 意味着您不能从类型转换为任何类型。 But that is not the case with Type Converter - you can convert a type to any type (either using the default type converters or you have to provide the implementation).但是类型转换器不是这种情况 - 您可以将类型转换为任何类型(使用默认类型转换器或您必须提供实现)。

You can think of marshal/unmarshal as a specific case of type conversion where the types are fixed (only Camel guys will provide the implementation usually).您可以将 marshal/unmarshal 视为类型转换的特定情况,其中类型是固定的(通常只有 Camel 人员会提供实现)。 As you have seen in your example, marshal().csv() is part of the Camel DSL.正如您在示例中所见, marshal().csv()是 Camel DSL 的一部分。

But if you use Type Converter, you are free to add your own logic.但是如果你使用 Type Converter,你可以随意添加你自己的逻辑。 That is generic.那是通用的。

Camel supports different data formats, in a pluggable way. Camel 以可插拔的方式支持不同的数据格式。 This means that Camel can marshall or unmarshall a message in a given format.这意味着 Camel 可以以给定格式编组或解组消息。 Camel natively supports Avro, JSON, protobuf, JAXB, XmlBeans, XStream, JiBX, SOAP, and so on. Camel 原生支持 Avro、JSON、protobuf、JAXB、XmlBeans、XStream、JiBX、SOAP 等。

Camel knows expected format and type of endpoints,for this camel looks for a type converter, which can transform message from one type to another.You can even use your own Type Converter like a POJO class. Camel 知道端点的预期格式和类型,因为这个骆驼寻找一个类型转换器,它可以将消息从一种类型转换为另一种类型。您甚至可以像 POJO 类一样使用自己的类型转换器。

In the below example .convertBodyTO convert the stream to a string first, we can also change the encoding of the stream by setting the charset parameter.在下面的示例中.convertBodyTO将流转换为字符串,我们还可以通过设置 charset 参数来更改流的编码。

example -例子 -

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

.marshal().csv() marshels it to csv string format .marshal().csv()将其编组为 csv 字符串格式

You can use Dataformat when you have to convert your message to specific data types like JSON,YAML,JAXB , but you cannot use DataFormat to convert your message to user defined data type.当您必须将消息转换为特定数据类型(如JSON,YAML,JAXB ,可以使用 Dataformat,但不能使用 DataFormat 将消息转换为用户定义的数据类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM