目的很简单,就是将mongodb数据导入es建立相应索引。数据是从特定的网站扒下来,然后进行二次处理,也就是数据去重、清洗,接着再保存到mongodb里,那么如何将数据搞到ElasticSearch中呢?调研之后觉得logstash-input-mongodb插件是个不错的选择,当然了也有很多其他实现方式,具体原因:
- 爬虫在实时存储数据,需要进行实时同步到ElasticSearch中
- 支持断点续传
- 时间成本...
首先介绍下版本(5.0以上)
- logstash 5.X
- elasticsearch 5.X
- logstash-input-mongodb-0.4.1(在线更新过
接下来就是实际操作了
这是插件GitHub地址:https://github.com/phutchins/logstash-input-mongodb
进入logstash 下bin目录 查看已安装的插件:
./logstash-plugin list
没有logstash-input-mongodb插件那么:
./logstash-plugin install logstash-input-mongodb
此步骤安装比较慢,很有可能失败,翻过墙另说,哈哈,建议替换镜像库为国内的库。
没有gem命令的先安装:
yum install gem
可以先看下镜像库地址命令如下:
gem sources -l
可以看到地址是:https://rubygems.org/
现在替换为国内的ruby-china库:
gem sources --add https://gems.ruby-china.org/ --remove https://rubygems.org/ #在查看 gem sources -l
此时一切换成功,当然,并没有完成,需要进入logstash目录对 Gemfile文件 进行编辑:
vim Gemfile
将文件里的 source "https://rubygems.org" 换成 source "https://gems.ruby-china.org",如图:
wq保存退出,好了进入bin再执行: ./logstash-plugin install logstash-input-mongodb
等待时间可能比较长,如果没有成功的话,切换镜像源成阿里的 再试一次
gem sources --add https://ruby.taobao.org/ --remove https://rubygems.org #如果之前已经换成国内的需要把将上面的 https://rubygems.org 换成 https://gems.ruby-china.org即: gem sources --add https://ruby.taobao.org/ --remove https://gems.ruby-china.org #然后 vim Gemfile #修改为: source "https://gems.ruby-china.org"
安装成功:
不排除还有失败的可能,可以把logstash-input-mongodb-0.4.1.gem文件下载下来(这里把文件移动到logstash目录下了),执行
./logstash-plugin install logstash-input-mongodb-0.4.1.gem
安装成功:
接下来就是添加logstash配置文件如下:


input { mongodb { uri => 'mongodb://192.168.1.43:27017/testData' placeholder_db_dir => '/opt/logstash-mongodb/' placeholder_db_name =>'testData.db' collection => 'test_Current' } } filter { # 把mongodb的_id替换掉,因为_id是跟es中的_id相冲突 mutate { rename => ["_id", "uid"] } # ruby { # code => "event.set('message', eval(event('title')))" # } } output { file { path => "/var/log/mongons.log" } stdout { codec => json_lines } elasticsearch { hosts => ["192.168.1.171:9200"] index => "testData" manage_template=>true document_type => "judicial" } }
启动:
bin/logstash -f logstash.conf #后台启动: nohup bin/logstash -f logstash.conf &>/var/log/null &