dgraph 基本查询语法 一

Posted rongfengliang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了dgraph 基本查询语法 一相关的知识,希望对你有一定的参考价值。

dgraph 的查询语法是在graphql 上的扩展,添加了新的支持,同时官方提供了一个
学习的网站 https://tour.dgraph.io/

基本环境(cluster 模式的)

参考 github 项目 https://github.com/rongfengliang/dgraph-docker-compose-deploy

git clone https://github.com/rongfengliang/dgraph-docker-compose-deploy
cd dgraph-docker-compose-deploy/one-zero
docker-compose up -d

基本使用

  • 加载schema
name: string @index(term) @lang .
age: int @index(int) .
friend: uid @count .
  • 加载数据
{
  set {
    _:michael <name> "Michael" .
    _:michael <age> "39" .
    _:michael <friend> _:amit .
    _:michael <friend> _:sarah .
    _:michael <friend> _:sang .
    _:michael <friend> _:catalina .
    _:michael <friend> _:artyom .
    _:michael <owns_pet> _:rammy .

    _:amit <name> "????"@hi .
    _:amit <name> "????"@bn .
    _:amit <name> "Amit"@en .
    _:amit <age> "35" .
    _:amit <friend> _:michael .
    _:amit <friend> _:sang .
    _:amit <friend> _:artyom .

    _:luke <name> "Luke"@en .
    _:luke <name> "?ukasz"@pl .
    _:luke <age> "77" .

    _:artyom <name> "Артём"@ru .
    _:artyom <name> "Artyom"@en .
    _:artyom <age> "35" .

    _:sarah <name> "Sarah" .
    _:sarah <age> "55" .

    _:sang <name> "??"@ko .
    _:sang <name> "Sang Hyun"@en .
    _:sang <age> "24" .
    _:sang <friend> _:amit .
    _:sang <friend> _:catalina .
    _:sang <friend> _:hyung .
    _:sang <owns_pet> _:goldie .

    _:hyung <name> "??"@ko .
    _:hyung <name> "Hyung Sin"@en .
    _:hyung <friend> _:sang .

    _:catalina <name> "Catalina" .
    _:catalina <age> "19" .
    _:catalina <friend> _:sang .
    _:catalina <owns_pet> _:perro .

    _:rammy <name> "Rammy the sheep" .

    _:goldie <name> "Goldie" .

    _:perro <name> "Perro" .
  }
}
  • 基本查询
{
  everyone(func: anyofterms(name, "Michael Amit")) {
    name
    friend {
      [email protected]:ko:en
      friend { expand(_all_) { expand(_all_) } }
    }
  }
}

技术分享图片

  • 数据类型查询
 schema(pred: [name, age, friend, owns_pet]) {
  type
  index
}

技术分享图片

  • 语言支持
<name>@<lan>
参考:
{
  language_support(func: allofterms([email protected], "????")) {
    [email protected]:hi:en
    age
    friend {
      [email protected]:ru
      age
    }
  }
}

技术分享图片

  • 函数&&过滤
内容查询
- allOfTerms(edge_name, "term1 ... termN")
- anyOfTerms(edge_name, "term1 ... termN") 
比较查询
- eq(edge_name, value):  
- ge(edge_name, value):  
- le(edge_name, value):  
- gt(edge_name, value): 
- lt(edge_name, value):  
  • AND OR NOT 查询
参考:
{
  michael_friends_and(func: allofterms(name, "Michael")) {
    name
    age
    friend @filter(ge(age, 27) AND le(age, 48)) {
      [email protected]
      age
    }
  }
}
  • 排序
orderasc or orderdesc
{
  michael_friends_sorted(func: allofterms(name, "Michael")) {
    name
    age
    friend (orderasc: age) {
      [email protected]
      age
    }
  }
}

技术分享图片

  • 分页
first: N  
offset: N  
after: uid  
参考:
{
  michael_friends_first(func: allofterms(name, "Michael")) {
    name
    age
    friend (orderasc: [email protected], offset: 1, first: 2) {
      [email protected]
    }
  }
}

技术分享图片

  • count
{
  michael_number_friends(func: allofterms(name, "Michael")) {
    name
    age
    count(friend)
  }
}

技术分享图片

  • has
{
  have_friends(func: has(friend)) {
    [email protected]
    age
    number_of_friends : count(friend)
  }
}

技术分享图片

  • 别名(和graphql 一致)
{
  michael_number_friends(func: allofterms(name, "Michael")) {
    persons_name : name
    age
    number_of_friends : count(friend)
  }
}

技术分享图片

参考资料

https://tour.dgraph.io/

以上是关于dgraph 基本查询语法 一的主要内容,如果未能解决你的问题,请参考以下文章

dgraph实现基本操作

dgraph cluster docker-compose 安装

Dgraph 图相交

译丨Yarn - Javascript 新一代套件管理

dgraph 使用简介

Dgraph安装以及使用1