多线程抓取

 由于下载速度太慢,打算重载QueueAssignmentPolicy这个类,并重写它的getClassKey()这个方法,网上的代码有:

Code:
  1. public String getClassKey(CrawlController controller, CandidateURI cauri) {  
  2.        String uri = cauri.getUURI().toString();  
  3.        long hash = ELFHash(uri);  
  4.        String a = Long.toString(hash % 100);  
  5.        return a;  
  6.    }  
  7.    public long ELFHash(String str)  
  8.    {  
  9.       long hash = 0;  
  10.       long x    = 0;  
  11.       for(int i = 0; i < str.length(); i++)  
  12.       {  
  13.          hash = (hash << 4) + str.charAt(i);  
  14.          if((x = hash & 0xF0000000L) != 0)  
  15.          {  
  16.             hash ^= (x >> 24);  
  17.             hash &= ~x;  
  18.          }  
  19.       }  
  20.       return (hash & 0x7FFFFFFF);  
  21.    }  

但是重写后怎么才能用呢?这个问题我研究了一上午。

 这个类的作用是在Frontier为线程提供链接的时候提供链接队列的分配策略(默认是org.archive.crawler.frontier.HostnameQueueAssignmentPolicy这个类来提供)由于一般是选用BdbFrontier,(但是BdbFrontier中没有)-->查看父类WorkQueueFrontier(也没有)-->查看父类AbstractFrontier(发现了)

Code:
  1. public final static String ATTR_QUEUE_ASSIGNMENT_POLICY =  
  2.     "queue-assignment-policy";  

并在其构造方法中发现了对此变量引用的足迹

Code:
  1. // Read the list of permissible choices from heritrix.properties.  
  2.         // Its a list of space- or comma-separated values.  
  3. //通过“ ATTR_QUEUE_ASSIGNMENT_POLICY”(queue-assignment-policy)获得此键的属性  
  4.         String queueStr = System.getProperty(AbstractFrontier.class.getName() +  
  5.                 "." + ATTR_QUEUE_ASSIGNMENT_POLICY,  
  6.                 HostnameQueueAssignmentPolicy.class.getName() + " " +  
  7.                 IPQueueAssignmentPolicy.class.getName() + " " +  
  8.                 BucketQueueAssignmentPolicy.class.getName() + " " +  
  9.                 SurtAuthorityQueueAssignmentPolicy.class.getName() + " " +  
  10.                 TopmostAssignedSurtQueueAssignmentPolicy.class.getName());  
  11. //  
  12.         Pattern p = Pattern.compile("//s*,//s*|//s+");  
  13. //通过正在表达式切分改String,返回一个String数组  
  14.         String [] queues = p.split(queueStr);  
  15.         if (queues.length <= 0) {  
  16.             throw new RuntimeException("Failed parse of " +  
  17.                     " assignment queue policy string: " + queueStr);  
  18.         }  
  19. //创建一个SimpleType加入到definitionMap中  
  20. /*具体方法体为: 
  21.  
  22.   public Type addElementToDefinition(Type type) { 
  23.         if (isInitialized()) { 
  24.             throw new IllegalStateException( 
  25.                     "Elements should only be added to definition in the " + 
  26.                     "constructor."); 
  27.         } 
  28.         if (definitionMap.containsKey(type.getName())) { 
  29.             definition.remove(definitionMap.remove(type.getName())); 
  30.         } 
  31.              
  32.         definition.add(type); 
  33.         definitionMap.put(type.getName(), type); 
  34.         return type; 
  35.     } 
  36. */  
  37.         t = addElementToDefinition(new SimpleType(ATTR_QUEUE_ASSIGNMENT_POLICY,  
  38.                 "Defines how to assign URIs to queues. Can assign by host, " +  
  39.                 "by ip, and into one of a fixed set of buckets (1k).",  
  40.                 queues[0], queues));  

其注释也清楚的说明了://Read the list of permissible choices from heritrix.properties  

查看heritrix.properties:

Code:
  1. #####################################################  
  2. # FRONTIER  
  3. #####################################################  
  4. # List here all queue assignment policies you'd have show as a  
  5. # queue-assignment-policy choice in AbstractFrontier derived Frontiers  
  6. # (e.g. BdbFrontier).  
  7.   org.archive.crawler.frontier.AbstractFrontier.queue-assignment-policy =/
  8.   org.archive.crawler.frontier.HostnameQueueAssignmentPolicy/
  9.   org.archive.crawler.frontier.IPQueueAssignmentPolicy/
  10.   org.archive.crawler.frontier.BucketQueueAssignmentPolicy/
  11.   org.archive.crawler.frontier.SurtAuthorityQueueAssignmentPolicy/
  12.   org.archive.crawler.frontier.TopmostAssignedSurtQueueAssignmentPolicy
  13. org.archive.crawler.frontier.BdbFrontier.level = INFO  

进一步看:

Code:
  1. new SimpleType(ATTR_QUEUE_ASSIGNMENT_POLICY,  
  2.                 "Defines how to assign URIs to queues. Can assign by host, " +  
  3.                 "by ip, and into one of a fixed set of buckets (1k).",  
  4.                 queues[0], queues)  

该方法最终继承至attribute并把ATTR_QUEUE_ASSIGNMENT_POLICY,queues[0]传入Attribute构造方法中。

queues[0]即为org.archive.crawler.frontier.HostnameQueueAssignmentPolicy。

 

由以上论述分析可知:

如果想重写QueueAsignmentPolicy,yournameQueueAsignmentPolicy放到AbstraceFrontier.java中的:

Code:
  1.    String queueStr = System.getProperty(AbstractFrontier.class.getName() +  
  2.                 "." + ATTR_QUEUE_ASSIGNMENT_POLICY,   
  3.                 MyQueueAsignmentPolicy.class.getName()+" "+  
  4.                 HostnameQueueAssignmentPolicy.class.getName() + " " +  
  5.                 IPQueueAssignmentPolicy.class.getName() + " " +  
  6.                 BucketQueueAssignmentPolicy.class.getName() + " " +  
  7.                 SurtAuthorityQueueAssignmentPolicy.class.getName() + " " +  
  8.                 TopmostAssignedSurtQueueAssignmentPolicy.class.getName());  

 

在heritrix.propertits中修改如下:

Code:
  1. #############################################################################  
  2. # FRONTIER  
  3. #############################################################################  
  4.   
  5. # List here all queue assignment policies you'd have show as a  
  6. # queue-assignment-policy choice in AbstractFrontier derived Frontiers  
  7. # (e.g. BdbFrontier).  
  8. org.archive.crawler.frontier.AbstractFrontier.queue-assignment policy = /userP.myQueueAssignmentPolicy/
  9. org.archive.crawler.frontier.HostnameQueueAssignmentPolicy/   
  10. org.archive.crawler.frontier.IPQueueAssignmentPolicy/ 
  11. org.archive.crawler.frontier.BucketQueueAssignmentPolicy/   
  12. org.archive.crawler.frontier.SurtAuthorityQueueAssignmentPolicy/   
  13. org.archive.crawler.frontier.TopmostAssignedSurtQueueAssignmentPolicy  
  14. org.archive.crawler.frontier.BdbFrontier.level = INFO  

OK!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值