rupy2012 arangodb workshop part1

22
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

Upload: arangodb

Post on 29-Aug-2014

1.792 views

Category:

Technology


6 download

DESCRIPTION

 

TRANSCRIPT

ArangoDB & AshikawaWorkshop

Frank Celler & Lucas Dohmen, triAGENS, CologneRuPy 2012, Brno

2012-11-16

www.arangodb.org (c) [email protected]

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) [email protected]

Donnerstag, 15. November 12

www.arangodb.org (c) [email protected]

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

Donnerstag, 15. November 12

www.arangodb.org (c) [email protected]

arangod: the server, written in C++

arangosh: JavaScript shell

arangoimp: Import for JSON / CSV

Ashikawa: Ruby driver

Donnerstag, 15. November 12

Mac OS X

installs stable 1.0.4installs development 1.1.beta2

www.arangodb.org (c) [email protected]

brew install arangodbbrew install --devel arangodb

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

Donnerstag, 15. November 12

Download a Package

www.arangodb.org (c) [email protected]

Use http://www.arangodb.org/download to download a package for

Centos

Debian

Ubuntu

SuSE

Mint

Mac OS X

Donnerstag, 15. November 12

From the Source

www.arangodb.org (c) [email protected]

~> git clone -b 1.0 https://github.com/triAGENS/ArangoDBCloning 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

Donnerstag, 15. November 12

Arango Shell

www.arangodb.org (c) [email protected]

~> /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

Donnerstag, 15. November 12

First Steps

www.arangodb.org (c) [email protected]

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 }

Donnerstag, 15. November 12

Web Interface

www.arangodb.org (c) [email protected]

start at http://localhost:8529/

Donnerstag, 15. November 12

Create Read Update Delete

www.arangodb.org (c) [email protected]

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);

Donnerstag, 15. November 12

List & Sub-Objects

www.arangodb.org (c) [email protected]

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}

Donnerstag, 15. November 12

Import Data

www.arangodb.org (c) [email protected]

~> 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]","[email protected]","[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]","[email protected]","[email protected]"],"region":"302","phone":[]},"likes":["snowboarding"],"memberSince":"2009-04-27"}

Donnerstag, 15. November 12

Import Data

www.arangodb.org (c) [email protected]

~> /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 : userscreate : yesreusing ids : nofile : names_10000.jsontype : jsonquote : "separator : ,connect timeout : 5request timeout : 300----------------------------------------Starting JSON import...

created : 10000errors : 0total : 10000

Donnerstag, 15. November 12

Import Data

www.arangodb.org (c) [email protected]

~> /usr/local/bin/arangoimp --type csv --collection airports --create-collection true airports.csvConnected to Arango DB 127.0.0.1:8529 Version 1.0.4----------------------------------------collection : airportscreate : yesreusing ids : nofile : airports.csvtype : csvquote : "separator : ,connect timeout : 5request timeout : 300----------------------------------------Starting CSV import...

created : 43991errors : 0total : 43992

Donnerstag, 15. November 12

Query Data

www.arangodb.org (c) [email protected]

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]", "[email protected]"] }, likes : ["shopping", "skiing"] }

Grep 5 users and return their names and ids

Donnerstag, 15. November 12

Query Data

www.arangodb.org (c) [email protected]

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

Find out how many users live in each city

Donnerstag, 15. November 12

Query Data

www.arangodb.org (c) [email protected]

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) }

Find the 5 regions in state CA with the most inhabitants

Donnerstag, 15. November 12

Query Data

www.arangodb.org (c) [email protected]

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 groupSORT LENGTH(group) DESCLIMIT 0, 5RETURN { "what" : what, "count" : LENGTH(group) }

Find the other top 5 hobbies of male users that also like running

Donnerstag, 15. November 12

Query Data

www.arangodb.org (c) [email protected]

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') }

Find the 10 nearest larger airports around Cologne

Donnerstag, 15. November 12

Indexes

www.arangodb.org (c) [email protected]

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 }

Create a Geo–Index

Donnerstag, 15. November 12

Thank You!

Fork me on github

Google Group: ArangoDB

Twitter: @fceller & @arangodb

www.arangodb.org

www.arangodb.org (c) [email protected]

Stay in Touch:

Donnerstag, 15. November 12