Skip to main content

ArangoDB

# https://hub.docker.com/_/arangodb
docker run --rm -it -v /etc/localtime:/etc/localtime:ro \
-e ARANGO_ROOT_PASSWORD=password \
-v $PWD/arangodb/data:/var/lib/arangodb3 -v $PWD/arangodb/apps:/var/lib/arangodb3-apps \
-p 8529:8529 \
--name arangodb arangodb --tcp.reuse-address=true --http.hide-product-header=true --query.cache-mode=on

# 远程连接
# 如果是 https 则用 ssl://mydomain.com
arangosh --server.endpoint http+tcp://192.168.1.1:8529 \
--server.username $USERNAME --server.password $PASSWORD --server.database Nodes

# Linux client
curl -O https://download.arangodb.com/arangodb39/Community/Linux/arangodb3-client-linux-3.9.2.tar.gz
tar zxvf arangodb3-client-linux-3.9.2.tar.gz

| env | | ------------------------------------------ | --- | --- | | ARANGO_ROOT_PASSWORD | | ARANGO_NO_AUTH | | 1 | | ARANGO_RANDOM_ROOT_PASSWORD | | 1 | | ARANGODB_OVERRIDE_DETECTED_TOTAL_MEMORY | | 4G | | ARANGODB_OVERRIDE_DETECTED_NUMBER_OF_CORES | | ARANGOSH_ARGS |

Note 可能遇到的问题

  • graph visualizer
    • pick multiple labels #14456
    • Support of multiple attributes for displaying labels #7772

Note 系统限制

  • AQL
    • 字符串拼接使用 CONCAT,不能 +
    • 三元运算 a ? b : c 会导致 b c 都计算 - 不能短路
    • 一次图查询最多涉及 256 个 Collection
    • 最多 1000 注册器 - 变量、查询变量、内部查询变量、中间结果
    • 初始查询最多 4000 节点
    • 一次查询最多 2048 collections/shards
// https://docs.arangodb.com/3.1/Manual/Administration/ManagingUsers.html
// 用户管理
var users = require('@arangodb/users');
// users.save(user, passwd, active, extra)
users.save('admin@testapp', 'mypassword');
users.grantDatabase('admin@testapp', 'testdb');

// 导出查询结果
require('fs').write('/var/lib/arangodb3/export.json', JSON.stringify(aql`FOR n IN Nodes return n`));
// 导出拼接后的数据
require('fs').write(
'/var/lib/arangodb3/export.txt',
db
._query(`return CONCAT_SEPARATOR("\n",FOR n IN Nodes FILTER n.name != null COLLECT col = n.name return col)`)
.toArray()[0],
);
// 将边的数量更新到节点上
FOR doc IN documents
LET inEdgesCount = LENGTH(FOR v IN 1..1 INBOUND doc GRAPH 'edgeGraph' RETURN 1)
LET outEdgesCount = LENGTH(FOR v IN 1..1 OUTBOUND doc GRAPH 'edgeGraph' RETURN 1)
UPDATE doc WITH
{inEdgesCount: inEdgesCount, outEdgesCount: outEdgesCount} In Documents

Model

  • _key
    • 254bytes
    • ^[-a-zA-Z0-9_:.@()+,=;$!*'%]{,254}$
  • Collection
    • Document
    • Edge - _from,_to
  • Graph
    • fromCollections
    • toCollections
    • Vertex - graph 一部分但未用于 edge 定义
    • OUTBOUND: _from → _to, INBOUND: _from ← _to, ANY: _from ↔ _to
    • Algorithms
      • Traversal
      • Shortest Path
      • k Shortest Paths
      • k Paths
      • Distributed Iterative Graph Processing (Pregel)

AQL

插入

INSERT {name: "John Doe", gender: "m"} INTO users;

FOR user IN [
{name: "John Doe",gender: "m"},
{name: "Jane Smith",gender: "f"}
]
INSERT user INTO users;

FOR user IN users FILTER user.active = = 1 INSERT user INTO backup;

FOR i IN 1..1000 INSERT {name: CONCAT("test", i), gender: (i % 2 = = 0 ? "f": "m")} INTO users

更新

UPDATE { _key: "1" }
WITH { name: "John Smith" }
IN users;

FOR user IN users
UPDATE user
WITH { numberOfLogins: 0 } IN users;

FOR user IN users
FILTER user.active == 1
UPDATE user
WITH {
numberOfLogins: LENGTH(
FOR login IN logins
FILTER login.user == user._key
COLLECT WITH COUNT INTO numLogins
RETURN numLogins
)
} IN users;

LET date = DATE_NOW()
FOR user IN users
FILTER user.isImportantUser == null
LET numberOfLogins = (
FOR login IN logins
FILTER login.user == user._key
COLLECT WITH COUNT INTO numLogins
RETURN numLogins
)
FILTER numberOfLogins > 50
UPDATE user
WITH {
isImportantUser: 1,
dateBecameImportant: date
}
IN users;

-- 移除属性
FOR user IN users
UPDATE user WITH { numberOfLogins: null }
IN users
OPTIONS { keepNull: false };

-- 移除部分属性
FOR user IN users
FILTER user.isImportantUser == 1 AND
user.active == 0
UPDATE user
WITH {
isImportantUser: null,
dateBecameImportant: null
}
IN users
OPTIONS { keepNull: false }

FTS

RETURN TOKENS("今天的天气真的很好", "text_zh")

工具

brew install arangodb
  • arangodump
  • arangorestore
  • arangobackup
  • arangoimport
  • arangoexport
  • arangobench
  • arangovpack
    • 用于转换 VelocyPack 的工具
    • 开发调试 ArangoDB 时使用
    • json,json-hex,vpack,vpack-hex
  • arangoinspect

arangoimport

  • file 支持压缩 - gz,bz2
optdemo valNote
typeauto,tsv,csv,json,jsonlauto 基于文件名检测
file-- stdin
definekey=val替换配置中的 @key@
headers-fileheader.csv
on-duplicateerror, ignore, replace, update
overwritefalse替换 collection - 删除之前数据
collection
create-collectionfalse
create-collection-typedocument, edge
create-databasefalse
skip-lines0
skip-validationfalse
separator用于 tsv,csv
quote"
backslash-escapefalse
batch-size8388608单位 byte - 默认 8MB
auto-rate-limitfalse
ignore-missingfalse
preprocess
translateid=_key
remove-attributeattr
datatypekey=string|number|null|boolean
merge-attributesfullName=[firstName]:[lastName]
edge
from-collection-prefix
to-collection-prefix
arangoimport --file "data.csv" --type csv --collection "users"
edge
{"_from": "users/1234", "_to": "users/4321", "desc": "1234 is connected to 4321"}

arangosh

Foxx

Schema Validation

var schema = {
rule: {
properties: {nums: {type: 'array', items: {type: 'number', maximum: 6}}},
additionalProperties: {type: 'string'},
required: ['nums'],
},
level: 'moderate',
message:
"The document does not contain an array of numbers in attribute 'nums', or one of the numbers is bigger than 6.",
};

/* Create a new collection with schema */
db._create('schemaCollection', {schema: schema});

/* Update the schema of an existing collection */
db.schemaCollection.properties({schema: schema});

FAQ

failed to locate javascript.startup-directory directory

  • macOS /usr/local/opt/arangodb/share/arangodb3/
arangosh --javascript.startup-directory /usr/local/opt/arangodb/share/arangodb3/js