ArangoDB & Ashikawa
                             Workshop

                              Frank Celler & Lucas Dohmen, triAGENS, Cologne
                                              RuPy 2012, Brno
                                                 2012-11-16




                              www.arangodb.org (c) f.celler@triagens.de
Donnerstag, 15. November 12
Agenda
                              Brief Introduction to ArangoDB

                              Installing ArangoDB

                              CRUD Operations for Documents

                              ArangoDB Query Language

                              Using the Ruby Driver Ashikawa

                              Build a small Ruby example

                                    www.arangodb.org (c) f.celler@triagens.de
Donnerstag, 15. November 12
multi-model NoSQL database and application
                     server

                     Basically, it's a document store...

                     ...but also supports key / value access

                     ... and provides functionality to store and analyse
                     document relations, making it a graph database,
                     too

                              www.arangodb.org (c) f.celler@triagens.de
Donnerstag, 15. November 12
arangod: the server, written in C++

                     arangosh: JavaScript shell

                     arangoimp: Import for JSON / CSV

                     Ashikawa: Ruby driver



                              www.arangodb.org (c) f.celler@triagens.de
Donnerstag, 15. November 12
Mac OS X

     brew install arangodb                                                                                     installs stable 1.0.4
     brew install --devel arangodb                                                                  installs development 1.1.beta2
     If this is your first install, automatically load on login with:
         mkdir -p ~/Library/LaunchAgents
         cp /usr/local/Cellar/arangodb/1.0.4/homebrew.mxcl.arangodb.plist ~/Library/LaunchAgents/
         launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.arangodb.plist

     If this is an upgrade and you already have the homebrew.mxcl.arangodb.plist loaded:
         launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.arangodb.plist
         cp /usr/local/Cellar/arangodb/1.0.4/homebrew.mxcl.arangodb.plist ~/Library/LaunchAgents/
         launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.arangodb.plist

     To start the ArangoDB server manually, run:
         /usr/local/sbin/arangod

     To start the ArangoDB shell, run:
         arangosh




                                          www.arangodb.org (c) f.celler@triagens.de
Donnerstag, 15. November 12
Download a Package
     Use http://www.arangodb.org/download to download a package for


                     Centos

                     Debian

                     Ubuntu

                     SuSE

                     Mint

                     Mac OS X


                                www.arangodb.org (c) f.celler@triagens.de
Donnerstag, 15. November 12
From the Source
     ~> git clone -b 1.0 https://github.com/triAGENS/ArangoDB
     Cloning into 'ArangoDB'...

     ~> ./build.sh
     ....
     ########################################################
     arangod
     ########################################################

     bin/arangod:
          $Revision:          READLINE 0x0402.hex $
          $Revision:          V8 3.9.4 $
          $Revision:          BASICS 1.0.4 (c) triAGENS GmbH $
          $Revision:          BOOST 1.48.0 $
          $Revision:          BASICS-C 1.0.4 (c) triAGENS GmbH $
          $Revision:          NCURSES ncurses $
          $Revision:          REST 1.0.4 (c) triAGENS GmbH $
          $Revision:          OPENSSL OpenSSL 0.9.8r 8 Feb 2011 $
          $Revision:          LIBEV 4.11 $




                  see http://www.arangodb.org/manuals/current/Compiling.html for details


                                           www.arangodb.org (c) f.celler@triagens.de
Donnerstag, 15. November 12
Arango Shell
     ~> /usr/local/sbin/arangod &
     ~> /usr/local/bin/arangosh
                                                       _
       __ _ _ __ __ _ _ __   __ _ ___ ___| |__
      / _` | '__/ _` | '_  / _` |/ _ / __| '_ 
     | (_| | | | (_| | | | | (_| | (_) __  | | |
      __,_|_| __,_|_| |_|__, |___/|___/_| |_|
                            |___/

     Welcome to arangosh 1.0.4. Copyright (c) 2012 triAGENS GmbH.
     Using Google V8 3.9.24 JavaScript engine.
     Using READLINE 0x0402.hex.

     Connected to Arango DB 127.0.0.1:8529 Version 1.0.4

     ------------------------------------- Help -------------------------------------
     Predefined objects:
        arango:                                          ArangoConnection
        db:                                              ArangoDatabase
        edges:                                           ArangoEdges
     Example:
      > db._collections();                               list all collections
      > db.<coll_name>.all().toArray();                  list all documents
      > id = db.<coll_name>.save({ ... });               save a document
      > db.<coll_name>.remove(<_id>);                    delete a document
      > db.<coll_name>.document(<_id>);                  get a document
      > help                                             show help pages
      > exit




                                           www.arangodb.org (c) f.celler@triagens.de
Donnerstag, 15. November 12
First Steps
     Create a collection (similar to a table)

     arangosh> db._create("cars");
     [ArangoCollection 2769319, "cars" (status loaded)]
     arangosh> db.cars.toArray();
     [ ]

     Create a document in that collection

     arangosh> db._create("cars");
     [ArangoCollection 2769319, "cars" (status loaded)]
     arangosh> db.cars.toArray();
     [ ]

     arangosh> db.cars.save({ manufacturer: "skoda", model: "superb", year: 2010 });
     { error : false, _id : "2769319/4407719", _rev : 4407719 }
     arangosh> db.cars.document("2769319/4407719");
     { year : 2010, manufacturer : "skoda", model : "superb", _id : "2769319/4407719", _rev : 4407719 }




                                                www.arangodb.org (c) f.celler@triagens.de
Donnerstag, 15. November 12
Web Interface
                                   start at http://localhost:8529/




                              www.arangodb.org (c) f.celler@triagens.de
Donnerstag, 15. November 12
Create Read Update Delete


     Create: octavia = db.cars.save({ model: “octavia“ });

     Read: db.cars.document(octavia._id);

     Update: db.cars.replace(octavia, { model: “fabia“ });

     Delete: db.cars.remove(octavia._id);




                              www.arangodb.org (c) f.celler@triagens.de
Donnerstag, 15. November 12
List & Sub-Objects

     arangosh> r = db.cars.save({ model: "yeti",
       address: { city: "Cologne", street: "Trankgasse" },
       drivers: [ "fceller", "lucas" ]});

     arangosh> car = db.cars.document(r);
     {
       model : "yeti",
       address : {
          city : "Cologne",
          street : "Trankgasse"
        },
       drivers : [
          "fceller",
          "lucas"
       ],
       _id : "2769319/6504871",
       _rev : 6504871
     }




                               www.arangodb.org (c) f.celler@triagens.de
Donnerstag, 15. November 12
Import Data

     ~> curl "http:/ /www.arangodb.org/rupy2012/airports.csv" > airports.csv
     ~> head -2 airports.csv
     "id","ident","type","name","latitude_deg","longitude_deg","elevation_ft","continent",...
     6523,"00A","heliport","Total Rf Heliport",40.07080078125,-74.9336013793945,11,"NA",....

     ~> curl "http://www.arangodb.org/rupy2012/names_10000.json" > names_10000.json
     ~> head -2 names_10000.json
     {"name":{"first":"Caren","last":"Ferm"},"gender":"female","birthday":"1971-07-22","contact":{"address":{"street":"3
     Wyoming Cir","zip":"08053","city":"Marlton","state":"NJ"},"email":["caren.ferm@nosql-matters.org","ferm@nosql-
     matters.org","caren@nosql-matters.org"],"region":"856","phone":["856-5374929"]},"likes":
     ["boxing"],"memberSince":"2008-11-07"}
     {"name":{"first":"Jack","last":"Irias"},"gender":"male","birthday":"1967-02-20","contact":{"address":{"street":"7
     Santa fe Way","zip":"19885","city":"Wilmington","state":"DE"},"email":["jack.irias@nosql-matters.org","irias@nosql-
     matters.org","jack@nosql-matters.org"],"region":"302","phone":[]},"likes":
     ["snowboarding"],"memberSince":"2009-04-27"}




                                   www.arangodb.org (c) f.celler@triagens.de
Donnerstag, 15. November 12
Import Data

     ~> /usr/local/bin/arangoimp --type json --collection users --create-collection true names_10000.json
     Connected to Arango DB 127.0.0.1:8529 Version 1.0.4
     ----------------------------------------
     collection           : users
     create               : yes
     reusing ids          : no
     file                 : names_10000.json
     type                 : json
     quote                : "
     separator            : ,
     connect timeout : 5
     request timeout : 300
     ----------------------------------------
     Starting JSON import...

     created             : 10000
     errors              : 0
     total               : 10000




                                   www.arangodb.org (c) f.celler@triagens.de
Donnerstag, 15. November 12
Import Data

     ~> /usr/local/bin/arangoimp --type csv --collection airports --create-collection true airports.csv
     Connected to Arango DB 127.0.0.1:8529 Version 1.0.4
     ----------------------------------------
     collection           : airports
     create               : yes
     reusing ids          : no
     file                 : airports.csv
     type                 : csv
     quote                : "
     separator            : ,
     connect timeout : 5
     request timeout : 300
     ----------------------------------------
     Starting CSV import...

     created             : 43991
     errors              : 0
     total               : 43992




                                   www.arangodb.org (c) f.celler@triagens.de
Donnerstag, 15. November 12
Query Data
            Grep 5 users and return their names and ids
     FOR u IN users
         LIMIT 5
         RETURN u


     arangosh> a = db._createStatement(
       { query: "for u in users limit 5 return u" }).execute()
     [object ArangoQueryCursor]

     arangosh> a.next()
     { _id : "6570407/264716711", _rev : 264716711, gender : "male", birthday :
     "1964-01-09", memberSince : "2008-09-18", name : { last : "Geving", first :
     "Millard" }, contact : { region : "409", phone : ["409-0605391"], address : { zip :
     "75941", city : "Diboll", state : "TX", street : "18 Woodlawn Loop" }, email :
     ["millard.geving@nosql-matters.org", "geving@nosql-matters.org", "millard@nosql-
     matters.org"] }, likes : ["shopping", "skiing"] }




                              www.arangodb.org (c) f.celler@triagens.de
Donnerstag, 15. November 12
Query Data
                  Find out how many users live in each city



     FOR u IN users
       COLLECT city = u.contact.address.city INTO g
       LIMIT 0,20
       RETURN { "city" : city, "users" : length(g) }




                              www.arangodb.org (c) f.celler@triagens.de
Donnerstag, 15. November 12
Query Data
       Find the 5 regions in state CA with the most inhabitants



     FOR u IN users
       FILTER u.contact.address.state == "CA"
       COLLECT region = u.contact.region INTO group
       SORT LENGTH(group) DESC
       LIMIT 0, 5
       RETURN { "region" : region, "count" : LENGTH(group) }




                              www.arangodb.org (c) f.celler@triagens.de
Donnerstag, 15. November 12
Query Data
                        Find the other top 5 hobbies of male users that also like running



     FOR likes IN (
       FOR u IN users
         FILTER u.gender == "male" && "running" IN u.likes
         FOR value IN u.likes
           FILTER value != "running"
           RETURN value
     )
     COLLECT what = likes INTO group
     SORT LENGTH(group) DESC
     LIMIT 0, 5
     RETURN { "what" : what, "count" : LENGTH(group) }




                                   www.arangodb.org (c) f.celler@triagens.de
Donnerstag, 15. November 12
Query Data
                              Find the 10 nearest larger airports around Cologne




     FOR a IN NEAR(airports, 50.67, 6.9, 200, "distance")
       FILTER a.type == "large_airport"
       SORT a.distance ASC
       LIMIT 0, 10
       RETURN { "name" : a.name,
                "code" : a.iata_code,
                "country" : a.iso_country,
                "city" : a.municipality,
                "distance" : CONCAT(TO_STRING(CEIL(a.distance/1000)), ' km') }




                                  www.arangodb.org (c) f.celler@triagens.de
Donnerstag, 15. November 12
Indexes
                                        Create a Geo–Index



     arangosh> db.airports.ensureGeoIndex('latitude_deg', 'longitude_deg');
     {
         id : "716390823/3631628711",
         type : "geo2",
         constraint : false,
         fields : [
            "latitude_deg",
            "longitude_deg"
         ],
         isNewlyCreated : true,
         error : false,
         code : 201
       }




                              www.arangodb.org (c) f.celler@triagens.de
Donnerstag, 15. November 12
Thank You!

                 Stay in Touch:

                       Fork me on github

                       Google Group: ArangoDB

                       Twitter: @fceller & @arangodb

                       www.arangodb.org

                               www.arangodb.org (c) f.celler@triagens.de
Donnerstag, 15. November 12

Rupy2012 ArangoDB Workshop Part1

  • 1.
    ArangoDB & Ashikawa Workshop Frank Celler & Lucas Dohmen, triAGENS, Cologne RuPy 2012, Brno 2012-11-16 www.arangodb.org (c) [email protected] Donnerstag, 15. November 12
  • 2.
    Agenda Brief Introduction to ArangoDB Installing ArangoDB CRUD Operations for Documents ArangoDB Query Language Using the Ruby Driver Ashikawa Build a small Ruby example www.arangodb.org (c) [email protected] Donnerstag, 15. November 12
  • 3.
    multi-model NoSQL databaseand application server Basically, it's a document store... ...but also supports key / value access ... and provides functionality to store and analyse document relations, making it a graph database, too www.arangodb.org (c) [email protected] Donnerstag, 15. November 12
  • 4.
    arangod: the server,written in C++ arangosh: JavaScript shell arangoimp: Import for JSON / CSV Ashikawa: Ruby driver www.arangodb.org (c) [email protected] Donnerstag, 15. November 12
  • 5.
    Mac OS X brew install arangodb installs stable 1.0.4 brew install --devel arangodb installs development 1.1.beta2 If this is your first install, automatically load on login with: mkdir -p ~/Library/LaunchAgents cp /usr/local/Cellar/arangodb/1.0.4/homebrew.mxcl.arangodb.plist ~/Library/LaunchAgents/ launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.arangodb.plist If this is an upgrade and you already have the homebrew.mxcl.arangodb.plist loaded: launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.arangodb.plist cp /usr/local/Cellar/arangodb/1.0.4/homebrew.mxcl.arangodb.plist ~/Library/LaunchAgents/ launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.arangodb.plist To start the ArangoDB server manually, run: /usr/local/sbin/arangod To start the ArangoDB shell, run: arangosh www.arangodb.org (c) [email protected] Donnerstag, 15. November 12
  • 6.
    Download a Package Use http://www.arangodb.org/download to download a package for Centos Debian Ubuntu SuSE Mint Mac OS X www.arangodb.org (c) [email protected] Donnerstag, 15. November 12
  • 7.
    From the Source ~> git clone -b 1.0 https://github.com/triAGENS/ArangoDB Cloning into 'ArangoDB'... ~> ./build.sh .... ######################################################## arangod ######################################################## bin/arangod: $Revision: READLINE 0x0402.hex $ $Revision: V8 3.9.4 $ $Revision: BASICS 1.0.4 (c) triAGENS GmbH $ $Revision: BOOST 1.48.0 $ $Revision: BASICS-C 1.0.4 (c) triAGENS GmbH $ $Revision: NCURSES ncurses $ $Revision: REST 1.0.4 (c) triAGENS GmbH $ $Revision: OPENSSL OpenSSL 0.9.8r 8 Feb 2011 $ $Revision: LIBEV 4.11 $ see http://www.arangodb.org/manuals/current/Compiling.html for details www.arangodb.org (c) [email protected] Donnerstag, 15. November 12
  • 8.
    Arango Shell ~> /usr/local/sbin/arangod & ~> /usr/local/bin/arangosh _ __ _ _ __ __ _ _ __ __ _ ___ ___| |__ / _` | '__/ _` | '_ / _` |/ _ / __| '_ | (_| | | | (_| | | | | (_| | (_) __ | | | __,_|_| __,_|_| |_|__, |___/|___/_| |_| |___/ Welcome to arangosh 1.0.4. Copyright (c) 2012 triAGENS GmbH. Using Google V8 3.9.24 JavaScript engine. Using READLINE 0x0402.hex. Connected to Arango DB 127.0.0.1:8529 Version 1.0.4 ------------------------------------- Help ------------------------------------- Predefined objects: arango: ArangoConnection db: ArangoDatabase edges: ArangoEdges Example: > db._collections(); list all collections > db.<coll_name>.all().toArray(); list all documents > id = db.<coll_name>.save({ ... }); save a document > db.<coll_name>.remove(<_id>); delete a document > db.<coll_name>.document(<_id>); get a document > help show help pages > exit www.arangodb.org (c) [email protected] Donnerstag, 15. November 12
  • 9.
    First Steps Create a collection (similar to a table) arangosh> db._create("cars"); [ArangoCollection 2769319, "cars" (status loaded)] arangosh> db.cars.toArray(); [ ] Create a document in that collection arangosh> db._create("cars"); [ArangoCollection 2769319, "cars" (status loaded)] arangosh> db.cars.toArray(); [ ] arangosh> db.cars.save({ manufacturer: "skoda", model: "superb", year: 2010 }); { error : false, _id : "2769319/4407719", _rev : 4407719 } arangosh> db.cars.document("2769319/4407719"); { year : 2010, manufacturer : "skoda", model : "superb", _id : "2769319/4407719", _rev : 4407719 } www.arangodb.org (c) [email protected] Donnerstag, 15. November 12
  • 10.
    Web Interface start at http://localhost:8529/ www.arangodb.org (c) [email protected] Donnerstag, 15. November 12
  • 11.
    Create Read UpdateDelete Create: octavia = db.cars.save({ model: “octavia“ }); Read: db.cars.document(octavia._id); Update: db.cars.replace(octavia, { model: “fabia“ }); Delete: db.cars.remove(octavia._id); www.arangodb.org (c) [email protected] Donnerstag, 15. November 12
  • 12.
    List & Sub-Objects arangosh> r = db.cars.save({ model: "yeti", address: { city: "Cologne", street: "Trankgasse" }, drivers: [ "fceller", "lucas" ]}); arangosh> car = db.cars.document(r); { model : "yeti", address : { city : "Cologne", street : "Trankgasse" }, drivers : [ "fceller", "lucas" ], _id : "2769319/6504871", _rev : 6504871 } www.arangodb.org (c) [email protected] Donnerstag, 15. November 12
  • 13.
    Import Data ~> curl "http:/ /www.arangodb.org/rupy2012/airports.csv" > airports.csv ~> head -2 airports.csv "id","ident","type","name","latitude_deg","longitude_deg","elevation_ft","continent",... 6523,"00A","heliport","Total Rf Heliport",40.07080078125,-74.9336013793945,11,"NA",.... ~> curl "http://www.arangodb.org/rupy2012/names_10000.json" > names_10000.json ~> head -2 names_10000.json {"name":{"first":"Caren","last":"Ferm"},"gender":"female","birthday":"1971-07-22","contact":{"address":{"street":"3 Wyoming Cir","zip":"08053","city":"Marlton","state":"NJ"},"email":["[email protected]","ferm@nosql- matters.org","[email protected]"],"region":"856","phone":["856-5374929"]},"likes": ["boxing"],"memberSince":"2008-11-07"} {"name":{"first":"Jack","last":"Irias"},"gender":"male","birthday":"1967-02-20","contact":{"address":{"street":"7 Santa fe Way","zip":"19885","city":"Wilmington","state":"DE"},"email":["[email protected]","irias@nosql- matters.org","[email protected]"],"region":"302","phone":[]},"likes": ["snowboarding"],"memberSince":"2009-04-27"} www.arangodb.org (c) [email protected] Donnerstag, 15. November 12
  • 14.
    Import Data ~> /usr/local/bin/arangoimp --type json --collection users --create-collection true names_10000.json Connected to Arango DB 127.0.0.1:8529 Version 1.0.4 ---------------------------------------- collection : users create : yes reusing ids : no file : names_10000.json type : json quote : " separator : , connect timeout : 5 request timeout : 300 ---------------------------------------- Starting JSON import... created : 10000 errors : 0 total : 10000 www.arangodb.org (c) [email protected] Donnerstag, 15. November 12
  • 15.
    Import Data ~> /usr/local/bin/arangoimp --type csv --collection airports --create-collection true airports.csv Connected to Arango DB 127.0.0.1:8529 Version 1.0.4 ---------------------------------------- collection : airports create : yes reusing ids : no file : airports.csv type : csv quote : " separator : , connect timeout : 5 request timeout : 300 ---------------------------------------- Starting CSV import... created : 43991 errors : 0 total : 43992 www.arangodb.org (c) [email protected] Donnerstag, 15. November 12
  • 16.
    Query Data Grep 5 users and return their names and ids FOR u IN users LIMIT 5 RETURN u arangosh> a = db._createStatement( { query: "for u in users limit 5 return u" }).execute() [object ArangoQueryCursor] arangosh> a.next() { _id : "6570407/264716711", _rev : 264716711, gender : "male", birthday : "1964-01-09", memberSince : "2008-09-18", name : { last : "Geving", first : "Millard" }, contact : { region : "409", phone : ["409-0605391"], address : { zip : "75941", city : "Diboll", state : "TX", street : "18 Woodlawn Loop" }, email : ["[email protected]", "[email protected]", "millard@nosql- matters.org"] }, likes : ["shopping", "skiing"] } www.arangodb.org (c) [email protected] Donnerstag, 15. November 12
  • 17.
    Query Data Find out how many users live in each city FOR u IN users COLLECT city = u.contact.address.city INTO g LIMIT 0,20 RETURN { "city" : city, "users" : length(g) } www.arangodb.org (c) [email protected] Donnerstag, 15. November 12
  • 18.
    Query Data Find the 5 regions in state CA with the most inhabitants FOR u IN users FILTER u.contact.address.state == "CA" COLLECT region = u.contact.region INTO group SORT LENGTH(group) DESC LIMIT 0, 5 RETURN { "region" : region, "count" : LENGTH(group) } www.arangodb.org (c) [email protected] Donnerstag, 15. November 12
  • 19.
    Query Data Find the other top 5 hobbies of male users that also like running FOR likes IN ( FOR u IN users FILTER u.gender == "male" && "running" IN u.likes FOR value IN u.likes FILTER value != "running" RETURN value ) COLLECT what = likes INTO group SORT LENGTH(group) DESC LIMIT 0, 5 RETURN { "what" : what, "count" : LENGTH(group) } www.arangodb.org (c) [email protected] Donnerstag, 15. November 12
  • 20.
    Query Data Find the 10 nearest larger airports around Cologne FOR a IN NEAR(airports, 50.67, 6.9, 200, "distance") FILTER a.type == "large_airport" SORT a.distance ASC LIMIT 0, 10 RETURN { "name" : a.name, "code" : a.iata_code, "country" : a.iso_country, "city" : a.municipality, "distance" : CONCAT(TO_STRING(CEIL(a.distance/1000)), ' km') } www.arangodb.org (c) [email protected] Donnerstag, 15. November 12
  • 21.
    Indexes Create a Geo–Index arangosh> db.airports.ensureGeoIndex('latitude_deg', 'longitude_deg'); { id : "716390823/3631628711", type : "geo2", constraint : false, fields : [ "latitude_deg", "longitude_deg" ], isNewlyCreated : true, error : false, code : 201 } www.arangodb.org (c) [email protected] Donnerstag, 15. November 12
  • 22.
    Thank You! Stay in Touch: Fork me on github Google Group: ArangoDB Twitter: @fceller & @arangodb www.arangodb.org www.arangodb.org (c) [email protected] Donnerstag, 15. November 12