Imported all the notebooks
[tm351-notebooks.git] / notebooks / zz-mongo / Using replication sets.ipynb
diff --git a/notebooks/zz-mongo/Using replication sets.ipynb b/notebooks/zz-mongo/Using replication sets.ipynb
new file mode 100644 (file)
index 0000000..428d45b
--- /dev/null
@@ -0,0 +1,300 @@
+{
+ "metadata": {
+  "name": "",
+  "signature": "sha256:06c7af374e437a40e8782eb95fc7b6d6f19666116819c432d94f828ca6c6eafb"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+  {
+   "cells": [
+    {
+     "cell_type": "markdown",
+     "metadata": {},
+     "source": [
+      "Example docs from [High Availability and PyMongo](http://api.mongodb.org/python/current/examples/high_availability.html)"
+     ]
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "# Import the required libraries\n",
+      "\n",
+      "import pymongo\n",
+      "import datetime\n",
+      "import collections\n",
+      "import matplotlib as mpl\n",
+      "import matplotlib.pyplot as plt\n",
+      "from mpl_toolkits.basemap import Basemap\n",
+      "import numpy as np\n",
+      "%matplotlib inline"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": [],
+     "prompt_number": 1
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "# Open a connection to the Mongo server, open the accidents database and name the collections of accidents and labels\n",
+      "# client = pymongo.MongoClient('mongodb://ogedei:27017/')\n",
+      "# client = pymongo.MongoClient(['mongodb://localhost:31000/', 'mongodb://localhost:31001/', 'mongodb://localhost:31002/'])\n",
+      "\n",
+      "client = pymongo.MongoReplicaSetClient('mongodb://192.168.33.10:31000/', replicaSet='rs0')\n",
+      "\n",
+      "db = client.small_accidents\n",
+      "accidents = db.accidents\n",
+      "labels = db.labels"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": [],
+     "prompt_number": 5
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "client"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": [
+      {
+       "metadata": {},
+       "output_type": "pyout",
+       "prompt_number": 10,
+       "text": [
+        "MongoReplicaSetClient(['192.168.33.10:31001', '192.168.33.10:31002', '192.168.33.10:31000'])"
+       ]
+      }
+     ],
+     "prompt_number": 10
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "accidents.find().count()"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": [
+      {
+       "metadata": {},
+       "output_type": "pyout",
+       "prompt_number": 11,
+       "text": [
+        "145571"
+       ]
+      }
+     ],
+     "prompt_number": 11
+    },
+    {
+     "cell_type": "markdown",
+     "metadata": {},
+     "source": [
+      "Show the port for our current connection to the primary"
+     ]
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "client.primary"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": [
+      {
+       "metadata": {},
+       "output_type": "pyout",
+       "prompt_number": 12,
+       "text": [
+        "('192.168.33.10', 31001)"
+       ]
+      }
+     ],
+     "prompt_number": 12
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "client.secondaries"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": [
+      {
+       "metadata": {},
+       "output_type": "pyout",
+       "prompt_number": 13,
+       "text": [
+        "{('192.168.33.10', 31000), ('192.168.33.10', 31002)}"
+       ]
+      }
+     ],
+     "prompt_number": 13
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "client.auto_start_request"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": [
+      {
+       "metadata": {},
+       "output_type": "pyout",
+       "prompt_number": 14,
+       "text": [
+        "False"
+       ]
+      }
+     ],
+     "prompt_number": 14
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "client.write_concern"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": [
+      {
+       "metadata": {},
+       "output_type": "pyout",
+       "prompt_number": 15,
+       "text": [
+        "{}"
+       ]
+      }
+     ],
+     "prompt_number": 15
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "test_db = client.crud_test\n",
+      "tc = test_db.test_collection"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": [],
+     "prompt_number": 16
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "for f, s, b in zip('William Patrick Jon Tom Peter Colin Sylvester Paul Christopher David Matt Peter'.split(),\n",
+      "                   'Hartnell Troughton Pertwee Baker Davison Baker McCoy McGann Eccleston Tennant Smith Capaldi'.split(),\n",
+      "                [1908, 1920, 1919, 1934, 1951, 1943, 1943, 1959, 1964, 1971, 1982, 1958]):\n",
+      "    tc.insert({'name': {'forename': f, 'surname': s}, 'birthyear': b})\n",
+      "for p in tc.find():\n",
+      "    print(p)"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": [
+      {
+       "output_type": "stream",
+       "stream": "stdout",
+       "text": [
+        "{'name': {'surname': 'Hartnell', 'forename': 'William'}, 'birthyear': 1908, '_id': ObjectId('53c40e9cdbc2416eec094f9f')}\n",
+        "{'name': {'surname': 'Troughton', 'forename': 'Patrick'}, 'birthyear': 1920, '_id': ObjectId('53c40e9cdbc2416eec094fa0')}\n",
+        "{'name': {'surname': 'Pertwee', 'forename': 'Jon'}, 'birthyear': 1919, '_id': ObjectId('53c40e9cdbc2416eec094fa1')}\n",
+        "{'name': {'surname': 'Baker', 'forename': 'Tom'}, 'birthyear': 1934, '_id': ObjectId('53c40e9cdbc2416eec094fa2')}\n",
+        "{'name': {'surname': 'Davison', 'forename': 'Peter'}, 'birthyear': 1951, '_id': ObjectId('53c40e9cdbc2416eec094fa3')}\n",
+        "{'name': {'surname': 'Baker', 'forename': 'Colin'}, 'birthyear': 1943, '_id': ObjectId('53c40e9cdbc2416eec094fa4')}\n",
+        "{'name': {'surname': 'McCoy', 'forename': 'Sylvester'}, 'birthyear': 1943, '_id': ObjectId('53c40e9cdbc2416eec094fa5')}\n",
+        "{'name': {'surname': 'McGann', 'forename': 'Paul'}, 'birthyear': 1959, '_id': ObjectId('53c40e9cdbc2416eec094fa6')}\n",
+        "{'name': {'surname': 'Eccleston', 'forename': 'Christopher'}, 'birthyear': 1964, '_id': ObjectId('53c40e9cdbc2416eec094fa7')}\n",
+        "{'name': {'surname': 'Tennant', 'forename': 'David'}, 'birthyear': 1971, '_id': ObjectId('53c40e9cdbc2416eec094fa8')}\n",
+        "{'name': {'surname': 'Smith', 'forename': 'Matt'}, 'birthyear': 1982, '_id': ObjectId('53c40e9cdbc2416eec094fa9')}\n",
+        "{'name': {'surname': 'Capaldi', 'forename': 'Peter'}, 'birthyear': 1958, '_id': ObjectId('53c40e9cdbc2416eec094faa')}\n"
+       ]
+      }
+     ],
+     "prompt_number": 17
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "tc.update({'name.forename': 'William', 'name.surname': 'Hartnell'},\n",
+      "        {'$set': {'episodes': ['An Unearthly Child', 'The Daleks', 'The Tenth Planet']}})"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": [
+      {
+       "metadata": {},
+       "output_type": "pyout",
+       "prompt_number": 18,
+       "text": [
+        "{'n': 1,\n",
+        " 'lastOp': Timestamp(1405357804, 1),\n",
+        " 'ok': 1,\n",
+        " 'electionId': ObjectId('53c4073f058398e7bf3294ef'),\n",
+        " 'updatedExisting': True,\n",
+        " 'nModified': 1}"
+       ]
+      }
+     ],
+     "prompt_number": 18
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "for p in tc.find():\n",
+      "    print(p)"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": [
+      {
+       "output_type": "stream",
+       "stream": "stdout",
+       "text": [
+        "{'name': {'surname': 'Troughton', 'forename': 'Patrick'}, 'birthyear': 1920, '_id': ObjectId('53c40e9cdbc2416eec094fa0')}\n",
+        "{'name': {'surname': 'Pertwee', 'forename': 'Jon'}, 'birthyear': 1919, '_id': ObjectId('53c40e9cdbc2416eec094fa1')}\n",
+        "{'name': {'surname': 'Baker', 'forename': 'Tom'}, 'birthyear': 1934, '_id': ObjectId('53c40e9cdbc2416eec094fa2')}\n",
+        "{'name': {'surname': 'Davison', 'forename': 'Peter'}, 'birthyear': 1951, '_id': ObjectId('53c40e9cdbc2416eec094fa3')}\n",
+        "{'name': {'surname': 'Baker', 'forename': 'Colin'}, 'birthyear': 1943, '_id': ObjectId('53c40e9cdbc2416eec094fa4')}\n",
+        "{'name': {'surname': 'McCoy', 'forename': 'Sylvester'}, 'birthyear': 1943, '_id': ObjectId('53c40e9cdbc2416eec094fa5')}\n",
+        "{'name': {'surname': 'McGann', 'forename': 'Paul'}, 'birthyear': 1959, '_id': ObjectId('53c40e9cdbc2416eec094fa6')}\n",
+        "{'name': {'surname': 'Eccleston', 'forename': 'Christopher'}, 'birthyear': 1964, '_id': ObjectId('53c40e9cdbc2416eec094fa7')}\n",
+        "{'name': {'surname': 'Tennant', 'forename': 'David'}, 'birthyear': 1971, '_id': ObjectId('53c40e9cdbc2416eec094fa8')}\n",
+        "{'name': {'surname': 'Smith', 'forename': 'Matt'}, 'birthyear': 1982, '_id': ObjectId('53c40e9cdbc2416eec094fa9')}\n",
+        "{'name': {'surname': 'Capaldi', 'forename': 'Peter'}, 'birthyear': 1958, '_id': ObjectId('53c40e9cdbc2416eec094faa')}\n",
+        "{'episodes': ['An Unearthly Child', 'The Daleks', 'The Tenth Planet'], 'name': {'surname': 'Hartnell', 'forename': 'William'}, 'birthyear': 1908, '_id': ObjectId('53c40e9cdbc2416eec094f9f')}\n"
+       ]
+      }
+     ],
+     "prompt_number": 19
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [],
+     "language": "python",
+     "metadata": {},
+     "outputs": []
+    }
+   ],
+   "metadata": {}
+  }
+ ]
+}
\ No newline at end of file