Python and MongoDB. Why?



Similar documents
The MongoDB Tutorial Introduction for MySQL Users. Stephane Combaudon April 1st, 2014

Big Data. Facebook Wall Data using Graph API. Presented by: Prashant Patel Jaykrushna Patel

MongoDB Developer and Administrator Certification Course Agenda

Humongous MongoDB. Sean Corfield World Singles llc

Department of Software Systems. Presenter: Saira Shaheen, Dated:

Short notes on webpage programming languages

MongoDB Aggregation and Data Processing Release 3.0.4

MongoDB Aggregation and Data Processing

Specialized Programme on Web Application Development using Open Source Tools

Document Oriented Database

HDFS. Hadoop Distributed File System

MongoDB. The Definitive Guide to. The NoSQL Database for Cloud and Desktop Computing. Apress8. Eelco Plugge, Peter Membrey and Tim Hawkins

Perl & NoSQL Focus on MongoDB. Jean-Marie Gouarné jmgdoc@cpan.org

Integrating Big Data into the Computing Curricula

Big Data & Data Science Course Example using MapReduce. Presented by Juan C. Vega

An Approach to Implement Map Reduce with NoSQL Databases

MongoDB. Or how I learned to stop worrying and love the database. Mathias Stearn. N*SQL Berlin October 22th, gen

PHP and MongoDB Web Development Beginners Guide by Rubayeet Islam

DIPLOMA IN WEBDEVELOPMENT

How To Let A Lecturer Know If Someone Is At A Lecture Or If They Are At A Guesthouse

NOSQL INTRODUCTION WITH MONGODB AND RUBY GEOFF

Getting Started with MongoDB

Introduction to NoSQL Databases and MapReduce. Tore Risch Information Technology Uppsala University

MongoDB Aggregation and Data Processing

Web Development using PHP (WD_PHP) Duration 1.5 months

Attacking MongoDB. Firstov Mihail

ANDROID APPS DEVELOPMENT FOR MOBILE GAME

Database Management System Choices. Introduction To Database Systems CSE 373 Spring 2013

DYNAMIC QUERY FORMS WITH NoSQL

Querying MongoDB without programming using FUNQL

Scaling up = getting a better machine. Scaling out = use another server and add it to your cluster.

Real-time Streaming Analysis for Hadoop and Flume. Aaron Kimball odiago, inc. OSCON Data 2011

Skills for Employment Investment Project (SEIP)

Specialized Programme on Web Application Development using Open Source Tools

Dr. Chuck Cartledge. 15 Oct. 2015

Intro to Databases. ACM Webmonkeys 2011

WEB DEVELOPMENT COURSE (PHP/ MYSQL)

Open Source Technologies on Microsoft Azure

Application note: Connecting the to a Database

Sisense. Product Highlights.

Developing a Web Server Platform with SAPI Support for AJAX RPC using JSON

Oracle Database: SQL and PL/SQL Fundamentals

Please ask questions! Have people used non-relational dbs before? MongoDB?

MongoDB. An introduction and performance analysis. Seminar Thesis

How To Use Query Console

L7_L10. MongoDB. Big Data and Analytics by Seema Acharya and Subhashini Chellappan Copyright 2015, WILEY INDIA PVT. LTD.

Benchmarking and monitoring tools

MongoDB in the NoSQL and SQL world. Horst Rechner Berlin,

Click Stream Data Analysis Using Hadoop

Slides from INF3331 lectures - web programming in Python

DbSchema Tutorial with Introduction in MongoDB

Oracle 10g PL/SQL Training

G563 Quantitative Paleontology. SQL databases. An introduction. Department of Geological Sciences Indiana University. (c) 2012, P.

NoSQL replacement for SQLite (for Beatstream) Antti-Jussi Kovalainen Seminar OHJ-1860: NoSQL databases

Overview of Databases On MacOS. Karl Kuehn Automation Engineer RethinkDB

Power Tools for Pivotal Tracker

Programming Hadoop 5-day, instructor-led BD-106. MapReduce Overview. Hadoop Overview

Oracle Database: SQL and PL/SQL Fundamentals NEW

Brad Dayley. NoSQL with MongoDB

DBMS Project. COP Spring Final Submission Report

Chapter 2: Interactive Web Applications

CSCI110 Exercise 4: Database - MySQL

Microsoft Azure: Opção de Nuvem para Todo o Desenvolvedor. Danilo Bordini & Osvaldo Daibert

SuiteCRM for Developers

NoSQL Roadshow Berlin Kai Spichale

NoSQL, But Even Less Security Bryan Sullivan, Senior Security Researcher, Adobe Secure Software Engineering Team

Cyber Security Challenge Australia 2014

Fax via HTTP (POST) Traitel Telecommunications Pty Ltd 2012 Telephone: (61) (2) Page 1

Oracle SQL. Course Summary. Duration. Objectives

ADVANCED DATABASES PROJECT. Juan Manuel Benítez V Gledys Sulbaran

MongoDB and Couchbase

Web Application Security Part 1

USING MYWEBSQL FIGURE 1: FIRST AUTHENTICATION LAYER (ENTER YOUR REGULAR SIMMONS USERNAME AND PASSWORD)

NoSQL web apps. w/ MongoDB, Node.js, AngularJS. Dr. Gerd Jungbluth, NoSQL UG Cologne,

Introduction to MongoDB. Kristina Chodorow

MongoDB: document-oriented database

Schema Extraction of Document Database - MongoDB. Master of Engineering. Computer Science and Engineering. Submitted By.

getsharedpreferences() - Use this if you need multiple preferences files identified by name, which you specify with the first parameter.

Programming the Web Server. Robert M. Dondero, Ph.D. Princeton University

CS 1133, LAB 2: FUNCTIONS AND TESTING

Big Data Frameworks: Scala and Spark Tutorial

Can the Elephants Handle the NoSQL Onslaught?

Eclipse Exam Tutorial - Pros and Cons

RARITAN VALLEY COMMUNITY COLLEGE ACADEMIC COURSE OUTLINE CISY 233 INTRODUCTION TO PHP

Building Your First MongoDB Application

DocStore: Document Database for MySQL at Facebook. Peng Tian, Tian Xia 04/14/2015

16.1 MAPREDUCE. For personal use only, not for distribution. 333

Cloud Application Development (SE808, School of Software, Sun Yat-Sen University) Yabo (Arber) Xu

Exercise 1: Python Language Basics

PHP Tutorial From beginner to master

shodan-python Documentation

An Open Source NoSQL solution for Internet Access Logs Analysis

Open source framework for interactive data exploration in server based architecture

A Brief Introduction to MySQL

UQC103S1 UFCE Systems Development. uqc103s/ufce PHP-mySQL 1

6 CURRENT JOB OPENINGS:

.NET User Group Bern

Whisler 1 A Graphical User Interface and Database Management System for Documenting Glacial Landmarks

10CS73:Web Programming

Oracle Database 11g SQL

Transcription:

Python and MongoDB Kevin Swingler Why? Python is becoming the scripting language of choice in big data It has a library for connecting to a MongoDB: PyMongo Nice mapping betwenpython data structures and JSON objects Reduces relational impedance as database objects = dictionary objects 1

Python Dictionary b = {'one': {'a':'a','b':'b'}, 'two': 2, 'three': [3,2,1]} dict b= 'one'= 'a'='a' 'b'='b' b['one']['b']='b' b['two']=2 b['three'][3]=1 'two'=2 'three'= [3,2,1] dict list Let s jump right in PyMongo # connect and authorise (see later) d=db.col.find_one() for k,v in one.iteritems(): # Do something with k (the key) # and v (the value) 2

Connect and Authenticate <% import pymongo from pymongo import MongoClient client = MongoClient( mqr1.cs.stir.ac.uk") client.kms.authenticate(username,pwd) db = client.name colc=db.colcname %> # Database # Collection Python Server Pages (PSP) You can use Python to drive the output of a web page This can connect to a MongoDB Much like using PHP and MySQL Many methods exist from simple scripts to large frameworks We will illustrate things with PSP 3

Make an HTML form Simple Form to DB Run the Python script when the form is submitted Extract the values and put them into a dictionary object Put the dictionary object into the database First, the form in HMTL: : <form action=form.psp method=post> <input type=text name=key> <input type=text name=val><br> <input type=submit value=add> </form> : 4

Extract Values and Add to DB <% key=form.getfirst('key')# Get key value val=form.getfirst('val')# Get row value if key and val: # If both filled in dct[key] = val# Add to dictionary if len(dct) > 0: colc.insert(dct) # Add to db %> Search and Display <% f=colc.find() # Find every entry for a in f: # Each key,value pair for k,v in a.iteritems(): if k!= "_id": # Ignore _id req.write( %s = %s<br> %(k,v)) %> 5

Some Details d=db.col.find_one()returns a single document the first match f=db.col.find()returns a cursor which iterates through documents for example: for d in f: Specific Search db.col.find({name:"bill"}) # Find all documents where Name = "Bill" db.col.find({name: "Bill", Age: 18}) # Find all documents where Name= "Bill " AND Age=18 is the same as db.col.find({$and: [{Name: "Bill"}, {Age: 18}]}) but we must be explicit if we want to use OR: db.col.find({$or: [{Name: "Bill"}, {Age: 17}]}) $Notdoes as you'd expect and$normeans not and not. 6

Regular Expression Search Find names that contain "ev" anywhere find({name: /ev/}) Names that start with "K", ignoring case (K or k) find({name: /^K/i}) Names that contain "a" or "b" or "c" anywhere find({name: /[abc]/}) Find names that end with some numbers find({name: /\w{1,}\d{1,}/}) Bulk Inserts You can use colc.insert(docs)where docsis iterable That is, it is a list of dictionaries that can be iterated names=[{"name":"bill", " Age":18}, {"Name":"Ted", "Age":18}] colc.insert(names) 7

Counting colc.count(query) Sorting colc.find(query).sort("name") 8

Aggregation In SQL we use Count(), Sum().. GROUP BY In MongoDBthere are a number of aggregation options: Aggregate() method Map Reduce Group() method Aggregate db.things.insert({"x": 1, "tags":["dog", "cat"]}) db.things.insert({"x": 2, "tags":["cat"]}) db.things.insert({"x": 2, "tags":["mouse", "cat", "dog"]}) db.things.insert({"x": 3, "tags":[]}) Then from bson.son import SON db.things.aggregate([ {"$unwind": "$tags"}, {"$group": {"_id": "$tags", "count": {"$sum": 1}}}, {"$sort": SON([("count", -1), ("_id", -1)])} ]) Gives [{'count': 3, '_id': 'cat'}, {'count': 2, '_id': 'dog'}, {'count': 1, '_id': 'mouse'}]} http://api.mongodb.org/python/current/examples/aggregation.html 9

In Detail -unwind {"$unwind": "$tags"} $unwind operator turns a single document with and array value into several documents, each with one value from the array in place of the array unwind({"x": 1, "tags":["dog", "cat"]}) Produces {"x": 1, "tags":"dog"}, {"x": 1, "tags":"cat"} In Detail -group {"$group": {"_id":"$tags", "count":{"$sum":1}}} Means create documents like: {"_id": "cat", "count":3} 10

In Detail -sort {"$sort": SON([("count", -1), ("_id", -1)])} Means sort all the documents descending by count. Python dictionaries don t maintain order, so we use SONto sort SONis Serialized OcumentNotation like a dictionary, but ordered Map Reduce PyMongo sapi supports all of the features of MongoDB s map/reduce engine map_reduce(map, reduce, out, full_response=false, **kwargs) map: map function (as a JavaScript string) reduce: reduce function (as a JavaScript string) out: output collection name or out object(dict). SON can be used to specify multiple options. e.g. SON([( replace, <collection name>), ( db, <database name>)]) full_response(optional): if True, return full response to this command -otherwise just return the result collection **kwargs(optional): additional arguments to the map reduce command may be passed as keyword arguments to this helper method db.test.map_reduce(map, reduce, "results", limit=2) 11

Group Write the reducer code in Javascript reducer = Code(""" function(obj, prev){ prev.count++; }""") Run a query providing the key to group by, initial value for the reducer, and the name of the reducer function results = db.things.group(key={"x":1}, condition={}, initial={"count": 0}, reduce=reducer) Iterate through the documents in the result for doc in results: print doc {'count': 1.0, 'x': 1.0}{'count': 2.0, 'x': 2.0}{'count': 1.0, 'x': 3.0} Cursors You might not want your program to retrieve every match to a query at once Perhaps you want to show 20 per page, for example Cursors are how to do this db.test.find()[50] returns an entry db.test.find()[20:25] returns a cursor (contains 5 entries from 20) 12

Cursors Find() returns a cursor: cur = db.test.find() cur.sort( field :1) PyDev Eclipse plug-in In all machines in CS labs Python development environment Includes interactive console 13

PyDev Interactive Console Nice alternative to the Mongo shell PyDev 14

Cursor Example 15