Talk by Bruno Rocha on 10th Python Brazilian Conference showing how too work with Graph Database OrientDB using Python and presenting the OrientEngine Project
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
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:
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
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")
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'
= 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’
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)>, ...]
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