此内容没有您所选择的语言版本。

Chapter 7. avro


Avro Component

Available as of Camel 2.10
This component provides a dataformat for avro, which allows serialization and deserialization of messages using Apache Avro's bindary dataformat. Moreover, it provides support for Apache Avro's rpc, by providing producers and consumers endpoint for using avro over netty or http.
Maven users will need to add the following dependency to their pom.xml for this component:
<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-avro</artifactId>
    <version>x.x.x</version>
    <!-- use the same version as your Camel core version -->
</dependency>
Copy to Clipboard Toggle word wrap

Apache Avro Overview

Avro allows you to define message types and a protocol using a json like format and then generate java code for the specified types and messages. However, it doesn't enforce a schema first approach and you can create schema for your existing classes. An example of how a schema looks like is below.
{"namespace": "org.apache.camel.avro.generated",
 "protocol": "KeyValueProtocol",

 "types": [
     {"name": "Key", "type": "record",
      "fields": [
          {"name": "key",   "type": "string"}
      ]
     },
     {"name": "Value", "type": "record",
      "fields": [
          {"name": "value",   "type": "string"}
      ]
     }
 ],

 "messages": {
     "put": {
         "request": [{"name": "key", "type": "Key"}, {"name": "value", "type": "Value"} ],
         "response": "null"
     },
     "get": {
         "request": [{"name": "key", "type": "Key"}],
         "response": "Value"
     }
 }
}
Copy to Clipboard Toggle word wrap
You can easily generate classes from a schema, using maven, ant etc. More details can be found at the Apache Avro documentation.

Using the Avro data format

Using the avro data format is as easy as specifying that the class that you want to marshal or unmarshal in your route.
    <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
        <route>
            <from uri="direct:in"/>
            <marshal>
                <avro instanceClass="org.apache.camel.dataformat.avro.Message"/>
            </marshal>
            <to uri="log:out"/>
        </route>
    </camelContext>
Copy to Clipboard Toggle word wrap
An alternative can be to specify the dataformat inisde the context and reference it from your route.
    <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
         <dataFormats>
            <avro id="avro" instanceClass="org.apache.camel.dataformat.avro.Message"/>
        </dataFormats>
        <route>
            <from uri="direct:in"/>
            <marshal ref="avro"/>
            <to uri="log:out"/>
        </route>
    </camelContext>
Copy to Clipboard Toggle word wrap
In the same manner you can umarshal using the avro data format.

Using Avro IPC in Camel

As mentioned above Avro also provides RPC support over multiple transports such as http and netty. Camel provides consumers and producers for these two transports.
avro:[transport]:[host]:[port]
Copy to Clipboard Toggle word wrap
The supported transport values are currently http or netty.
When using camel producers for avro ipc, the "in" message body needs to contain the arguments of the operation sepcified in the avro protocol. The response will be added in the body of the "out" message.
In a similar manner when using camel avro consumers for avro ipc, the requests arguments will be placed inside the "in" message body of the created exchange and once the exchange is processed the body of the "out" message will be send as a response.

Avro IPC URI Options

Expand
Name Description
protocolClassName The class name of the avro protocol.

Avro IPC Headers

Expand
Name Description
CamelAvroMessageName The name of the message to send.

Examples

An example of using camel avro producers via http:
        <route>
            <from uri="direct:start"/>
            <to uri="avro:http:localhost:{{avroport}}?protocolClassName=org.apache.camel.avro.generated.KeyValueProtocol"/>
            <to uri="log:avro"/>
        </route>
Copy to Clipboard Toggle word wrap
An example of consuming messages using camel avro consumers via netty:
        <route>
            <from uri="avro:netty:localhost:{{avroport}}?protocolClassName=org.apache.camel.avro.generated.KeyValueProtocol"/>
            <choice>
                <when>
                    <el>${in.headers.CamelAvroMessageName == 'put'}</el>
                    <process ref="putProcessor"/>
                </when>
                <when>
                    <el>${in.headers.CamelAvroMessageName == 'get'}</el>
                    <process ref="getProcessor"/>
                </when>
            </choice>
        </route>
Copy to Clipboard Toggle word wrap
返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat