Skip to content

Instantly share code, notes, and snippets.

@rjbriody
Last active December 14, 2015 17:28
Show Gist options
  • Save rjbriody/5122294 to your computer and use it in GitHub Desktop.
Save rjbriody/5122294 to your computer and use it in GitHub Desktop.
A one stop shop for how to get, configure, and run mongodb and elasticsearch with the mongodb-river from https://github.com/richardwilly98/elasticsearch-river-mongodb. I am by no means a bash expert and this script is pretty hacky even by my standards, but it is simply meant to illustrate the steps a developer would take to get a "hello world" u…
#!/bin/bash
# Definitely make sure you create a directory to run this in.
# Set this to match the mongodb link for your architecture
# http://www.mongodb.org/downloads
# arch="linux-i686"
# arch="osx-x86_64"
arch="linux-x86_64"
cleanup()
{
pkill mongod 2>/dev/null
jps|grep ElasticSearch|sed 's/ElasticSearch//g'|xargs kill 2>/dev/null
rm -rf mongodb.rs0.log*
rm -rf rs0
rm -rf elasticsearch-0.20.5/data
}
# Enable cleanup by uncommenting the following line (for when you need to try again)
# cleanup
# ============= Get and run MongoDB =================
# Get mongodb. Adjust for your architecture
wget http://fastdl.mongodb.org/linux/mongodb-$arch-2.2.3.tgz
tar -zxf mongodb-$arch-2.2.3.tgz
# Make a data directory for mongodb
mkdir rs0
# Start mongod
./mongodb-$arch-2.2.3/bin/mongod --dbpath=rs0 --replSet rs0 --fork --logpath mongodb.rs0.log --smallfiles --oplogSize 50
# Give mongodb a sec to fire up
sleep 1
# Setup a simple 1 node replica set
# http://docs.mongodb.org/manual/tutorial/deploy-replica-set/
rsconf='{
_id: "rs0",
members: [{_id: 0, host: "localhost:27017"}]
}'
./mongodb-$arch-2.2.3/bin/mongo --eval "rs.initiate( $rsconf )"
# Wait for mongodb to elect a primary
(./mongodb-$arch-2.2.3/bin/mongo --quiet --eval 'rs.isMaster().ismaster'|grep true > /dev/null)
while [ $? != 0 ] ; do
echo "Waiting for mongodb to elect a primary."
sleep 2
(./mongodb-$arch-2.2.3/bin/mongo --quiet --eval 'rs.isMaster().ismaster'|grep true > /dev/null)
done
echo
# Get elastic search
wget https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-0.20.5.zip
unzip elasticsearch-0.20.5.zip
# Get mongodb-river
# https://github.com/richardwilly98/elasticsearch-river-mongodb/wiki
pushd elasticsearch-0.20.5 > /dev/null
# elasticsearch-river-mongodb plugin depends on elasticsearch-mapper-attachments
bin/plugin -install elasticsearch/elasticsearch-mapper-attachments/1.6.0
bin/plugin -url https://github.com/downloads/richardwilly98/elasticsearch-river-mongodb/elasticsearch-river-mongodb-1.6.1.zip -install river-mongodb
popd > /dev/null
# Start elasticsearch
./elasticsearch-0.20.5/bin/elasticsearch
# Wait for elasticsearch to fire up
cmd="curl -s -XGET localhost:9200/_status"
$cmd > /dev/null
while [ $? != 0 ] ; do
echo "Waiting for elastic search to start."
sleep 2
$cmd > /dev/null
done
# Add a river index for the collection
curl -XPUT "localhost:9200/_river/mongodbriver/_meta" -d'{
"type": "mongodb",
"mongodb": {
"db": "my_db",
"collection": "my_collection"
},
"index": {
"name": "my_es_index",
"type": "my_es_type"
}
}'
echo
# Add some documents to mongodb
echo "use my_db" > insertSomeStuff.js
for ii in {1..10}
do
echo "db.my_collection.insert({name: \"$ii\", time: new Date().toString()})" >> insertSomeStuff.js
done
mongo < insertSomeStuff.js
# Flow down the river does take a sec
sleep 5
# Search documents to make sure they made it down the river
curl -XPOST http://localhost:9200/my_es_index/my_es_type/_search?pretty=true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment