Upgrade to Pro — share decks privately, control downloads, hide ads and more …

OrientDB - No More Joins - PythonBrasil[10] 2014

Bruno Rocha
November 10, 2014

OrientDB - No More Joins - PythonBrasil[10] 2014

Talk by Bruno Rocha on 10th Python Brazilian Conference showing how too work with Graph Database OrientDB using Python and presenting the OrientEngine Project

Bruno Rocha

November 10, 2014
Tweet

More Decks by Bruno Rocha

Other Decks in Programming

Transcript

  1. What is a NOSQL project? * O projeto que não

    tem UM banco de dados e sim um ambiente de dados formado por multiplos bancos de dados e não apenas SQL! Exemplo de data-stack: Tipo: Relacional Aplicação: Persistencia / BI / Legado / Finances Tipo: Document Aplicação: Coleta de dados, Mobile apps Tipo: Ultra fast key-value Aplicação: Pub/sub, Real time, chats memcached Tipo: In Memory key-value Aplicação: Cache Tipo: Grafo, Document, Object Oriented Aplicação: Coleta de dados Mobile apps Redes sociais Machine Learning Recomendação Redes complexas Data mining OrientDB
  2. BRUNO PYBR 2014 ORIENT DB PE VERTEX / Vertices /

    Nós / Objetos / Instâncias.
  3. BRUNO PYBR 2014 ORIENT DB PE {“id”: 123, “tipo”: “palestrante”}

    {“id”: 456, “tipo”: “estado”} {“id”: 789, “tipo”: “banco de dados”} {“id”: 101112, “tipo”: “Mega Evento”} Meta dados
  4. BRUNO PYBR 2014 ORIENT DB PE {“id”: 123, “tipo”: “palestrante”}

    {“id”: 456, “tipo”: “estado”} {“id”: 789, “tipo”: “banco de dados”} {“id”: 101112, “tipo”: “Mega Evento”} Travels to attends to subject in talks about uses Happens in Edge, Aresta, Ligação, Link, Adjacencia
  5. BRUNO PYBR 2014 ORIENT DB PE {“id”: 123, “tipo”: “palestrante”}

    {“id”: 456, “tipo”: “estado”} {“id”: 789, “tipo”: “banco de dados”} {“id”: 101112, “tipo”: “Mega Evento”} Travels to attends to subject in talks about uses Happens in meta dados {“since”: 2014} {“when”: 11/2014} {“tracks”: [ “NoSQL”, “Python”]} {“speaker”: true, “since”: 2009 } {“for”: [“social network”, “machine learning”]} {“track”: “NoSQL”, “date”: 2014-11-07}
  6. BRUNO PYBR 2014 ORIENT DB PE {“id”: 123, “tipo”: “palestrante”}

    {“id”: 456, “tipo”: “estado”} {“id”: 789, “tipo”: “banco de dados”} {“id”: 101112, “tipo”: “Mega Evento”} Travels to attends to subject in talks about uses Happens in complexity {“since”: 2014} {“when”: 11/2014} {“tracks”: [ “NoSQL”, “Python”]} {“speaker”: true, “since”: 2009 } {“for”: [“social network”, “machine learning”]} {“track”: “NoSQL”, “date”: 2014-11-07} RENATO Lives in {“since”: 1990} knows worked with
  7. BRUNO PYBR 2014 ORIENT DB PE {“id”: 123, “tipo”: “palestrante”}

    {“id”: 456, “tipo”: “estado”} {“id”: 789, “tipo”: “banco de dados”} {“id”: 101112, “tipo”: “Mega Evento”} Travels to attends to subject in talks about uses Happens in Path Traverse {“since”: 2014} {“when”: 11/2014} {“tracks”: [ “NoSQL”, “Python”]} {“speaker”: true, “since”: 2009 } {“for”: [“social network”, “machine learning”]} {“track”: “NoSQL”, “date”: 2014-11-07} Lives in {“since”: 1990} knows worked with RENATO
  8. GRAPH OBJECT ORIENTED DOCUMENT SQL Language Tinkerpop Blueprints BUILT IN

    CACHE 1999 MVRB+Tree Open Source Enterprise Edition Community ACID Transaction MIXED Scheme Full/ Less
  9. > download at: http://www.orientechnologies.com/download/ > tar -zxvf orientdb….tar.gz > cd

    orientdb-community/ > vim config/orientdb-server-config.xml Edit the configuration file (./config/orientdb-server-config.xml). Add a super-user called "admin". Insert the following line inside the users tags: <user resources="*" password="admin" name="admin"/> > cd bin > ./server.sh start | stop To open the console use: > ./console.sh Quick Start:
  10. connect remote:localhost root copied_password list databases connect remote:localhost/GratefulDeadConcerts admin admin

    create class Vegetable create property Vegetable.name string create property Vegetable.color string create property Vegetable.quantity integer create property Vegetable.good_on_pizza boolean classes info class vegetable browse class vegetable select from vegetable create class Animal extends V create class Food extends V create class Environment extends V create class Eat extends E create class Live extends E create vertex Animal set name = “Rabbit” create vertex Food set name = “Carrot” create vertex Environment set name = “Forest” create edge Eat from ( select from Animal where name = 'Rabbit' ) to ( select from Food where name = 'Carrot' ) create edge Live from ( select from Animal where name = 'Rabbit' ) to ( select from Environment where name = 'Forest' ) select expand( out() ) from Animal
  11. client = pyorient.OrientDB("localhost", 2424) # host, port # open a

    connection (username and password) client.connect("admin", "admin") # create a database client.db_create(“animals”, DB_TYPE_GRAPH, STORAGE_TYPE_MEMORY) # select to use that database client.db_open(“animals”, "admin", "admin")
  12. name: rat specie: rodent name: pea color: green name: man

    specie: human class: Animal class: Food vértice vértice vértice from from to to class: Eat edge class: Eat edge in out out
  13. client.command("create class Animal extends V") client.command("insert into Animal ('name', 'specie')

    values ('rat', 'rodent')") client.query("select * from Animal") [<OrientRecord at 0x7f…>, …] client.command('create class Food extends V') client.command("insert into Food (name, color) values ('pea', 'green')") client.command('create class Eat extends E') eat_edges = client.command(""" create edge Eat from (select from Animal where name in ['rat', ‘man’]) to (select from Food where name = 'pea') """) [edge.out.get() for edge in eat_edges] ['11:0', ‘12:0’] client.record_load(‘11:0’).name 'rat' client.record_load(‘12:0’).name 'man'
  14. name: rat specie: rodent name: pea color: green name: man

    specie: human class: Animal class: Food vértice vértice vértice from from to to class: Eat edge class: Eat edge in out out
  15. # I want to know which animals eats peas pea_eaters

    = client.query( “select expand( in( Eat ) ) from Food where name = ‘pea’” ) for animal in pea_eaters: print animal.rid, animal.name, animal.specie ‘11:0 rat rodent’ ‘12:0 man human’
  16. from orientengine import Vertex, StringProperty from .edges import Eatable, Eater

    class Animal(Vertex, Eater): # Can eat Out specie = StringProperty() name = StringProperty() class Food(Vertex, Eatable): # Can be eated In color = StringProperty() name = StringProperty() from .vertices import Animal, Food # create some instances (vertices) rat = Animal(specie='rodent', name='rat') man = Animal(specie='human', name='man') pea = Food(color='green', name='pea') # define some links (edges) rat.eats(pea) man.eats(pea, amount=30) # or pea.eaters.extend(rat, man) # Who eats peas? pea.eaters [<OrientRecord(rat)>, <OrientRecord(man)>, ...] # or Animal.objects(eats=pea) [<OrientRecord(rat)>, <OrientRecord(man)>, ...]
  17. client.command("create class Animal extends V") client.command('create class Food extends V')

    client.command('create class Eat extends E') eat_edges = client.command(""" create edge Eat from (select from Animal) to (select from Food) """) pea_eaters = client.query( """select expand( in( Eat ) ) from Food WHERE name = ‘pea’ """) class Animal(Vertex, Eater): specie = StringProperty() name = StringProperty() class Food(Vertex, Eatable): color = StringProperty() name = StringProperty() pea = Food(name='pea') for animal in Animal.objects: animal.eats(pea) pea_eaters = pea.eaters # or pea_eaters = Animal.objects(eats=pea) pure PyOrient orientengine Object-Graph-Document-Mapper for PyOrient