Monday, July 1, 2013

My Notes: Playframework with MongoDB Part 1 of 2

I found Playframework to be easy to use, a great alternative to Java "whatever" MVC framework out there,  and way easier than most of them (yeah, personal preference).
This blog is to document part of what I've learned about Playframework and how to use MongoDB so I can come back here if I forget something about them.

Here, I'm going to create a very simple web app to catalog pet fish.

What I use:
1. MacOS 10.8.4
2. Playframework 2.1.0, get it from http://www.playframework.com
3. MongoDB: get it from http://www.mongodb.org/downloads
4. Eclipse Juno


Getting and installing MongoDB on MacOS:

Download it from http://www.mongodb.org/downloads
The one I have at the time of writing this blog is mongodb-osx-x86_64-2.4.3.tgz.

$ cd ~/dev
$ tar xzvf ~/Downloads/mongodb-osx-x86_64-2.4.3.tgz

Create a symlink
$ ln -s mongodb-osx-x86_64-2.4.3 mongodb

Put ~/dev/mongodb/bin to your $PATH by updating your ~/.bash_profile

By default mongo uses /data/db/ to store its data, so let make the directories and make yourself the owner

$ sudo mkdir -p /data/db
$ sudo chown -R me /data


Now you can start mongo daemon:

$ cd ~/dev/mongodb/bin
$ ./mongod & 

You should see something like this:

Wed May 26 21:39:50.803 [initandlisten] MongoDB starting : pid=2009 port=27017 dbpath=/data/db/ 64-bit host=My-Mac.local
Wed May 26 21:39:50.803 [initandlisten] db version v2.4.3
Wed May 26 21:39:50.803 [initandlisten] git version: fe1743177a5ea03e91e0052fb5e2cb2945f6d95f
Wed May 26 21:39:50.803 [initandlisten] build info: Darwin bs-osx-106-x86-64-1.local 10.8.0 Darwin Kernel Version 10.8.0: Tue Jun  7 16:33:36 PDT 2011; root:xnu-1504.15.3~1/RELEASE_I386 i386 BOOST_LIB_VERSION=1_49
Wed May 26 21:39:50.803 [initandlisten] allocator: system
Wed May 26 21:39:50.803 [initandlisten] options: {}
Wed May 26 21:39:50.804 [initandlisten] journal dir=/data/db/journal
Wed May 26 21:39:50.804 [initandlisten] recover begin
Wed May 26 21:39:50.805 [initandlisten] recover lsn: 23197557
Wed May 26 21:39:50.805 [initandlisten] recover /data/db/journal/j._0
Wed May 26 21:39:50.806 [initandlisten] recover skipping application of section seq:0 < lsn:23197557
Wed May 26 21:39:50.806 [initandlisten] recover skipping application of section seq:2802916 < lsn:23197557
Wed May 26 21:39:50.807 [initandlisten] recover skipping application of section seq:19276757 < lsn:23197557
Wed May 26 21:39:50.807 [initandlisten] recover skipping application of section seq:19331960 < lsn:23197557
Wed May 26 21:39:50.808 [initandlisten] recover skipping application of section seq:19387211 < lsn:23197557
Wed May 26 21:39:50.808 [initandlisten] recover skipping application of section seq:19442474 < lsn:23197557
Wed May 26 21:39:50.808 [initandlisten] recover skipping application of section seq:19552807 < lsn:23197557
Wed May 26 21:39:50.809 [initandlisten] recover skipping application of section seq:19607957 < lsn:23197557
Wed May 26 21:39:50.809 [initandlisten] recover skipping application of section seq:19773797 < lsn:23197557
Wed May 26 21:39:50.809 [initandlisten] recover skipping application of section more...
Wed May 26 21:39:50.812 [initandlisten] recover cleaning up
Wed May 26 21:39:50.812 [initandlisten] removeJournalFiles
Wed May 26 21:39:50.812 [initandlisten] recover done
Wed May 26 21:39:50.877 [websvr] admin web console waiting for connections on port 28017
Wed May 26 21:39:50.877 [initandlisten] waiting for connections on port 27017


Let's quickly check if your mongo is working.
Launch mongodb shell:

$ cd ~/dev/mongodb/bin
$ ./mongo
MongoDB shell version: 2.4.3
connecting to: test
> _

MongoDB automatically creates database and collections (these are "tables" in relational database) the first time you use them so you don't need to explicitly create them.
Now let's create a new database called mydb

> use mydb
switched to db mydb


Now insert "Guppy" document into "fakeFish" collection (this is, again, table in relational database):

> db.fakeFish.insert( {_id: "guppy", name: "Guppy", latin: "Poecilia reticulata", sizeRangeMilimeter: [40, 60], image: "guppy.jpg"})


Notice that you don't need to create "fakeFish" collection beforehand. MongoDB is document based database so it has no "schema", your application has to know the structure or schema of the documents you are getting from the collections. To make thing simple, you usually group similar documents into their own collections. In this case I'm using "fakeFish" collection and my app knows the structure it's getting.


See what you just added:

> db.fakeFish.find()
{ "_id" : "guppy", "name" : "Guppy", "latin" : "Poecilia reticulata", "sizeRangeMilimeter" : [ 40, 60 ], "image" : "guppy.jpg" }


Let's leave it here and let's install Playframework.

Installing Playframework


Download Playframework from www.playframework.com, unzip it to some directory. Here I just use my home directory:

$ cd ~/
$ unzip play-2.1.0.zip
$ cd play-2.1.0.zip

Edit your ~/.bashrc to put ~/play-2.1.0 to your $PATH

Create playframework app

$ mkdir -p ~/dev/play
$ cd ~/dev/
$ play new play-mongo

       _            _
 _ __ | | __ _ _  _| |
| '_ \| |/ _' | || |_|
|  __/|_|\____|\__ (_)
|_|            |__/

play! 2.1.0 (using Java 1.7.0_09 and Scala 2.10.0), http://www.playframework.org

The new application will be created in /Users/elousf/dev/play/play-mongo

What is the application name? [play-mongo]


Which template do you want to use for this new application? 

  1             - Create a simple Scala application
  2             - Create a simple Java application

> 2
OK, application play-mongo is created.

Have fun!


Assuming java and javac (JDK) are available, you now have a functional web application and let's quickly test it:

$ cd ~/dev/play/play-mongo   
$ play run

[info] Loading project definition from /Users/elousf/dev/play/play-mongo/project
[info] Set current project to play-mongo (in build file:/Users/elousf/dev/play/play-mongo/)

[info] Updating {file:/Users/elousf/dev/play/play-mongo/}play-mongo...
[info] Done updating.                                                                  
--- (Running the application from SBT, auto-reloading is enabled) ---

[info] play - Listening for HTTP on /0:0:0:0:0:0:0:0:9000

(Server started, use Ctrl+D to stop and go back to the console...)
 

From you web browser go to http://localhost:9000 and you should see the web page with big title

"Your new application is ready"

If this is your fist time using Play I highly recommend you to follow some links on that page, that is how I learned Play.

Setting up Play for Eclipse

If you don't have Eclipse you can download it. Since it's so common and most programmers have no problem using it so installing Eclipse is beyond this blog.

Let's create Eclipse project for the "play-mongo" web app we just created.
Go back to your terminal from where you launch "play run" and press CTRL-D to stop the web app

$ pwd
$ /Users/me/dev/play/play-mongo
$ play


[play-mongo] $ eclipse


That would create eclipse project and your directory should look like this (notice .classpath, .project, and .settings/)

$ ls -l
total 64
drwxr-xr-x  14 me  staff    476 May 26 21:05 ./
drwxr-xr-x   8 me  staff    272 May 26 21:06 ../
-rw-r--r--   1 me  staff  17570 May 26 21:05 .classpath
-rw-r--r--   1 me  staff    141 Feb  4 16:51 .gitignore
-rw-r--r--   1 me  staff    266 May 26 21:05 .project
drwxr-xr-x   4 me  staff    136 May 26 21:05 .settings/
-rw-r--r--   1 me  staff    151 Feb  4 16:51 README
drwxr-xr-x   4 me  staff    136 May 26 20:54 app/
drwxr-xr-x   4 me  staff    136 May 26 20:54 conf/
drwxr-xr-x   3 me  staff    102 May 26 21:00 logs/
drwxr-xr-x   7 me  staff    238 May 26 21:00 project/
drwxr-xr-x   5 me  staff    170 May 26 20:54 public/
drwxr-xr-x   6 me  staff    204 May 26 21:00 target/
drwxr-xr-x   4 me  staff    136 May 26 20:54 test/
 

Launch your eclipse and import the project above. Now let's create some pages before we use MongoDB!

Now, we're ready to create one or two web pages backed by MongoDB... next notes (2/2)


No comments: