camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > avro
Date Mon, 28 May 2012 19:47:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/2042/9/1/_/styles/combined.css?spaceKey=CAMEL&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="https://cwiki.apache.org/confluence/display/CAMEL/avro">avro</a></h2>
    <h4>Page  <b>added</b> by             <a href="https://cwiki.apache.org/confluence/display/~iocanel">Ioannis
Canellos</a>
    </h4>
         <br/>
    <div class="notificationGreySide">
         <h2><a name="avro-AvroComponent"></a>Avro Component</h2>
<p><b>Available as of Camel 2.10</b></p>

<p>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.</p>

<p>Maven users will need to add the following dependency to their <tt>pom.xml</tt>
for this component:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;dependency&gt;</span>
    <span class="code-tag">&lt;groupId&gt;</span>org.apache.camel<span
class="code-tag">&lt;/groupId&gt;</span>
    <span class="code-tag">&lt;artifactId&gt;</span>camel-avro<span
class="code-tag">&lt;/artifactId&gt;</span>
    <span class="code-tag">&lt;version&gt;</span>x.x.x<span class="code-tag">&lt;/version&gt;</span>
    <span class="code-tag"><span class="code-comment">&lt;!-- use the same
version as your Camel core version --&gt;</span></span>
<span class="code-tag">&lt;/dependency&gt;</span>
</pre>
</div></div>

<h3><a name="avro-ApacheAvroOverview"></a>Apache Avro Overview</h3>
<p>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.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
{<span class="code-quote">"namespace"</span>: <span class="code-quote">"org.apache.camel.avro.generated"</span>,
 <span class="code-quote">"protocol"</span>: <span class="code-quote">"KeyValueProtocol"</span>,

 <span class="code-quote">"types"</span>: [
     {<span class="code-quote">"name"</span>: <span class="code-quote">"Key"</span>,
<span class="code-quote">"type"</span>: <span class="code-quote">"record"</span>,
      <span class="code-quote">"fields"</span>: [
          {<span class="code-quote">"name"</span>: <span class="code-quote">"key"</span>,
  <span class="code-quote">"type"</span>: <span class="code-quote">"string"</span>}
      ]
     },
     {<span class="code-quote">"name"</span>: <span class="code-quote">"Value"</span>,
<span class="code-quote">"type"</span>: <span class="code-quote">"record"</span>,
      <span class="code-quote">"fields"</span>: [
          {<span class="code-quote">"name"</span>: <span class="code-quote">"value"</span>,
  <span class="code-quote">"type"</span>: <span class="code-quote">"string"</span>}
      ]
     }
 ],

 <span class="code-quote">"messages"</span>: {
     <span class="code-quote">"put"</span>: {
         <span class="code-quote">"request"</span>: [{<span class="code-quote">"name"</span>:
<span class="code-quote">"key"</span>, <span class="code-quote">"type"</span>:
<span class="code-quote">"Key"</span>}, {<span class="code-quote">"name"</span>:
<span class="code-quote">"value"</span>, <span class="code-quote">"type"</span>:
<span class="code-quote">"Value"</span>} ],
         <span class="code-quote">"response"</span>: <span class="code-quote">"null"</span>
     },
     <span class="code-quote">"get"</span>: {
         <span class="code-quote">"request"</span>: [{<span class="code-quote">"name"</span>:
<span class="code-quote">"key"</span>, <span class="code-quote">"type"</span>:
<span class="code-quote">"Key"</span>}],
         <span class="code-quote">"response"</span>: <span class="code-quote">"Value"</span>
     }
 }
}
</pre>
</div></div>

<p>You can easily generate classes from a schema, using maven, ant etc. More details
can be found at the <a href="http://avro.apache.org/docs/current/" class="external-link"
rel="nofollow">Apache Avro documentation</a>.</p>

<h3><a name="avro-UsingtheAvrodataformat"></a>Using the Avro data format</h3>
<p>Using the avro data format is as easy as specifying that the class that you want
to marshal or unmarshal in your route.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
    <span class="code-tag">&lt;camelContext id=<span class="code-quote">"camel"</span>
xmlns=<span class="code-quote">"http://camel.apache.org/schema/spring"</span>&gt;</span>
        <span class="code-tag">&lt;route&gt;</span>
            <span class="code-tag">&lt;from uri=<span class="code-quote">"direct:in"</span>/&gt;</span>
            <span class="code-tag">&lt;marshal&gt;</span>
                <span class="code-tag">&lt;avro instanceClass=<span class="code-quote">"org.apache.camel.dataformat.avro.Message"</span>/&gt;</span>
            <span class="code-tag">&lt;/marshal&gt;</span>
            <span class="code-tag">&lt;to uri=<span class="code-quote">"log:out"</span>/&gt;</span>
        <span class="code-tag">&lt;/route&gt;</span>
    <span class="code-tag">&lt;/camelContext&gt;</span>
</pre>
</div></div>

<p>An alternative can be to specify the dataformat inisde the context and reference
it from your route.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
    <span class="code-tag">&lt;camelContext id=<span class="code-quote">"camel"</span>
xmlns=<span class="code-quote">"http://camel.apache.org/schema/spring"</span>&gt;</span>
         <span class="code-tag">&lt;dataFormats&gt;</span>
            <span class="code-tag">&lt;avro id=<span class="code-quote">"avro"</span>
instanceClass=<span class="code-quote">"org.apache.camel.dataformat.avro.Message"</span>/&gt;</span>
        <span class="code-tag">&lt;/dataFormats&gt;</span>
        <span class="code-tag">&lt;route&gt;</span>
            <span class="code-tag">&lt;from uri=<span class="code-quote">"direct:in"</span>/&gt;</span>
            <span class="code-tag">&lt;marshal ref=<span class="code-quote">"avro"</span>/&gt;</span>
            <span class="code-tag">&lt;to uri=<span class="code-quote">"log:out"</span>/&gt;</span>
        <span class="code-tag">&lt;/route&gt;</span>
    <span class="code-tag">&lt;/camelContext&gt;</span>
</pre>
</div></div>  

<p>In the same manner you can umarshal using the avro data format.</p>

<h3><a name="avro-UsingAvroIPCinCamel"></a>Using Avro IPC in Camel</h3>
<p>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.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
avro:[transport]:[host]:[port]
</pre>
</div></div> 

<p>The supported transport values are currently http or netty.</p>

<p>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.</p>

<p>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.</p>

<h3><a name="avro-AvroIPCURIOptions"></a>Avro IPC URI Options</h3>

<div class="confluenceTableSmall"><div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Name </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>protocolClassName</tt> </td>
<td class='confluenceTd'> The class name of the avro protocol. </td>
</tr>
</tbody></table>
</div>
</div>

<h3><a name="avro-AvroIPCHeaders"></a>Avro IPC Headers</h3>

<div class="confluenceTableSmall"><div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Name </th>
<th class='confluenceTh'>  Description </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>CamelAvroMessageName</tt> </td>
<td class='confluenceTd'> The name of the message to send. </td>
</tr>
</tbody></table>
</div>
</div>


<h3><a name="avro-Examples"></a>Examples</h3>

<p>An example of using camel avro producers via http:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
        <span class="code-tag">&lt;route&gt;</span>
            <span class="code-tag">&lt;from uri=<span class="code-quote">"direct:start"</span>/&gt;</span>
            <span class="code-tag">&lt;to uri=<span class="code-quote">"avro:http:localhost:{{avroport}}?protocolClassName=org.apache.camel.avro.generated.KeyValueProtocol"</span>/&gt;</span>
            <span class="code-tag">&lt;to uri=<span class="code-quote">"log:avro"</span>/&gt;</span>
        <span class="code-tag">&lt;/route&gt;</span>
</pre>
</div></div>

<p>An example of consuming messages using camel avro consumers via netty:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
        <span class="code-tag">&lt;route&gt;</span>
            <span class="code-tag">&lt;from uri=<span class="code-quote">"avro:netty:localhost:{{avroport}}?protocolClassName=org.apache.camel.avro.generated.KeyValueProtocol"</span>/&gt;</span>
            <span class="code-tag">&lt;choice&gt;</span>
                <span class="code-tag">&lt;when&gt;</span>
                    <span class="code-tag">&lt;el&gt;</span>${in.headers.CamelAvroMessageName
== 'put'}<span class="code-tag">&lt;/el&gt;</span>
                    <span class="code-tag">&lt;process ref=<span class="code-quote">"putProcessor"</span>/&gt;</span>
                <span class="code-tag">&lt;/when&gt;</span>
                <span class="code-tag">&lt;when&gt;</span>
                    <span class="code-tag">&lt;el&gt;</span>${in.headers.CamelAvroMessageName
== 'get'}<span class="code-tag">&lt;/el&gt;</span>
                    <span class="code-tag">&lt;process ref=<span class="code-quote">"getProcessor"</span>/&gt;</span>
                <span class="code-tag">&lt;/when&gt;</span>
            <span class="code-tag">&lt;/choice&gt;</span>
        <span class="code-tag">&lt;/route&gt;</span>
</pre>
</div></div>
    </div>
    <div id="commentsSection" class="wiki-content pageSection">
       <div style="float: right;">
            <a href="https://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
       </div>
       <a href="https://cwiki.apache.org/confluence/display/CAMEL/avro">View Online</a>
              |
       <a href="https://cwiki.apache.org/confluence/display/CAMEL/avro?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
           </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message