initial try for binder setup
This commit is contained in:
parent
a7891480d1
commit
de800fa4e2
1
binder/PORT
Normal file
1
binder/PORT
Normal file
@ -0,0 +1 @@
|
||||
27017
|
||||
1
binder/VERSION
Normal file
1
binder/VERSION
Normal file
@ -0,0 +1 @@
|
||||
4.4.0
|
||||
16
binder/environment.yml
Normal file
16
binder/environment.yml
Normal file
@ -0,0 +1,16 @@
|
||||
name: pymongo
|
||||
|
||||
channels:
|
||||
- conda-forge
|
||||
- nodefaults
|
||||
|
||||
dependencies:
|
||||
# runtime dependencies
|
||||
- python >=3.6,<3.11.0
|
||||
- jupyterlab >=3.0.0,<4.0.0
|
||||
- pip
|
||||
- pymongo >=4
|
||||
- mongodb
|
||||
- nodejs
|
||||
- pip:
|
||||
- mtools
|
||||
13
binder/init
Normal file
13
binder/init
Normal file
@ -0,0 +1,13 @@
|
||||
VERSION=`cat VERSION`
|
||||
PORT=`cat PORT`
|
||||
MONGODB_BIN=`m bin $VERSION`
|
||||
mlaunch init --replicaset --name repl0 --nodes 3 --binarypath \
|
||||
$MONGODB_BIN --port $PORT --hostname localhost --setParameter \
|
||||
enableTestCommands=1
|
||||
mongo
|
||||
VERSION=`cat VERSION`
|
||||
PORT=`cat PORT`
|
||||
MONGODB_BIN=`m bin $VERSION`
|
||||
CMD="$MONGODB_BIN/mongo mongodb://localhost:$PORT/?replicaSet=repl0"
|
||||
echo "$CMD"
|
||||
$CMD
|
||||
8
binder/mongo
Normal file
8
binder/mongo
Normal file
@ -0,0 +1,8 @@
|
||||
VERSION=`cat VERSION`
|
||||
PORT=`cat PORT`
|
||||
MONGODB_BIN=`m bin $VERSION`
|
||||
CMD="$MONGODB_BIN/mongo --ssl --sslCAFile <path/to/ca.pem> \
|
||||
--sslPEMKeyFile <path/to/client.pem> \
|
||||
mongodb://localhost:$PORT/?replicaSet=repl0"
|
||||
echo "$CMD"
|
||||
$CMD
|
||||
14
binder/postBuild
Normal file
14
binder/postBuild
Normal file
@ -0,0 +1,14 @@
|
||||
#!/usr/bin/env bash
|
||||
set -eux
|
||||
|
||||
npm install -g m
|
||||
|
||||
pip install -e .
|
||||
|
||||
m 4.4.0
|
||||
|
||||
mkdir 440_psa_tls
|
||||
|
||||
cd binder
|
||||
|
||||
./init &
|
||||
463
notebooks/MongoClient.ipynb
Normal file
463
notebooks/MongoClient.ipynb
Normal file
@ -0,0 +1,463 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"slideshow": {
|
||||
"slide_type": "slide"
|
||||
}
|
||||
},
|
||||
"source": [
|
||||
"<img src=\"https://s3.amazonaws.com/edu-static.mongodb.com/lessons/M220/notebook_assets/screen_align.png\" style=\"margin: 0 auto;\">\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"slideshow": {
|
||||
"slide_type": "slide"
|
||||
}
|
||||
},
|
||||
"source": [
|
||||
"<h1 style=\"text-align: center; font-size=58px;\">MongoClient</h1>"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"metadata": {
|
||||
"slideshow": {
|
||||
"slide_type": "subslide"
|
||||
}
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"········\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from pymongo import MongoClient\n",
|
||||
"from getpass import getpass\n",
|
||||
"uri = \"mongodb+srv://silvester@mflix.qu46j.mongodb.net/test\"\n",
|
||||
"password = getpass()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"slideshow": {
|
||||
"slide_type": "notes"
|
||||
}
|
||||
},
|
||||
"source": [
|
||||
"The MongoClient constructor accepts many different arguments to configure how the driver connects to MongoDB and how many operations will be performed. We'll look at the most basic configuration first, which is passing the SRV string of our Atlas cluster to MongoClient."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"metadata": {
|
||||
"slideshow": {
|
||||
"slide_type": "subslide"
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"client = MongoClient(uri, password=password)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 13,
|
||||
"metadata": {
|
||||
"slideshow": {
|
||||
"slide_type": "fragment"
|
||||
}
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"Database(MongoClient(host=['mflix-shard-00-00.qu46j.mongodb.net:27017', 'mflix-shard-00-01.qu46j.mongodb.net:27017', 'mflix-shard-00-02.qu46j.mongodb.net:27017'], document_class=dict, tz_aware=False, connect=True, authsource='admin', replicaset='atlas-13a6ky-shard-0', ssl=True), 'stats')"
|
||||
]
|
||||
},
|
||||
"execution_count": 13,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"client.stats"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"slideshow": {
|
||||
"slide_type": "notes"
|
||||
}
|
||||
},
|
||||
"source": [
|
||||
"Note that because we're using an Atlas SRV string, we got an SSL connection for free! It also defaults the **authSource** to the **admin** database.\n",
|
||||
"\n",
|
||||
"Now that we've connected to our **mongod**, we can create a database handle. Let's look at the available databases."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 14,
|
||||
"metadata": {
|
||||
"slideshow": {
|
||||
"slide_type": "subslide"
|
||||
}
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"['sample_airbnb',\n",
|
||||
" 'sample_analytics',\n",
|
||||
" 'sample_geospatial',\n",
|
||||
" 'sample_mflix',\n",
|
||||
" 'sample_restaurants',\n",
|
||||
" 'sample_supplies',\n",
|
||||
" 'sample_training',\n",
|
||||
" 'sample_weatherdata',\n",
|
||||
" 'admin',\n",
|
||||
" 'local']"
|
||||
]
|
||||
},
|
||||
"execution_count": 14,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"client.list_database_names()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"slideshow": {
|
||||
"slide_type": "notes"
|
||||
}
|
||||
},
|
||||
"source": [
|
||||
"Let's use the **sample_mflix** database. One useful property of a MongoClient object is we can use property accessors"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 15,
|
||||
"metadata": {
|
||||
"slideshow": {
|
||||
"slide_type": "subslide"
|
||||
}
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"['sessions', 'comments', 'movies', 'theaters', 'users']"
|
||||
]
|
||||
},
|
||||
"execution_count": 15,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"mflix = client.sample_mflix\n",
|
||||
"mflix.list_collection_names()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"slideshow": {
|
||||
"slide_type": "notes"
|
||||
}
|
||||
},
|
||||
"source": [
|
||||
"or we can use dictionary accessors"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 16,
|
||||
"metadata": {
|
||||
"slideshow": {
|
||||
"slide_type": "subslide"
|
||||
}
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"['sessions', 'comments', 'movies', 'theaters', 'users']"
|
||||
]
|
||||
},
|
||||
"execution_count": 16,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"mflix = client['sample_mflix']\n",
|
||||
"mflix.list_collection_names()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"slideshow": {
|
||||
"slide_type": "notes"
|
||||
}
|
||||
},
|
||||
"source": [
|
||||
"Now that we have a database object and have listed available collections, let's create a collection object. As with the database object, we can use either property or dictionary accessors."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 17,
|
||||
"metadata": {
|
||||
"slideshow": {
|
||||
"slide_type": "subslide"
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"movies = mflix.movies"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"slideshow": {
|
||||
"slide_type": "notes"
|
||||
}
|
||||
},
|
||||
"source": [
|
||||
"And let's perform a query on our movies collection. We'll just get the count of documents in the collection."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 18,
|
||||
"metadata": {
|
||||
"scrolled": true,
|
||||
"slideshow": {
|
||||
"slide_type": "subslide"
|
||||
}
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"23530"
|
||||
]
|
||||
},
|
||||
"execution_count": 18,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"movies.count_documents({})"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 31,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[\n",
|
||||
" {\n",
|
||||
" \"title\": \"Roadracers\"\n",
|
||||
" },\n",
|
||||
" {\n",
|
||||
" \"title\": \"Midaq Alley\"\n",
|
||||
" },\n",
|
||||
" {\n",
|
||||
" \"title\": \"Desperado\"\n",
|
||||
" },\n",
|
||||
" {\n",
|
||||
" \"title\": \"Fools Rush In\"\n",
|
||||
" },\n",
|
||||
" {\n",
|
||||
" \"title\": \"The Hunchback\"\n",
|
||||
" },\n",
|
||||
" {\n",
|
||||
" \"title\": \"54\"\n",
|
||||
" },\n",
|
||||
" {\n",
|
||||
" \"title\": \"Frida\"\n",
|
||||
" },\n",
|
||||
" {\n",
|
||||
" \"title\": \"Wild Wild West\"\n",
|
||||
" },\n",
|
||||
" {\n",
|
||||
" \"title\": \"No One Writes to the Colonel\"\n",
|
||||
" },\n",
|
||||
" {\n",
|
||||
" \"title\": \"54\"\n",
|
||||
" },\n",
|
||||
" {\n",
|
||||
" \"title\": \"In the Time of the Butterflies\"\n",
|
||||
" },\n",
|
||||
" {\n",
|
||||
" \"title\": \"Once Upon a Time in Mexico\"\n",
|
||||
" },\n",
|
||||
" {\n",
|
||||
" \"title\": \"After the Sunset\"\n",
|
||||
" },\n",
|
||||
" {\n",
|
||||
" \"title\": \"Ask the Dust\"\n",
|
||||
" },\n",
|
||||
" {\n",
|
||||
" \"title\": \"Lonely Hearts\"\n",
|
||||
" },\n",
|
||||
" {\n",
|
||||
" \"title\": \"Puss in Boots\"\n",
|
||||
" },\n",
|
||||
" {\n",
|
||||
" \"title\": \"The Prophet\"\n",
|
||||
" },\n",
|
||||
" {\n",
|
||||
" \"title\": \"Here Comes the Boom\"\n",
|
||||
" },\n",
|
||||
" {\n",
|
||||
" \"title\": \"Here Comes the Boom\"\n",
|
||||
" },\n",
|
||||
" {\n",
|
||||
" \"title\": \"As Luck Would Have It\"\n",
|
||||
" },\n",
|
||||
" {\n",
|
||||
" \"title\": \"Tale of Tales\"\n",
|
||||
" }\n",
|
||||
"]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"cursor = movies.find( { \"cast\": \"Salma Hayek\"}, { \"title\": 1, \"_id\": 0 })\n",
|
||||
"import bson\n",
|
||||
"from bson.json_util import dumps\n",
|
||||
"print(dumps(cursor, indent=2))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"slideshow": {
|
||||
"slide_type": "notes"
|
||||
}
|
||||
},
|
||||
"source": [
|
||||
"The MongoClient constructor also accepts many optional keyword parameters. We can set the maximum connection pool, default read and write concerns, whether to retry writes, configuring SSL, authentication, and much more.\n",
|
||||
"\n",
|
||||
"A full list and how to use MongoClient for more advanced use cases is available [here](http://api.mongodb.com/python/current/api/pymongo/mongo_client.html)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"slideshow": {
|
||||
"slide_type": "subslide"
|
||||
}
|
||||
},
|
||||
"source": [
|
||||
"Here is an example setting the **connectTimeoutMS** to 200 milliseconds, how long the driver will allow attempt to connect before erroring, and setting **retryWrites** to True, signaling to the driver to retry a write in the event of a network error."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"slideshow": {
|
||||
"slide_type": "fragment"
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"client = MongoClient(uri, connectTimeoutMS=200, retryWrites=True)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"slideshow": {
|
||||
"slide_type": "fragment"
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"client.stats"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"slideshow": {
|
||||
"slide_type": "slide"
|
||||
}
|
||||
},
|
||||
"source": [
|
||||
"## Summary\n",
|
||||
"\n",
|
||||
"* MongoClient accepts many optional keyword arguments to fine-tune your connection.\n",
|
||||
"* After instantiating the client, databases handles can be created via property or dictionary accessors on the client object.\n",
|
||||
"* Collections handles are referenced from the database object.\n",
|
||||
"* Collection specific operations like querying or updating documents are performed on the collection object."
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.9.7"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user