下载安装
进入官网:OrientDB社区版下载,下载所需要的版本。
安装之前需要安装Java。
Python连接
注意:现阶段Python不支持OrientDB 3.X 版本,只支持OrientDB 2.X 版本。
如果你使用OrientDB 3.X 版本,会报以下错误:
PyOrientWrongProtocolVersionException:
Protocol version 37 is not supported yet by this client.
Pyorient:使用技巧:Pyorient使用方法
一、你仍然可能无法连接,如果报错:Server sent empty string。这时需要通过OGM来连接。
from pyorient.ogm import Graph, Config
config = Config.from_url('/localhost/mydb','root','root')
g = Graph(config, 'admin', 'admin')
g.client.command('') # 里面写命令即可
通过这种方式可以实现Python编辑OrientDB。
OGM封装了方法,有足够时间的话,可以看下Graph里面的源码,就知道为什么它可以成功连接。
二、查看Graph源码之后我们可以精简代码:
config = Config.from_url('/localhost/mydb', 'root', 'root')
client = pyorient.OrientDB(config.host, config.port, config.serialization_type)
client.connect(config.user, config.cred)
client.db_open('mydb', 'root', 'root')
client.command('') # 里面写命令即可
三、按照此思路可以接着将Config精简,以下是代码。
class OrientDB(object):
def __init__(self, host="localhost", db_name=None, port=2424, user="", pswd=""):
self.config = Config(host, port, user, pswd, db_name)
self.__init_client()
def __init_client(self):
self.client = pyorient.OrientDB(self.config.host, self.config.port, self.config.serialization_type)
self.client.connect(self.config.user, self.config.pswd)
if not self.client.db_exists(self.config.db_name):
self.client.db_create(self.config.db_name)
self.client.db_open(self.config.db_name, self.config.user, self.config.cred)
# 用的时候可以这样调用
mydb = OrientDB(host, db, port, user, pswd)