OrientDB 学习笔记之常用命令

2022-04-08 00:00:00 创建 数据库 删除 命令 记录

OrientDB 学习笔记

简明教程
#创建类
create class Person extends V
create class Company extends V
create class WorkAt extends E
#创建属性
create property Person.firstName string
create property Person.lastName string
create property Company.name string
#插入数据
insert into Person (firstName, lastName) values ("Gary", "White"), ("Join", "Steven"), ("Lin", "Tom")
insert into Company set name = "ABC"
insert into Company set name = "XYZ"
#查询
select from V

这个也不错
https://idlesummerbreeze.wordpress.com/2015/05/18/oirentdb-学习笔记/

数据库命令
创建
CREATE DATABASE <database-url> [<user> <password> <storage-type> [<db-type>]]
CREATE DATABASE remote:localhost/GratefulDeadConcerts root ****
CREATE DATABASE PLOCAL:/opt/orientdb/databses/demo root ****

更改属性
ALTER DATABASE <attribute-name> <attribute-value>

备份数据库
./backup.sh <dburl> <user> <password> <destination> [<type>]
backup.sh plocal: opt/orientdb/database/demo admin admin ./backup-demo.zip

#或者
CONNECT PLOCAL:/opt/orientdb/databases/demo admin admin
BACKUP DATABASE ./backup-demo.zip

还原数据库
RESTORE DATABSE <url of the backup zip file>
CONNECT PLOCAL:/opt/orientdb/databases/demo admin admin
RESTORE DATABASE backup-demo.zip

链接数据库
RESTORE DATABASE backup-demo.zip
CONNECT PLOCAL:/opt/orientdb/databases/demo admin admin
CONNECT remote:localhost/GratefulDeadConcerts admin admin

断开数据库
DISCONNECT

查看info
CONNECT remote:localhost/GratefulDeadConcerts admin admin
info

列出所有数据库
CONNECT remote:localhost root ****
LIST DATABASES

冻结数据库,不响应任何读取和写入状态
CONNECT remote:localhost/GratefulDeadConcerts admin admin
FREEZE DATABASE

释放数据库
CONNECT remote:localhost/GratefulDeadConcerts admin admin
RELEASE DATABASE

配置数据库
CONNECT remote:localhost/GratefulDeadConcerts admin admin
CONFIG #(查看)
CONFIG SET <config-variable> <config-value> #(设置)
CONFIG SET tx.autoRetry 5
CONFIG GET <config-variable> #(获取)

导出
CONNECT remote:localhost/GratefulDeadConcerts admin admin
EXPORT DATABASE <output file>
EXPORT DATABASE ./export-demo.export

导入
CONNECT remote:localhost/GratefulDeadConcerts admin admin
IMPORT DATABASE <output file>
IMPORT DATABASE ./export-demo.export

删除
DROP DATABASE [<database-name> <server-username> <server-user-password>]
#1
CONNECT remote:localhost/GratefulDeadConcerts admin admin
orientdb {db = demo}>
#2
DROP DATABASE PLOCAL:/opt/orientdb/databases/demo admin admin

Record命令
插入
#1 创建database-->创建类-->创建property
CREATE DATABASE plocal:/opt/orientdb/databases/sales
create class Customer
create property Customer.id integer
create property Customer.name String
create property Customer.age integer

#2 插入
INSERT INTO [class:]<class>|cluster:<cluster>|index:<index>
[(<field>[,]*) VALUES (<expression>[,]*)[,]*]|
[SET <field> = <expression>|<sub-command>[,]*]|
[CONTENT {<JSON>}]
[RETURN <expression>]
[FROM <query>]
INSERT INTO Customer (id, name, age) VALUES (01,'satish', 25)
INSERT INTO Customer SET id = 02, name = 'krishna', age = 26
INSERT INTO Customer CONTENT {"id": "03", "name": "kiran", "age": "29"}
INSERT INTO Customer (id, name, age) VALUES (04,'javeed', 21), (05,'raja', 29)

#查看
SELECT FROM Customer

查看
SELECT [ <Projections> ] [ FROM <Target> [ LET <Assignment>* ] ]
[ WHERE <Condition>* ]
[ GROUP BY <Field>* ]
[ ORDER BY <Fields>* [ ASC|DESC ] * ]
[ UNWIND <Field>* ]
[ SKIP <SkipRecords> ]
[ LIMIT <MaxRecords> ]
[ FETCHPLAN <FetchPlan> ]
[ TIMEOUT <Timeout> [ <STRATEGY> ] ]
[ LOCK default|record ]
[ PARALLEL ]
[ NOCACHE ]

SELECT FROM Customer
SELECT FROM Customer where name like 'k%' #k开头的所有记录
SELECT FROM Customer WHERE name.left(1) = 'k'
SELECT id, name.toUpperCase() FROM Customer #名字大写
SELECT FROM Customer WHERE age in [25,29] #年纪
SELECT FROM Customer WHERE ANY() LIKE '%sh%' #任意字段包含sh
SELECT FROM Customer ORDER BY age DESC

加载记录,根据id查找记录
SELECT FROM Customer
LOAD RECORD #25:0
重新加载记录,还是根据id查找记录,区别是忽略缓存
SELECT FROM Customer
RELOAD RECORD #25:0

导出记录,可以导出为json,感觉比较爽
SELECT FROM Customer
LOAD RECORD #25:0
EXPORT RECORD json

{
"@type": "d",
"@rid": "#25:0",
"@version": 1,
"@class": "Customer",
"name": "satish",
"id": 1,
"age": 25
}

更新
connect plocal:/opt/orientdb/databases/sales admin admin
select from Customer
UPDATE Customer SET age = 28 WHERE name = 'Raja'

删除
connect plocal:/opt/orientdb/databases/sales admin admin
select from Customer
DELETE FROM <Class>|cluster:<cluster>|index:<index>
[LOCK <default|record>]
[RETURN <returning>]
[WHERE <Condition>*]
[LIMIT <MaxRecords>]
[TIMEOUT <timeout>]
DELETE FROM Customer WHERE id = 4

类命令
创建
CREATE DATABASE plocal:/opt/orientdb/databases/sales
#or
connect plocal:/opt/orientdb/databases/sales admin admin
create class Customer
create property Customer.id integer
create property Customer.name String
create property Customer.age integer

#or
CREATE CLASS Account
CREATE CLASS Car EXTENDS Account
CREATE CLASS Person ABSTRACT #抽象类

改变类
CREATE DATABASE plocal:/opt/orientdb/databases/sales
#or
connect plocal:/opt/orientdb/databases/sales admin admin

ALTER CLASS <class> <attribute-name> <attribute-value>

#搞不懂有啥作用
#为现有类“Employee”定义超类“Person”。
ALTER CLASS Employee SUPERCLASS Person
#为现有类“Employee”添加超类“Person”
ALTER CLASS Employee SUPERCLASS +Person

删除类
DROP CLASS <class>
DROP CLASS Employee

集群命令
用于储存记录、文档或顶点

创建
CREATE CLUSTER <cluster> [ID <cluster-id>]
CREATE CLUSTER sales

改变
ALTER CLUSTER Employee NAME Employee2
ALTER CLUSTER 12 NAME Employee
#命令将集群冲突策略更改为自动合并
ALTER CLUSTER V CONFICTSTRATEGY automerge

删除
DROP CLUSTER <cluster-name>|<cluster-id>
1
属性相关命令
创建
CREATE DATABASE plocal:/opt/orientdb/databases/sales
#or
connect plocal:/opt/orientdb/databases/sales admin admin
create class Customer
create property Customer.id integer
create property Customer.name String
create property Customer.age integer

CREATE PROPERTY Employee.name STRING

改变
ALTER PROPERTY <class>.<property> <attribute-name> <attribute-value>
attribute-name就是NAME等属性
ALTER PROPERTY Customer.age NAME born
#改边为必须属性
ALTER PROPERTY Customer.name MANDATORY TRUE

删除
DROP PROPERTY <class>.<property> [FORCE]
DROP PROPERTY Customer.age

顶点相关命令
创建
CREATE VERTEX [<class>] [CLUSTER <cluster>] [SET <field> = <expression>[,]*]
CREATE VERTEX
CREATE CLASS V1 EXTENDS V
CREATE VERTEX V1
CREATE VERTEX V1 SET brand = 'maruti', name = 'swift'

移动
MOVE VERTEX <source> TO <destination>
[SET [<field>=<value>]* [,]]
[MERGE <JSON>]
[BATCH <batch-size>]

select from V
+----+-----+------+
|# |@RID |@CLASS|
+----+-----+------+
|0 |#9:0 |V |
|1 |#50:0|V1 |
+----+-----+------+
create class Employee extends V
move vertex #50:0 to class:Employee
select from V
+----+-----+--------+
|# |@RID |@CLASS |
+----+-----+--------+
|0 |#9:0 |V |
|1 |#58:0|Employee|
+----+-----+--------+

删除
DELETE VERTEX <vertex> [WHERE <conditions>]
[LIMIT <MaxRecords>>] [BATCH <batch-size>]
DELETE VERTEX #14:1
select from Customer
+----+-----+--------+----+-------+----+
|# |@RID |@CLASS |id |name |age |
+----+-----+--------+----+-------+----+
|0 |#25:0|Customer|1 |satish |25 |
|1 |#26:0|Customer|2 |krishna|26 |
|2 |#27:0|Customer|3 |kiran |29 |
|3 |#29:0|Customer|5 |raja |29 |
+----+-----+--------+----+-------+----+
DELETE VERTEX Customer WHERE isSpam = TRUE
delete vertex Customer where name = satish
select from Customer
+----+-----+--------+----+-------+----+
|# |@RID |@CLASS |id |name |age |
+----+-----+--------+----+-------+----+
|0 |#25:0|Customer|1 |satish |25 |
|1 |#26:0|Customer|2 |krishna|26 |
|2 |#27:0|Customer|3 |kiran |29 |
|3 |#29:0|Customer|5 |raja |29 |
+----+-----+--------+----+-------+----+

边缘命令
创建
CREATE EDGE <class> [CLUSTER <cluster>] FROM <rid>|(<query>)|[<rid>]* TO <rid>|(<query>)|[<rid>]*
[SET <field> = <expression>[,]*]|CONTENT {<JSON>}
[RETRY <retry> [WAIT <pauseBetweenRetriesInMs]] [BATCH <batch-size>]
CREATE EDGE FROM #11:4 TO #13:2
CREATE CLASS E1 EXTENDS E

select from V
+----+-----+--------+
|# |@RID |@CLASS |
+----+-----+--------+
|0 |#9:0 |V |
|1 |#58:0|Employee|
+----+-----+--------+
create class e1 extends E
select from E

create edge E1 from #9:0 to #58:0
select from E
+----+-----+------+----+-----+
|# |@RID |@CLASS|out |in |
+----+-----+------+----+-----+
|0 |#66:0|e1 |#9:0|#58:0|
+----+-----+------+----+-----+

更新
UPDATE EDGE <edge>
[SET|INCREMENT|ADD|REMOVE|PUT <field-name> = <field-value> [,]*]|[CONTENT|MERGE <JSON>]
[RETURN <returning> [<returning-expression>]]
[WHERE <conditions>]
[LOCK default|record]
[LIMIT <max-records>] [TIMEOUT <timeout>]
UPDATE EDGE address SET out = (SELECT FROM Address WHERE areaID = 001)

select from V
+----+-----+--------+-------+------+-----+-------+
|# |@RID |@CLASS |out_e1 |brand |name |in_e1 |
+----+-----+--------+-------+------+-----+-------+
|0 |#9:0 |V |[#66:0]| | | |
|1 |#50:1|V1 | |maruti|swift| |
|2 |#58:0|Employee| | | |[#66:0]|
+----+-----+--------+-------+------+-----+-------+
select from E
+----+-----+------+----+-----+
|# |@RID |@CLASS|out |in |
+----+-----+------+----+-----+
|0 |#66:0|e1 |#9:0|#58:0|
+----+-----+------+----+-----+
update edge #60:0 set in = #50:1
select from E
+----+-----+------+----+-----+
|# |@RID |@CLASS|out |in |
+----+-----+------+----+-----+
|0 |#66:0|e1 |#9:0|#58:0|
+----+-----+------+----+-----+

删除
DELETE EDGE
( <rid>
|
[<rid> (, <rid>)*]
|
( [ FROM (<rid> | <select_statement> ) ] [ TO ( <rid> | <select_statement> ) ] )
|
[<class>]
(
[WHERE <conditions>]
[LIMIT <MaxRecords>]
[BATCH <batch-size>]
))

DELETE EDGE FROM #11:2 TO #11:10 WHERE date >= "2012-01-15"
DELETE EDGE FROM #11:5 TO #11:10 WHERE @class = 'Customer'
delete edge #66:0

orientdb概念
图形函数
SELECT out() from Vehicle
select from E

+----+-----+------+----+-----+
|# |@RID |@CLASS|out |in |
+----+-----+------+----+-----+
|0 |#17:0|E |#9:0|#50:1|
+----+-----+------+----+-----+

select from V

+----+-----+--------+-------+-------+------+-----+-------+-------+
|# |@RID |@CLASS |out_e1 |out_ |brand |name |in_ |in_e1 |
+----+-----+--------+-------+-------+------+-----+-------+-------+
|0 |#9:0 |V |[#66:0]|[#17:0]| | | | |
|1 |#50:1|V1 | | |maruti|swift|[#17:0]| |
|2 |#58:0|Employee| | | | | |[#66:0]|
+----+-----+--------+-------+-------+------+-----+-------+-------+

SELECT out() from E

+----+-----+
|# |out()|
+----+-----+
|0 | |
+----+-----+

SELECT out() from V

+----+-------+
|# |out() |
+----+-------+
|0 |[#50:1]|
|1 |[] |
|2 |[] |
+----+-------+


SELECT both() FROM #11:3

数学公式
SELECT SUM(salary) FROM Employee
SELECT avg(salary) FROM Employee
SELECT ID, set(teacher.id) AS teacherID from classess where class_id = 9

其他
SELECT if(eval("name = 'satish'"), "My name is satish","My name is not satish") FROM Employee

相关文章