blob: 628213d9b2b637353f74ad7ae9fa3f6045f91105 [file] [log] [blame]
[[avro-dataformat]]
= Avro DataFormat
:page-source: components/camel-avro/src/main/docs/avro-dataformat.adoc
*Available as of Camel version 2.14*
This component provides a dataformat for avro, which allows
serialization and deserialization of messages using Apache Avro's binary
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:
[source,xml]
------------------------------------------------------------
<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>
------------------------------------------------------------
== 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.
An example of how a schema looks like is below.
[source,xml]
------------------------------------------------------------------------------------------
{"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"
}
}
}
------------------------------------------------------------------------------------------
You can easily generate classes from a schema, using maven, ant etc.
More details can be found at the
http://avro.apache.org/docs/current/[Apache Avro documentation].
However, it doesn't enforce a schema first approach and you can create
schema for your existing classes. *Since 2.12* you can use existing
protocol interfaces to make RCP calls. You should use interface for the
protocol itself and POJO beans or primitive/String classes for parameter
and result types. Here is an example of the class that corresponds to
schema above:
[source,java]
--------------------------------------------------------------
package org.apache.camel.avro.reflection;
public interface KeyValueProtocol {
void put(String key, Value value);
Value get(String key);
}
class Value {
private String value;
public String getValue() { return value; }
public void setValue(String value) { this.value = value; }
}
--------------------------------------------------------------
_Note: Existing classes can be used only for RPC (see below), not in
data format._
== 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.
[source,xml]
--------------------------------------------------------------------------------
<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>
--------------------------------------------------------------------------------
An alternative can be to specify the dataformat inside the context and
reference it from your route.
[source,xml]
--------------------------------------------------------------------------------------
<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><custom ref="avro"/></marshal>
<to uri="log:out"/>
</route>
</camelContext>
--------------------------------------------------------------------------------------
In the same manner you can umarshal using the avro data format.
== Avro Dataformat Options
// dataformat options: START
The Avro dataformat supports 2 options, which are listed below.
[width="100%",cols="2s,1m,1m,6",options="header"]
|===
| Name | Default | Java Type | Description
| instanceClassName | | String | Class name to use for marshal and unmarshalling
| contentTypeHeader | false | Boolean | Whether the data format should set the Content-Type header with the type from the data format if the data format is capable of doing so. For example application/xml for data formats marshalling to XML, or application/json for data formats marshalling to JSon etc.
|===
// dataformat options: END
// spring-boot-auto-configure options: START
== Spring Boot Auto-Configuration
When using Spring Boot make sure to use the following Maven dependency to have support for auto configuration:
[source,xml]
----
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-avro-starter</artifactId>
<version>x.x.x</version>
<!-- use the same version as your Camel core version -->
</dependency>
----
The component supports 15 options, which are listed below.
[width="100%",cols="2,5,^1,2",options="header"]
|===
| Name | Description | Default | Type
| *camel.component.avro.basic-property-binding* | Whether the component should use basic property binding (Camel 2.x) or the newer property binding with additional capabilities | false | Boolean
| *camel.component.avro.configuration.host* | Hostname to use | | String
| *camel.component.avro.configuration.message-name* | The name of the message to send. | | String
| *camel.component.avro.configuration.port* | Port number to use | | Integer
| *camel.component.avro.configuration.protocol* | Avro protocol to use | | Protocol
| *camel.component.avro.configuration.protocol-class-name* | Avro protocol to use defined by the FQN class name | | String
| *camel.component.avro.configuration.protocol-location* | Avro protocol location | | String
| *camel.component.avro.configuration.reflection-protocol* | If protocol object provided is reflection protocol. Should be used only with protocol parameter because for protocolClassName protocol type will be auto detected | false | Boolean
| *camel.component.avro.configuration.single-parameter* | If true, consumer parameter won't be wrapped into array. Will fail if protocol specifies more then 1 parameter for the message | false | Boolean
| *camel.component.avro.configuration.transport* | Transport to use, can be either http or netty | | AvroTransport
| *camel.component.avro.configuration.uri-authority* | Authority to use (username and password) | | String
| *camel.component.avro.enabled* | Enable avro component | true | Boolean
| *camel.dataformat.avro.content-type-header* | Whether the data format should set the Content-Type header with the type from the data format if the data format is capable of doing so. For example application/xml for data formats marshalling to XML, or application/json for data formats marshalling to JSon etc. | false | Boolean
| *camel.dataformat.avro.enabled* | Enable avro dataformat | true | Boolean
| *camel.dataformat.avro.instance-class-name* | Class name to use for marshal and unmarshalling | | String
|===
// spring-boot-auto-configure options: END
ND