目的很簡單,就是將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 &