一、简介

  1. Neo4j-CQL是声明性模式匹配语言,遵循SQL语法,简单可读,相关命令如下

image.png

  1. cypher语言描述实例

image.png

  1. (fox)<-[:knows]-(周瑜)-[:knows]->(诸葛)-[:knows]->(fox)
    1. ()内为实体,[]内为关系,箭头表示方向

      二、常用命令

  1. 官方文档https://neo4j.com/docs/cypher-manual/current/clauses/
  2. load csv
    1. load csv from 'file:///name.csv' as line
    2. create (:labelName { from:line[a], relation:line[b], to:line[c]})
      1. Path路径下的csv文件按行读入,对每一行创建关系line[a]-[:line[b]]->line[c],标签为labelName
      2. name.csv应该放在D:\Neo4j\neo4j-community-3.5.31\import目录下
  3. create
    1. 创建节点:create (l:LabelName{property1:value1,...,propertyn:valuen})
      1. LabelName为标签名,property为属性名
    2. 创建关系:create (nodeA)-[:RelationType{relation:value,...}]->(nodeB)
      1. 创建节点A和节点B之间的RelationType关系
    3. 创建节点属于多个标签:create (l:LabelNameA:LabelNameB:,...{property1:value1,...,propertyn:valuen})
  4. match
    1. 查询节点:match (l:LabelName) return l
      1. 查询节点标签为LabelName的所有节点
    2. 查询特定节点:match (l:LabelName{property:value,...}) return l
      1. 查询节点类型为LabelName且满足property限制的节点
      2. 也可以使用where关键字代替property限制
        1. match (l:LabelName) where l.property=value,... return l
    3. 查询关系:match l=(:LabelName{property:value,...})->[r:RelationType]->() return l
  5. return
    1. 在使用match命令后,若需要将特定的property返回,则添加return关键字
      1. match (l:LabelName) return l.property,...
      2. 在返回节点的id时需要使用id(l)的方式,即match (l:LabelName) return id(l),因为id属性是由Neo4j自动创建的
  6. delete
    1. 删除节点:match(l:LabelName{l.property:value,...}) delete l
      1. 如果当前删除的节点l存在与其他节点的关系,则需要先删除对应的关系
    2. 删除关系:match(lA:LabelName{lA.property:value,...})-[r:RelationType]->(lB:LabelName{lB.property:value,...}) delete r
  7. skip, limit
    1. limit n:限制只返回前n个结果
    2. skip n:限制忽略前n个结果
  8. remove
    1. 删除属性:match(l:LabelName{property:value,...}) remove l.property,... return l
    2. 删除节点特定标签:match(l:LabelName:...{property:value,...}) remove l:LabelName return l
      1. 节点有多个标签时使用
  9. set
    1. 修改属性值:match(l:LabelName{property:value,...}) set l.property=value,... return l
  10. order by
    1. 按照id排序:match(l:LabelName{property:value,...}) return l order by id(l) desc/asc
    2. 按照特定属性排序:match(l:LabelName{property:value,...}) return l order by l.property desc/asc
  11. null
    1. 根据特定属性是否为null区分重名节点:match(l:LabelName{property:value,...}) where l.property is/is not null return l
  12. in
    1. 查询属性在特定列表中的节点:match(l:LabelName) where l.property in[valueA,...,valueN] return l
  13. index索引:
    1. 创建索引:create index on :LabelName(property)
      1. 为标签LabelName对应的节点的property属性添加索引
    2. 删除索引:drop index on :LabelName(property)
      1. 删除标签LabelName对应的节点的property属性的索引
  14. unique约束:
    1. 创建唯一约束:create constraint on (l:LabelName) assert l.property is unique
      1. 约束标签为LabelName的节点的property属性必须唯一
    2. 删除唯一约束:drop constraint on (l:LabelName) assert l.property is unique
  15. distinct
    1. 保证返回值不存在重复值:match(l:LabelName) return distinct(l.property)
  16. 更多命令详见官方文档…

    三、常用函数

  17. String字符串:用于使用String自变量

    1. upper:将所有字母换为大写
    2. lower:将所有字母换为小写
    3. substring:截取字符串
      1. match (l:LabelName) return substring(l.property, start, end)
        1. substring("abcde", 0, 2)->ab
    4. replace:替换字符串中的特定字符
  18. Aggregation聚合:用于对CQL查询结果执行一些聚合操作
    1. count:返回match结果行数
      1. match (l:LabelName) return count(l)
    2. max:返回match结果行的最大值
    3. min:返回match结果行的最小值
    4. sum:返回match结果行的和
    5. avg:返回match结果行的平均值
  19. Relationship关系:用于获取关系的细节,如startnodeendnode

    1. startnode:开始节点
    2. endnode:结束节点
    3. id:关系id
    4. type:关系类型(名称)
      1. match(a)-[r]->(b) return id(r),type(r)

        四、Neo4j-admin的使用

  20. 数据库备份:对数据进行备份,还原,迁移操作时要关闭neo4j

    1. cd %NEO4J_HOME%/bin
    2. # 关闭 neo4j
    3. neo4j stop
    4. # 备份
    5. neo4j-admin dump --datebase=databaseName.db --to=/path/databaseName_backup.dump
  21. 数据库恢复:

    1. # 数据导入
    2. neo4j-admin load --from=/path/databaseName_backup.dump --database=databaseName.db --force
    3. # 重启服务
    4. neo4j start