简介

pyorient是orientdb的python库

该库提供两种访问orientdb的方式:1、client 的方式

2、ogm 的方式(类似于ORM)

由于OGM 封装了client,且由于OGM 易于理解,操作简单,所以,这里主要介绍OGM。

创建/连接数据库

导入OGM 所需模块

from pyorient.ogm import Graph, Config

实例化 graph

config = Config.from_url('/localhost/test','root','root')
graph = Graph(config,'admin','admin')

 :

  • config 中的两个 root 分别为orientdb 实例的用户名和密码。类似于如下命令

orientdb > connect remote:localhost root root

graph 中的两个 admin 即想要连接的数据库的用户名和密码

  • 由于Graph() 会调用open 方法,所以,如果数据库中没有将要连接的数据库,那么就会创建一个新的,相应的数据库。

操作数据库

映射

和ORM类似,OGM会将 python 代码中的 class 映射到orientdb中的class。相当于关系型数据库中的表;python中class的属性即为orientd中class的property。即相当于关系型数据库中表的字段/列

想要将python中的class 映射到orientdb 中的class,需要将python中的class进行注册。

代码如下:

from pyorient.ogm.declarative import declarative_node, declarative_relationship

Node = declarative_node()
Relationship = declarative_relationship()

class Person(Node):
    pass

class Likes(Relationship):
    pass

graph.create_all(Node.registry)
graph.create_all(Relationship.registry)

  • 每一次调用declarative_node( ) 和 declarative_relationship( ) 都会生成相应的注册表。其实Node和Relationship是两个基类。
  • create_all( ) 命令就会在orientdb中创建相应的class,如果该class已经存在,那么就会更新该class。

Boker (代理)

OGM 拥有多层映射。

上面提到的python class 到 orientdb class 是一层映射。另一层映射是python class 的object(对象) 和orientdb中具体的 vertex 和 edge 之间的映射。Boker 即工作在这一层。该层映射成功后,会自动为每一个class 创建个一个boker对象。需要注意的是vertex类需要有element_plural 属性,relationship类需要有 label 属性。

代码如下:

class Foo(Node):
    element_plural = 'foos'
    name = String()
class Friend(Relationship):
    label = 'friends'
graph.include(Node.registry)   
graph.foos.create(name='Bar')
Foo.objects.create(name='Baz')

find_bar = graph.foos.query(name='Bar')

  • graph.foos 和Foo.objects 都是一样的,都是boker对象。
  • Relationship 的子类,在orientdb中 是用 label 来命名类名的。如 Friend 类在数据库中的类名为friends
  • query() 反回的是一个查询集对象。

命令

inE()  outE() bothE()

返回进/出/进出自一个顶点或者类的 边的对象

in_() out() both()

返回 进/出/进出 自一个边的或者类的 顶点的对象。

get_vertex()  get_edge()  get_element()

获取 顶点/边/顶点或者边

element 包括顶点和边两种对象。

create_class()  create_vertex() create_edge() drop_class()

创建 顶点或边/顶点/边 ; 删除类

open()   drop()

打开/关闭一个数据库

 : 调用open( )时,如果该数据库不存在,则自动创建。

调用drop( ) 时,需要有一个已经打开的数据库。

query()

返回一个query对象。具有all count group_by order_by filter 等方法,可以对查询结果进行处理。

update()   clear_registry()

更新python中orientdb对象的元素注册表 /清除元素注册表

字段类型

在OGM中,python类的属性就是orientdb 的property,也就是相当于关系型数据库中的字段/列。在pyorient中,字段类型有 String Boolean Float Date 等类型。这些字段又有如下属性。

name 会覆盖掉 “=” 前面命名的属性名。

nullable 允许为空

readonly 只读

default 默认值

indexed 是否为索引值

unique 是否唯一,如果为真,那么会创建相应的索引。

mandatory 是否强制有值。如果没有设置可以为空,那么默认设定为强制有值。

pyorient的更多相关文章

随机推荐

  1. WIN7\win10下使用批处理配置JAVA环境变量

    我找了很多环境变量批处理的教程,都不太满意,因此综合修改了下,拼凑出了这么一个版本. 下面这个是我主要参考的博客,大部分的代码都是来自这里: http://blog.csdn.net/lpy36543 ...

  2. Angular 4 子路由

    子子路由 现在要为产品组件增加两个子组件 1. 创建productDesc和sellerInfo两个组件 ng g component productDesc ng g component selle ...

  3. ipconfig出现window IP configuration 。。

    我的电脑  右击 -->属性 ---->硬件---->设备管理器 安装以太网网卡 1.自动  电脑重新开机会自动提示安装 2.手动  下载以太网网卡驱动 ,选中合适类型(一般为int ...

  4. 豆瓣源安装python包

    例如安装scrapy: pip install -i https://pypi.douban.com/simple/ scrapy

  5. 关于 android 读取当前手机号码

    手机号码不是所有的都能获取.只是有一部分可以拿到.这个是由于移动运营商没有把手机号码的数据写入到sim卡中.SIM卡只有唯一的编号,供网络与设备识别那就是IMSI号码,手机的信号也可以说是通过这个号码 ...

  6. IE下的Firebug——IE WebDeveloper js debug

    原文地址:http://blog.csdn.net/wangbin1986/article/details/6837285 对于大部分做前端设计者而言应该都使用过Firefox浏览器下一款调试网站的扩 ...

  7. 解决Mac nginx问题 [emerg] 54933#0: bind() to 0.0.0.0:80 failed (13: Permission denied)

    brew services restart nginx Stopping nginx... (might take a while) ==> Successfully stopped nginx ...

  8. 深度强化学习:入门(Deep Reinforcement Learning: Scratching the surface)

    RL的方案 两个主要对象:Agent和Environment Agent观察Environment,做出Action,这个Action会对Environment造成一定影响和改变,继而Agent会从新 ...

  9. 更新OpenSSH

    1.安装必要组件: yum install -y gcc openssl-devel pam-devel rpm-build 2.下载OpenSSH最新版本: https://ftp.openbsd. ...

  10. 并发工具类(三)控制并发线程的数量 Semphore

    前言   JDK中为了处理线程之间的同步问题,除了提供锁机制之外,还提供了几个非常有用的并发工具类:CountDownLatch.CyclicBarrier.Semphore.Exchanger.Ph ...