图数据库 OrientDB 安装 及 初步使用
目录
安装 Java
安装OrientDB
命令行操作OrientDB
创建数据库
列出数据库
断开数据库
连接数据库
创建 class
插入记录
查询记录
更新记录
删除记录
删除数据库
玩 Spark GraphX 时接触到了 Spark GraphFrames,通过 Spark GraphFrames 知道了 Cypher 和 Neo4j,和 友人 探讨 图数据库 时,又了解到了 OrientDB。于是,新世界的大门打开了。
安装 Java
如果操作系统是 Windows,请自行百度或谷歌。
如果操作系统是 Ubuntu,可参考 https://blog.csdn.net/yitengtongweishi/article/details/81946349
安装OrientDB
https://orientdb.com/download-2/
点击上述链接,下载相应的版本。笔者选择的是 orientdb-3.0.5.zip。
解压缩该zip文件,得到 orientdb-3.0.5,后续,如没有特别说明,均是在 orientdb-3.0.5 目录或者子目录。
进入 bin 目录
$ ls -lt
total 74
-rw-r--r-- 1 Administrator 197121 1641 八月 2 08:42 oteleporter.bat
-rwxr-xr-x 1 Administrator 197121 1475 八月 2 08:42 oteleporter.sh*
-rwxr-xr-x 1 Administrator 197121 4377 八月 2 08:38 backup.sh*
-rw-r--r-- 1 Administrator 197121 1757 八月 2 08:38 console.bat
-rwxr-xr-x 1 Administrator 197121 1733 八月 2 08:38 console.sh*
-rw-r--r-- 1 Administrator 197121 3670 八月 2 08:38 dserver.bat
-rwxr-xr-x 1 Administrator 197121 4561 八月 2 08:38 dserver.sh*
-rw-r--r-- 1 Administrator 197121 1720 八月 2 08:38 oetl.bat
-rwxr-xr-x 1 Administrator 197121 2083 八月 2 08:38 oetl.sh*
-rw-r--r-- 1 Administrator 197121 268 八月 2 08:38 orientdb.service
-rwxr-xr-x 1 Administrator 197121 1770 八月 2 08:38 orientdb.sh*
-rw-r--r-- 1 Administrator 197121 394 八月 2 08:38 orientdb.upstart
-rw-r--r-- 1 Administrator 197121 4102 八月 2 08:38 server.bat
-rwxr-xr-x 1 Administrator 197121 4634 八月 2 08:38 server.sh*
-rw-r--r-- 1 Administrator 197121 1416 八月 2 08:38 shutdown.bat
-rwxr-xr-x 1 Administrator 197121 1886 八月 2 08:38 shutdown.sh*
由于 笔者 是 在Windows环境下,所以,启动 server.bat,看到如下输出
.
.` `
, `:.
`,` ,:`
.,. :,,
.,, ,,,
. .,.::::: ```` ::::::::: :::::::::
,` .::,,,,::.,,,,,,`;; .: :::::::::: ::: :::
`,. ::,,,,,,,:.,,.` ` .: ::: ::: ::: :::
,,:,:,,,,,,,,::. ` ` `` .: ::: ::: ::: :::
,,:.,,,,,,,,,: `::, ,, ::,::` : :,::` :::: ::: ::: ::: :::
,:,,,,,,,,,,::,: ,, :. : :: : .: ::: ::: :::::::
:,,,,,,,,,,:,:: ,, : : : : .: ::: ::: :::::::::
` :,,,,,,,,,,:,::, ,, .:::::::: : : .: ::: ::: ::: :::
`,...,,:,,,,,,,,,: .:,. ,, ,, : : .: ::: ::: ::: :::
.,,,,::,,,,,,,: `: , ,, : ` : : .: ::: ::: ::: :::
...,::,,,,::.. `: .,, :, : : : .: ::::::::::: ::: :::
,::::,,,. `: ,, ::::: : : .: ::::::::: ::::::::::
,,:` `,,.
,,, .,`
,,. `, VELOCE
`` `.
`` www.orientdb.com
`
2018-08-07 20:48:53:917 INFO Windows OS is detected, 262144 limit of open files will be set for the disk cache. [ONative]
2018-08-07 20:48:53:941 INFO Loading configuration from: D:/orientdb-3.0.5/config/orientdb-server-config.xml... [OServerConfigurationLoaderXml]
2018-08-07 20:48:54:113 INFO OrientDB Server v3.0.5 - Veloce (build 8e57e3ac781054604de425f088bd1b0a276bc760, branch 3.0.x) is starting up... [OServer]
2018-08-07 20:48:54:133 INFO 12817977344 B/12224 MB/11 GB of physical memory were detected on machine [ONative]
2018-08-07 20:48:54:134 INFO Detected memory limit for current process is 12817977344 B/12224 MB/11 GB [ONative]
2018-08-07 20:48:54:135 INFO JVM can use maximum 1963MB of heap memory [OMemoryAndLocalPaginatedEnginesInitializer]
2018-08-07 20:48:54:135 INFO Because OrientDB is running outside a container 2g of memory will be left unallocated according to the setting 'memory.leftToOS' not taking into account heap memory [OMemoryAndLocalPaginatedEnginesInitializer]
初次使用时,会要求设置用户名和密码。
+---------------------------------------------------------------+
| WARNING: FIRST RUN CONFIGURATION |
+---------------------------------------------------------------+
| This is the first time the server is running. Please type a |
| password of your choice for the 'root' user or leave it blank |
| to auto-generate it. |
| |
| To avoid this message set the environment variable or JVM |
| setting ORIENTDB_ROOT_PASSWORD to the root password to use. |
+---------------------------------------------------------------+
Root password [BLANK=auto generate it]: *
设置好后,单击 http://localhost:2480,
出现如上界面,表明安装成功。
命令行操作OrientDB
启动 console.bat,
$ ./console.bat
OrientDB console v.3.0.5 - Veloce (build 8e57e3ac781054604de425f088bd1b0a276bc760, branch 3.0.x) https://www.orientdb.com
Type 'help' to display all the supported commands.
orientdb>
创建数据库
orientdb> CREATE DATABASE remote:localhost/test root root
Creating database [remote:localhost/test] using the storage type [PLOCAL]...
Database created successfully.
Current database is: remote:localhost/test
orientdb {db=test}>
此时,会在 databases 目录下 形成 test 文件夹。
列出数据库
Current database is: remote:localhost/test
orientdb {db=test}> LIST DATABASES
Found 3 databases:
* test
* demodb
* graphxdb
orientdb {db=test}>
断开数据库
orientdb {db=test}> DISCONNECT
Disconnecting from the database [test]...OK
orientdb>
连接数据库
orientdb>
orientdb> CONNECT remote:localhost/graphxdb root root
Connecting to database [remote:localhost/graphxdb] with user 'root'...OK
orientdb {db=graphxdb}>
orientdb {db=graphxdb}>
创建 class
orientdb {db=graphxdb}> create class Categories
Class created successfully.
orientdb {db=graphxdb}> create class Authors
Class created successfully.
orientdb {db=graphxdb}> create class Posts
Class created successfully.
orientdb {db=graphxdb}>
插入记录
orientdb {db=graphxdb}> insert into posts (title,text) values ("My very first post", "This is the very first post I wrote!")
Inserted record '[Posts#49:0{title:My very first post,text:This is the very first post I wrote!} v1]' in 0.026000 sec(s).
orientdb {db=graphxdb}>
也可一次性插入多条记录,
orientdb {db=graphxdb}> insert into posts (title,text) values ("title1", "text1"), ("title2","text2"), ("title3","text3")
Inserted record '[Posts#50:0{title:title1,text:text1} v1, Posts#51:0{title:title2,text:text2} v1, Posts#52:0{title:title3,text:text3} v1]' in 0.009000 sec(s).
orientdb {db=graphxdb}>
查询记录
全表扫描
orientdb {db=graphxdb}>
orientdb {db=graphxdb}> select * from posts
+----+-----+------+------------------+------------------------------------+
|# |@RID |@CLASS|title |text |
+----+-----+------+------------------+------------------------------------+
|0 |#49:0|Posts |My very first post|This is the very first post I wrote!|
|1 |#50:0|Posts |title1 |text1 |
|2 |#51:0|Posts |title2 |text2 |
|3 |#52:0|Posts |title3 |text3 |
+----+-----+------+------------------+------------------------------------+
4 item(s) found. Query executed in 0.006 sec(s).
orientdb {db=graphxdb}>
查询某一列
OrientDB中,有一些常用的关键字
@class : This specifies the class of the record.
@rid : the Record ID (RID) of the record.
@version : This specifies the version number of the record. Every time a record is updated, its version number changes. This is useful for checking concurrent conflicts.
@size : This specifies the record size in bytes.
@type : This specifies the record type. It can be document or binary . The binary records are used to store blob objects.
@this : This specifies the record itself.
下面,实际感受一下这些关键字的输出。
orientdb {db=graphxdb}> select @class, @rid, @version, @size, @type, @this, title from Posts
+----+-----+------+-----+--------+-----+------------------+
|# |@RID |@CLASS|@size|@type |@this|title |
+----+-----+------+-----+--------+-----+------------------+
|0 |#49:0|Posts |56 |document|#49:0|My very first post|
|1 |#50:0|Posts |44 |document|#50:0|title1 |
|2 |#51:0|Posts |44 |document|#51:0|title2 |
|3 |#52:0|Posts |44 |document|#52:0|title3 |
+----+-----+------+-----+--------+-----+------------------+
4 item(s) found. Query executed in 0.027 sec(s).
orientdb {db=graphxdb}>
需要强调的是,查询同样的内容,不同的查询语句,速度上会有很大的差别。
例如,
orientdb {db=graphxdb}> select * from #50:0
+----+-----+------+------+-----+
|# |@RID |@CLASS|title |text |
+----+-----+------+------+-----+
|0 |#50:0|Posts |title1|text1|
+----+-----+------+------+-----+
1 item(s) found. Query executed in 0.014 sec(s).
orientdb {db=graphxdb}>
比
orientdb {db=graphxdb}> select * from posts where @rid=#50:0
+----+-----+------+------+-----+
|# |@RID |@CLASS|title |text |
+----+-----+------+------+-----+
|0 |#50:0|Posts |title1|text1|
+----+-----+------+------+-----+
1 item(s) found. Query executed in 0.072 sec(s).
orientdb {db=graphxdb}>
要快得多。
更新记录
orientdb {db=graphxdb}> update #51:0 set title=2
Updated record(s) '[{count:1}]' in 0.176000 sec(s).
orientdb {db=graphxdb}> select * from posts
+----+-----+------+------------------+------------------------------------+
|# |@RID |@CLASS|title |text |
+----+-----+------+------------------+------------------------------------+
|0 |#49:0|Posts |My very first post|This is the very first post I wrote!|
|1 |#50:0|Posts |title1 |text1 |
|2 |#51:0|Posts |2 |text2 |
|3 |#52:0|Posts |title3 |text3 |
+----+-----+------+------------------+------------------------------------+
4 item(s) found. Query executed in 0.006 sec(s).
orientdb {db=graphxdb}>
删除记录
orientdb {db=graphxdb}> delete from posts where title=2
Delete record(s) '[{count:1}]' in 0.036000 sec(s).
orientdb {db=graphxdb}> select * from posts
+----+-----+------+------------------+------------------------------------+
|# |@RID |@CLASS|title |text |
+----+-----+------+------------------+------------------------------------+
|0 |#49:0|Posts |My very first post|This is the very first post I wrote!|
|1 |#50:0|Posts |title1 |text1 |
|2 |#52:0|Posts |title3 |text3 |
+----+-----+------+------------------+------------------------------------+
3 item(s) found. Query executed in 0.002 sec(s).
orientdb {db=graphxdb}>
删除数据库
orientdb {db=graphxdb}> DROP DATABASE
Database 'graphxdb' deleted successfully
orientdb {server=remote:localhost/graphxdb}>
关于OrientDB,
数据库类型分两种:1、document;2、graph。参考链接1,参考链接2
存储类型有两种:1、plocal;2、memory。参考链接
本文用到的均是种。
相关文章