`

Getting started with Avro RPC

    博客分类:
  • avro
阅读更多

转:

 http://gbif.blogspot.com/2011/06/getting-started-with-avro-rpc.html

Getting started with Avro RPC


Apache Avro is a data exchange format started by Doug Cutting of Lucene and Hadoop fame. A good introduction to Avro is on the cloudera blog so an introduction is not the intention of this post. 

Avro is surprisingly difficult to get into, as it is lacking the most basic "getting started" documentation for a new-comer to the project. This post serves as a reminder to myself of what I did, and hopefully to help others get the hello world working quickly. If people find it useful, let's fill it out and submit it to the Avro wiki!

Prerequisites: knowledge of Apache Maven

Start by adding the Avro maven plugin to the pom. This is needed to compile the Avro schema definitions into the Java classes.

<plugin>
  <groupId>org.apache.avro</groupId>
  <artifactId>avro-maven-plugin</artifactId>
  <version>1.5.1</version>
  <executions>
    <execution>
      <id>schemas</id>
      <phase>generate-sources</phase>
      <goals>
        <goal>schema</goal>
        <goal>protocol</goal>
        <goal>idl-protocol</goal>
      </goals>
      <configuration>
        <excludes>
          <exclude>**/mapred/tether/**</exclude>
        </excludes>
        <sourceDirectory>${project.basedir}/src/main/avro/</sourceDirectory>
        <outputDirectory>${project.basedir}/src/main/java/</outputDirectory>
        <testSourceDirectory>${project.basedir}/src/test/avro/</testSourceDirectory>
        <testOutputDirectory>${project.basedir}/src/test/java/</testOutputDirectory>
      </configuration>
    </execution>
  </executions>
</plugin>

Now add the dependency on Avro and the Avro IPC (Inter Process Calls) 

<dependency>
  <groupId>org.apache.avro</groupId>
  <artifactId>avro</artifactId>
  <version>1.5.1</version>
</dependency>
<dependency>
  <groupId>org.apache.avro</groupId>
  <artifactId>avro-ipc</artifactId>
  <version>1.5.1</version>
</dependency>

Now we create the Avro Protocol file, which defines the RPC exchange. This file is stored in /src/main/avro/nublookup.avpr and looks like so:

{"namespace": "org.gbif.ecat.ws",
 "protocol": "NubLookup",
 "types": [
     {"name": "Request", "type": "record",
      "fields": [
        {"name": "kingdom", "type": ["string", "null"]},
        {"name": "phylum", "type": ["string", "null"]},
        {"name": "class", "type": ["string", "null"]},
        {"name": "order", "type": ["string", "null"]},
        {"name": "family", "type": ["string", "null"]},
        {"name": "genus", "type": ["string", "null"]},
        {"name": "name", "type": ["string", "null"]}
      ]
     },
     {"name": "Response", "type": "record",
      "fields": [
        {"name": "kingdomId", "type": ["int", "null"]},
        {"name": "phylumId", "type": ["int", "null"]},
        {"name": "classId", "type": ["int", "null"]},
        {"name": "orderId", "type": ["int", "null"]},
        {"name": "familyId", "type": ["int", "null"]},
        {"name": "genusId", "type": ["int", "null"]},
        {"name": "nameId", "type": ["int", "null"]}
      ]
     }  
 ],
 "messages": {
     "send": {
         "request": [{"name": "request", "type": "Request"}],
         "response": "Response"
     }
 }
}

This protocol defines an interface called NubLookup, that takes a Request and returns a Response. Simple stuff.

From the command line issue a compile:
$mvn compile
This will generate into src/main/java and the package I declared in the .avpr file (org.gbif.ecat.ws in my case).

Now we can test it using a simple Netty server which is included in the Avro dependency:

public class Test {
  private static NettyServer server;
  
  // A mock implementation
  public static class NubLookupImpl implements NubLookup {
    public Response send(Request request) throws AvroRemoteException {
      Response r = new Response();
      r.kingdomId=100;
      return r;
    }
  }
  
  public static void main(String[] args) throws IOException {
    server = new NettyServer(new SpecificResponder(
        NubLookup.class, 
        new NubLookupImpl()), 
        new InetSocketAddress(7001)); 

      NettyTransceiver client = new NettyTransceiver(
          new InetSocketAddress(server.getPort()));
      
      NubLookup proxy = (NubLookup) SpecificRequestor.getClient(NubLookup.class, client);
      
      Request req = new Request();
      req.name = new Utf8("Puma");
      System.out.println("Result: " + proxy.send(req).kingdomId);

      client.close();
      server.close();
  }
}

I am evaluating Avro to provide the high performance RPC chatter for lookup services while we process the content for the portal. I'll blog later about the performance compared to the Jersey REST implementation currently running.

 

分享到:
评论

相关推荐

    Apache Avro RPC简单示例

    NULL 博文链接:https://fengyilin.iteye.com/blog/2345324

    avro-rpc-demo:avro rpc的演示代码

    “ avro-rpc-demo”是Java实现的avro rpc的演示代码。 作者:zhexin Pan日期:20151103 简介此项目包括Java中的三个实现演示。 第一个是官方网站的快速入门演示,它是数据序列化和反序列化的两种实现,分别称为...

    avro-rpc-quickstart:Apache Avro RPC快速入门

    作者: (在上关注我)概括,如果您愿意的话...介绍该项目中包含的示例应用程序模拟了一个远程服务Mail,其中Avro RPC用于使用该服务发送消息。 本文档详细介绍了如何使用Maven构建和运行示例。 Avro jar文件(以及它们

    avro-rpc程序示例

    Avro RPC示例源码,包括client与server端,底层采用Netty

    avro-1.8.2.jar

    avro,avro-tools; Developers interested in getting more involved with Avro may join the mailing lists, report bugs, retrieve code from the version control system, and make ...

    Apache Avro

    Apache Avro™ is a data ...Developers interested in getting more involved with Avro may join the mailing lists, report bugs, retrieve code from the version control system, and make contributions.

    avro-tools-1.8.2.jar

    Developers interested in getting more involved with Avro may join the mailing lists, report bugs, retrieve code from the version control system, and make ...

    HelloAvro:一个示例应用程序,展示了如何使用很棒的Apache Avro序列化程序

    Avro RPC使用下面的Avro序列化器,但也允许客户端方法快速便捷地执行RPC等服务器方法。 RPC参数作为Avro序列化对象通过网络传递。 运行此示例应用程序 下载git仓库(duh!) 在Visual Studio中打开解决方案(.sln...

    hadooprpc机制&&将avro引入hadooprpc机制初探

    RPC(RemoteProcedureCall)——远程过程调用,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议。RPCServer实现了一种抽象的RPC服务,同时提供Call队列。RPCServer作为服务提供者由两个...

    avro的avro-tools-1.8.2的jar

    这是一个关于avro的1.8.2版本的avro-tools-1.8.2的jar包

    avro的avro-1.8.1的jar

    这是关于avro的avro-1.8.1版本的avro-tools的一个jar包

    avro-1.11.0-API文档-中文版.zip

    赠送jar包:avro-1.11.0.jar; 赠送原API文档:avro-1.11.0-javadoc.jar; 赠送源代码:avro-1.11.0-sources.jar; 赠送Maven依赖信息文件:avro-1.11.0.pom; 包含翻译后的API文档:avro-1.11.0-javadoc-API文档-...

    avro-1.8.2-API文档-中英对照版.zip

    赠送jar包:avro-1.8.2.jar; 赠送原API文档:avro-1.8.2-javadoc.jar; 赠送源代码:avro-1.8.2-sources.jar; 赠送Maven依赖信息文件:avro-1.8.2.pom; 包含翻译后的API文档:avro-1.8.2-javadoc-API文档-中文...

    avro-tools-1.8.1.jar

    avro 工具类 java -jar avro-tools-1.8.1.jar tojson --pretty test.avro &gt; output.json

    flink-avro-1.10.0-API文档-中文版.zip

    赠送jar包:flink-avro-1.10.0.jar; 赠送原API文档:flink-avro-1.10.0-javadoc.jar; 赠送源代码:flink-avro-1.10.0-sources.jar; 赠送Maven依赖信息文件:flink-avro-1.10.0.pom; 包含翻译后的API文档:flink-...

    avro-1.8.2-API文档-中文版.zip

    赠送jar包:avro-1.8.2.jar; 赠送原API文档:avro-1.8.2-javadoc.jar; 赠送源代码:avro-1.8.2-sources.jar; 赠送Maven依赖信息文件:avro-1.8.2.pom; 包含翻译后的API文档:avro-1.8.2-javadoc-API文档-中文...

    avro-1.10.0-API文档-中文版.zip

    赠送jar包:avro-1.10.0.jar; 赠送原API文档:avro-1.10.0-javadoc.jar; 赠送源代码:avro-1.10.0-sources.jar; 赠送Maven依赖信息文件:avro-1.10.0.pom; 包含翻译后的API文档:avro-1.10.0-javadoc-API文档-...

    avro-ipc-1.8.2-API文档-中文版.zip

    赠送jar包:avro-ipc-1.8.2.jar; 赠送原API文档:avro-ipc-1.8.2-javadoc.jar; 赠送源代码:avro-ipc-1.8.2-sources.jar; 赠送Maven依赖信息文件:avro-ipc-1.8.2.pom; 包含翻译后的API文档:avro-ipc-1.8.2-...

    avro+mapreduce安装报告

    avro是hadoop中一个序列化项目,avro和mapreduce结合如何使用,可以实现数据结构化并且序列化和反序列化

    avro-in-action:RPC与Apache Avro示例

    平均行动RPC与Apache Avro示例。

Global site tag (gtag.js) - Google Analytics