Skip to content
Zita Szupera edited this page Jun 11, 2015 · 3 revisions

#NOTES FROM THE COURSE

#Week 1: Introduction and Overview

What are we going to do?

Abstract

We are going to build a blog engine, written in Java, which is connected to a MongoDB database. Since this course is a MongoDB cousre, most of the Java code is written for us, so we can concentrate on the database.

Application architecture - Components

  • Database: MongoDB
  • Application:
    • Spark framework: URL mapping, lightweight web framework
    • Freemarker framework: Templating framework, we will use to create HTML templates with placeholders
    • MongoDB Java driver
      • Communicates with the database via TCP

Introduction to MongoDB

What is MongoDB?

  • Non-rational database
  • Stores JSON-s (actually BSON-s, which are the binary representation of JSON)
  • Documents can have hierarchy: nested lists and dictionaries
  • Schemaless
  • Lightweight

Relative to relational DBMS-es

MongoDB was designed to find a good compromise between functionality and scalability. Result: MongoDB scales better, than relational DBMS-es, but has less functionality (eg.: no joins, no transactions, BUT document modification is atomic)

Mongo program

We can access the DBMS directly (without drivers) via the mongo shell program, which has a JavaScript interface. You can start mongo shell by typing mongo into the terminal. Information about it's methods: http://docs.mongodb.org/manual/reference/method/

Playing with MongoDB

  • Start MongoDB: mongod --dbpath <path to directory> (Changed the default data directory)
  • Start mongo: mongo (Connects to the default database, test)
Note to self:
  echo $PATH   
  PATH=$PATH:<path to directory>
  • List avaiable databases: show dbs (Empty databases are not displayed)
  • Change to database: use <database name> (Creates if not exsists)
  • Save new item: db.<Collection name>.save(<JSON>)
  • List all collections of the database: show collections
Use TAB for autocompletion
  • List items in a collection: db.<Collection name>.find()
The documents we store in our database are placed in collections. A collection is like a table in a relational database, but it's schemaless and stores JSON documents instead of records.
We can see, that every item has an attribute called _id, it is a unique identifier, generated if not given, an index is built on it.

JSON

Introduction

MongoDB stores JSON-s, which stands for JavaScript Object Notation. It is a commonly used data-interchange format, it's standardized.

Syntax: http://json.org/

Good to know

  • Using "" in the keys is not required (at least in MongoDB), but recommended
  • {} -> valid JSON
  • {"a" : {"a" : 1}} -> valid JSON

Frameworks

The 2 frameworks which we are going to use are:

  • Spark
  • Freemarker

Spark

  • micro web framework for Java applications
  • helps mapping URL-s
  • it has a built-in webserver called Jetty
  • info: http://sparkjava.com/

Freemaker