elephant-bird的安装和使用

转自:http://guoyunsky.iteye.com/blog/1780165 

elephant-bird使用还是比较简单,毕竟只是一个生成代码的工具.我一开始以为elephant-bird也跟Protcol Buffer或Thrift一样,有自己的脚本,传入参数和参数值,通过脚本去生成代码.后来才发现,根本无需如此.
       毕竟elephant-bird基于Protocol Buffer和Thrift,而Protocol Buffer跟Thrift(Thrift我还没去测试过)又支持调用外部命令,也就是shell脚本.而这个shell脚本可以是elephant-bird生成代码的地方.具体看使用吧.
       1.依赖环境: 

                 1)Ant 

                 2)Protocol Buffer
       2.下载   

               下载相对简单,我这里通过git:   

                git clone https://github.com/kevinweil/elephant-bird.git 

                这里下载的elephant-bird路径我在下面简称为$ELEPHANT_BIRD_HOME
       3.安装 

              安装也相对简单,通过ant即可.如: ant install-local ant compile 

              运行这两个ant之后,会发现在$ELEPHANT_BIRD_HOME/build目录下发现elephant-bird-xxx.jar以及lib/compile目录,等下需要用到.
       4.使用 

             1)所需要的proto文件

               我这里直接拷贝Protocol Buffer的样例:address_book.proto,代码如下:    

 

Proto代码   收藏代码
  1.  package com.twitter.data.proto.tutorial;  
  2. // The sample protocol buffer file that Google uses in their examples at   
  3. // http://code.google.com/p/protobuf.   
  4. // Used in this project for tests and examples.  
  5. option java_outer_classname = "AddressBookProtos";  
  6. message Person {  
  7.     required string name = 1;   
  8.     required int32 id = 2;   
  9.     optional string email = 3;  
  10.   
  11.    enum PhoneType {   
  12.              MOBILE = 0;   
  13.              HOME = 1;   
  14.              WORK = 2;  
  15.     }  
  16.   
  17.      message PhoneNumber {   
  18.              required string number = 1;   
  19.              optional PhoneType type = 2 [default = HOME];   
  20.      }  
  21.      repeated PhoneNumber phone = 4;   
  22. }  
  23.   
  24. message AddressBook {  
  25.      repeated Person person = 1;   
  26. }   

      也可以从$ELEPHANT_BIRD_HOME/examples/src/proto/下获取address_book.proto


    2)新建build.xml,代码如下:  

     

Xml代码   收藏代码
  1. <project name="elephant-bird-study" basedir"." default="generate-protobuf" >  
  2.       <property name="src.dir" location="src" />   
  3.       <property name="src.java.dir" location="${src.dir}/java" />   
  4.       <property name="src.proto.dir" location="${src.dir}/proto" />   
  5.       <property name="src.gen.java.dir" location="${src.dir}/gen-java" />  
  6.   
  7.      <target name="generate-protobuf" >   
  8.            <delete dir="${src.gen.java.dir}"/>   
  9.            <mkdir dir="${src.gen.java.dir}"/>   
  10.            <apply executable="protoc" failonerror="true" skipemptyfilesets="true" verbose="true">                                      
  11.            <arg value="--proto_path=${src.proto.dir}" />      
  12.             <arg value="--java_out=${src.gen.java.dir}" />     
  13.             <arg value="--test_out=${src.gen.java.dir}" />      
  14.             <fileset dir="${src.proto.dir}" includes="**/*.proto" />   
  15.          </apply>  
  16.     </target>  
  17. </project>  

  
        3)通过ant脚本生成address_book.proto对应的代码:

 

           ant generate-protobuf    

           如果不出意外,可以在你工程目录下的src/gen-java看到生成的代码:com.twitter.data.proto.tutorial.AddressBookProtos.java.

 

        4)以上只是通过Protocol Buffer生成了Java,但对应Hadoop的Writable,Pig的LoadFunc还没生成,这里还要使用protoc命令,由protoc去调用一个脚本去生成这些代码.具体如下: 

           i.需要的东西:    

             a)各种jar:

                     elephant-bird-2.1.8.jar,guava-10.0.1.jar,hadoop-core-0.20.2-cdh3u0.jar,hadoop-lzo-0.4.15.jar,protobuf-java-2.3.0.jar,yamlbeans-0.9.3.jar,这些自己下载获取从$ELEPHANT_BIRD_HOME/build/lib/compile或$ELEPHANT_BIRD_HOME/lib下获取 

            b)需要运行elephant-bird脚本,我这里放在$YOUR_PROJECT_HOME/script目录下 

        ii.步骤: 

             a)将以上需要的jar放到你的工程目录下的lib目录中    

             b)更改build.xml,如下:      

Xml代码   收藏代码
  1. <project name="elephant-bird-study" basedir"." default="generate-protobuf" >  
  2.          <property name="src.dir" location="src" />  
  3.          <property name="src.java.dir" location="${src.dir}/java" />   
  4.          <property name="src.proto.dir" location="${src.dir}/proto" />   
  5.          <property name="src.gen.java.dir" location="${src.dir}/gen-java" />  
  6.   
  7.          <target name="generate-protobuf" >   
  8.                <delete dir="${src.gen.java.dir}"/>   
  9.                <mkdir dir="${src.gen.java.dir}"/>   
  10.                <apply executable="protoc" failonerror="true" skipemptyfilesets="true" verbose="true">                                          <env key="PATH" path="${env.PATH}:${basedir}/script" />    
  11.                    <arg value="--proto_path=${src.proto.dir}" />    
  12.                    <arg value="--java_out=${src.gen.java.dir}" />   
  13.                    <arg value="--twadoop_out=${src.gen.java.dir}" />   
  14.                    <fileset dir="${src.proto.dir}" includes="**/*.proto" />   
  15.               </apply>   
  16.        </target>  
  17. </project>  

 

           增加了<env key="PATH" path="${env.PATH}:${basedir}/script" />,表示将刚才新建的$YOUR_PROJECT_HOME/script下的文件放入path中     

         增加了参数<arg value="--twadoop_out=${src.gen.java.dir}" />,这里elephant-bird有个奇怪的规则,参数名为--twadoop_out,其中twadoop存在规则,他将跟protoc-gen-组成protoc-gen-twadoop做为Protocol Buffer调用elephant-bird的脚本文件名.   

           c)在$YOUR_PROJECT_HOME/script目录下新建脚本protoc-gen-twadoop,内容如下:    

Shell代码   收藏代码
  1. #!/bin/bash  
  2. bindir=`/usr/bin/dirname "$0"`   
  3. /usr/bin/java -cp $bindir/../lib/*: com.twitter.elephantbird.proto.HadoopProtoCodeGenerator $bindir/config-twadoop.yml -  

         以上会将刚拷贝到$YOUR_PROJECT_HOME/lib下的所有jar由java执行,然后会调用com.twitter.elephantbird.proto.HadoopProtoCodeGenerator类去生成所需要的各种代码.想要什么代码,则由config-twadoop.yml配置 

 

       d)注意protoc-gen-twadoop中有config-twadoop.yml,该文件配置elephant-bird想生成代码.文件内容如下:    

Txt代码   收藏代码
  1. address_book:    
  2. - com.twitter.elephantbird.proto.codegen.DeprecatedLzoProtobufBlockInputFormatGenerator    
  3. - com.twitter.elephantbird.proto.codegen.LzoProtobufB64LineInputFormatGenerator    
  4. - com.twitter.elephantbird.proto.codegen.LzoProtobufB64LineOutputFormatGenerator  
  5. #  - com.twitter.elephantbird.proto.codegen.LzoProtobufB64LinePigLoaderGenerator    
  6. - com.twitter.elephantbird.proto.codegen.LzoProtobufBlockInputFormatGenerator    
  7. - com.twitter.elephantbird.proto.codegen.LzoProtobufBlockOutputFormatGenerator  
  8. #  - com.twitter.elephantbird.proto.codegen.LzoProtobufBlockPigLoaderGenerator  
  9. # - com.twitter.elephantbird.proto.codegen.LzoProtobufHiveSerdeGenerator      
  10. - com.twitter.elephantbird.proto.codegen.ProtobufWritableGenerator  
  11. # - com.twitter.elephantbird.proto.codegen.ProtobufBytesToPigTupleGenerator   

     我这里不想生成pig和hive的代码,所以在前面加了个#注释了.


    e)生成代码,再次运行ant generate-protobuf,不出意外的话,YOUR_PROJECT_HOME/src/gen-java就会生成所需要的代码.

 

     注:本博客基于Elephantbird2.1.8

    在github上建了一个开源工程,可以运行ant命令基于elephantbird直接生成代码.地址:

       https://github.com/guoyunsky/elephant-bird-simple


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值