爬虫的基础环境主要包括Nodejs运行环境和MongoDB数据库,其中在《实现一个简单的爬虫》章节中仅需要Nodejs运行环境,在《保存数据到数据库》章节中开始使用MongoDB数据库;之后的章节里面,Nodejs运行环境和MongoDB数据库都会被用到。
Nodejs运行环境和MongoDB数据库的安装教程网上已经有很多了,这里提供官网的链接:
注:教程需要的nodejs的版本大于8.9,mongodb大于3.2
OooOoops
最近写了一个爬虫对B站的视频统计数据进行追踪,每2分钟爬取一次存在mongo里,然后用这些数据画折线图。这个时候问题来了,如果我爬取了一年的数据,进行数据展示的时候,不应该把一年的数据都从数据库里读取出来,对于年这样大粒度统计,应该以每天抽取一条记录就OK了;当想看一天内的数据变化,又要以分钟为粒度进行记录抽取。
基本的mongo查询语法已经难以解决均匀抽样查询记录了,这时就需要聚合查询这样的工具。
more >>前几日在VPS折腾MongoDB,由于机器内存实在是太小了(1G,又跑了别的程序),进行重建索引操作时,内存不足被系统给kill了。强制kill的Mongo无法在repair模式下恢复,只能直接用Wiredtiger工具读取二进制数据文件进行恢复了。
本教程提供一种KMS激活方案,即在本地搭建临时性KMS服务器,通过相应命令进行激活。本教程使用Office Visio 2016为例子,Windows 10和其他Office 2016组件激活同理。
        
          more >>
        
      
      
    
在对前端传来的参数进行校验是后端程序开发中不可或缺的步骤,但是大量参数校验的代码混杂在业务逻辑代码中实在是令人无奈何,无形之中使得简单的代码逻辑趋于复制。本文就以int类型为例,使用Spring注解和全局异常捕获对简单参数校验的代码简化技巧进行介绍。
        
          more >>
        
      
      
    
LocalDate类属于java8 Time API系列,那么如何使用LocalDate类来获取两个日期的间隔呢?
这里主要用了 `LocalDate::toEpochDay()` 方法,这个方法是获取1970-01-01到当前日期所经历的天数。
| 1 | public static long daysBetween(LocalDate from, LocalDate to) { | 
| 1 | public static void main(String[] args) { | 
| 1 | to: 17377 | 
code:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99public class TickTocker {
    private final static ConcurrentLinkedQueue<Ele> queue = new ConcurrentLinkedQueue<>();
    private Consumer<Long> callback = null;
    public final int period = 1000 * 5; //10s
    public final long expirTime = 1000 * 12; //1min
    public static TickTocker init(Consumer<Long> callback) {
        TickTocker tickTocker = new TickTocker();
        tickTocker.start(callback);
        return tickTocker;
    }
    public void add(long key) {
        queue.offer(new Ele(key));
    }
    private void start(Consumer<Long> callback) {
        this.callback = callback;
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
            public void run() {
                doJob();
            }
        }, 1000, period);
    }
    private void doJob() {
        //获取队列元素
        Ele e = queue.peek();
        //空队列什么都不执行
        if (e == null) {
            System.out.println(LocalDateTime.now() + " - Queue is empty.");
            return;
        }
        //判断是否超时
        if (isExpired(e.getCtime(), expirTime)) {
            System.out.println(LocalDateTime.now() + " - KEY:" + e.getKey() + " is expired.");
            queue.poll(); //从队列中移出第一个元素
            callback.accept(e.getKey()); //执行回调函数
            doJob();
        } else {
//            System.out.println(LocalDateTime.now() + " - Do nothing.");
        }
    }
    private boolean isExpired(long ctime, long expirTime) {
//        System.out.println("ctime: " + ctime + ", expirTime: " + expirTime + ", now: " + System.currentTimeMillis());
        return ctime + expirTime < System.currentTimeMillis();
    }
    class Ele {
        private long ctime;
        private long key;
        public Ele(long key) {
            this.key = key;
            this.ctime = System.currentTimeMillis();
        }
        public long getKey() {
            return key;
        }
        public void setKey(long key) {
            this.key = key;
        }
        public void setCtime(long ctime) {
            this.ctime = ctime;
        }
        public long getCtime() {
            return ctime;
        }
    }
    public static void main(String[] args){
        Random random = new Random();
        System.out.println(LocalDateTime.now() + " - Program started.");
        TickTocker tickTocker = TickTocker.init(key -> {
            System.out.println("Do something for " + key);
        });
        ExecutorService exec = Executors.newCachedThreadPool();
        for (int i = 100; i < 110; i++){
            final int key = i;
            exec.execute(()->{
                try {
                    Thread.sleep(random.nextInt(20000));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(LocalDateTime.now() + " - add KEY: " + key);
                tickTocker.add(key + 0L);
            });
        }
        exec.shutdown();
    }
}
            缺失模块。
1、请确保node版本大于6.2
2、在博客根目录(注意不是yilia根目录)执行以下命令:
 npm i hexo-generator-json-content --save
            3、在根目录_config.yml里添加配置:
  jsonContent:
    meta: false
    pages: false
    posts:
      title: true
      date: true
      path: true
      text: false
      raw: false
      content: false
      slug: false
      updated: false
      comments: false
      link: false
      permalink: false
      excerpt: false
      categories: false
      tags: true