你好,游客 登录 注册 搜索
背景:
阅读新闻

Nutch 2.0 之 Apache Gora 介绍

[日期:2012-07-21] 来源:Linux社区  作者:amuseme_lu [字体:大 中 小]

下来是定义数据模型,Gora使用Apache Avro来定义数据的对象模型,avro可以方便的提供跟踪数对象的持久化状态与对象的持久化功能。定义数据模型还是比较简单的,它是以json格式来定义的,其中使用的PageView(src/main/avro/pageview.json)的定义格式如下:

  1.    {  
  2.   "type": "record",  
  3.   "name": "Pageview",  
  4.   "namespace": "org.apache.gora.tutorial.log.generated",  
  5.   "fields" : [  
  6.     {"name": "url", "type": "string"},  
  7.     {"name": "timestamp", "type": "long"},  
  8.     {"name": "ip", "type": "string"},  
  9.     {"name": "httpMethod", "type": "string"},  
  10.     {"name": "httpStatusCode", "type": "int"},  
  11.     {"name": "responseSize", "type": "int"},  
  12.     {"name": "referrer", "type": "string"},  
  13.     {"name": "userAgent", "type": "string"}  
  14.   ]  
  15. }  

我们可以看到,其中的数据类型是record,name是生成的类的名称,而namespace是对应java中package的名字,而fields中定义的字段的名称与类型。

下来是对定义好的数据模型进行编译,自动生成相应的代码,命令如下

  1. $ bin/gora compile  
  2.   
  3. $ Usage: SpecificCompiler <schema file> <output dir>  
  4.   
  5. $ bin/gora compile gora-tutorial/src/main/avro/pageview.json gora-tutorial/src/main/java/  

编译以后生成文件 gora-tutorial/src/main/java/org/apache/gora/tutorial/log/generated/Pageview.java

这里Gora的解析器扩展了Avro的SpecificCompiler方法,因为生成的对象模型在扩展自Gora自己的Persistent接口,而Persistent这个接口定义了对象的持久化,对象状态的跟踪等方法。下面是部分Pageview.java的代码

  1.  public class Pageview extends PersistentBase {  
  2.   
  3.   
  4. private Utf8 url;  
  5. private long timestamp;  
  6. private Utf8 ip;  
  7. private Utf8 httpMethod;  
  8. private int httpStatusCode;  
  9. private int responseSize;  
  10. private Utf8 referrer;  
  11. private Utf8 userAgent;  
  12.   
  13.   
  14. ...  
  15.   
  16.   
  17. public static final Schema _SCHEMA = Schema.parse("{\"type\":\"record\", ... ");  
  18. public static enum Field {  
  19.   URL(0,"url"),  
  20.   TIMESTAMP(1,"timestamp"),  
  21.   IP(2,"ip"),  
  22.   HTTP_METHOD(3,"httpMethod"),  
  23.   HTTP_STATUS_CODE(4,"httpStatusCode"),  
  24.   RESPONSE_SIZE(5,"responseSize"),  
  25.   REFERRER(6,"referrer"),  
  26.   USER_AGENT(7,"userAgent"),  
  27.   ;  
  28.   private int index;  
  29.   private String name;  
  30.   Field(int index, String name) {this.index=index;this.name=name;}  
  31.   public int getIndex() {return index;}  
  32.   public String getName() {return name;}  
  33.   public String toString() {return name;}  
  34. };  
  35. public static final String[] _ALL_FIELDS = {"url","timestamp","ip","httpMethod"  
  36.   ,"httpStatusCode","responseSize","referrer","userAgent",};  
  37.   
  38. ...  
  39. }  
linux
相关资讯       Nutch  Apache Gora  Nutch 2.0 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款