dgraph 基本查詢文法 三

來源:互聯網
上載者:User

標籤:func   query   UNC   like   can   put   stars   操作   where   

這部分主要是查詢塊、查詢變數、彙總操作

多名稱查詢

實際上就是類似多個查詢資料的拼接

格式:{  caro(func: allofterms([email protected], "Marc Caro")) {    [email protected]    director.film {      [email protected]    }  }  jeunet(func: allofterms([email protected], "Jean-Pierre Jeunet")) {    [email protected]    director.film {      [email protected]    }  }}
查詢變數

類似graphql 總的input 變數,但是查詢變數更方便,可以理解為sql 的預存程序,或者編程中的函數

var_name as some_block { ... }
查詢變數資料的引用

查詢變數的資料可以傳遞給子查詢block 使用,這點相比graphql 的方式有很大的方便

參考格式:{  coactors(func:allofterms([email protected], "Jane Campion")) @cascade {    JC_films as director.film { # JC_films = all Jane Campion‘s films      starting_movie: [email protected]      starring {        JC_actors as performance.actor { # JC_actors = all actors in all JC films          actor : [email protected]          actor.film {            performance.film @filter(not uid(JC_films)) {              film_together : [email protected]              starring {                # find a coactor who has been in some JC film                performance.actor @filter(uid(JC_actors)) {                  coactor_name: [email protected]                }              }            }          }        }      }    }  }}
指變數(min max)

使用min max 可以擷取變數的最大或者最小指

參考格式:{  q(func: allofterms([email protected], "Ang Lee")) {    director.film {      uid      [email protected]      # Count the number of starring edges for each film      num_actors as count(starring)      # In this block, num_actors is the value calculated for this film.      # The film with uid and name    }    # Here num_actors is a map of film uid to value for all    # of Ang Lee‘s films    #    # It can‘t be used directly, but aggregations like min and max    # work over all the values in the map    most_actors : max(val(num_actors))  }  # to use num_actors in another query, make sure it‘s done in a context  # where the film uid to value map makes sense.}
指變數(sum avg)

可以擷取變數的sum 以及avg

參考格式:{  ID as var(func: allofterms([email protected], "Steven Spielberg")) {    # count the actors and save to a variable    # average as ...  }  # average is a map from uid to value so it must be used in a context  # where the map makes sense. Because query block avs works over the UID  # of Steven Spielberg, the value variable has the value we expect.  avs(func: uid(ID)) @normalize {    name : [email protected]    # get the average    # also count the movies  }}
指變數 filter order

指變數可以應用filter以及order 操作

參考格式:{  ID as var(func: allofterms([email protected], "Steven")) {    director.film {      num_actors as count(starring)    }    average as avg(val(num_actors))  }  avs(func: uid(ID), orderdesc: val(average)) @filter(ge(val(average), 40)) @normalize {    name : [email protected]    average_actors : val(average)    num_films : count(director.film)  }}
指變數 math

dgraph 內建math 函數操作,可以進行一些常見的+ / - 以及sin 。。。。 操作

參考格式:{ var(func:allofterms([email protected], "Jean-Pierre Jeunet")) {  [email protected]  films as director.film {   stars as count(starring)   directors as count(~director.film)   ratio as math(stars / directors)  } } best_ratio(func: uid(films), orderdesc: val(ratio)){  [email protected]  stars_per_director : val(ratio)  num_stars : val(stars) }}
groupby 操作

類似sql 的groupby 操作,groupby 代碼塊內部操作只能應用到彙總函式上,同時count 只能
應用到uid 上,同時可以方便的通過變數應用到其他查詢中

參考格式:{  var(func:allofterms([email protected], "Steven Spielberg")) {    director.film @groupby(genre) {      a as count(uid)    }  }  byGenre(func: uid(a), orderdesc: val(a)) {    [email protected]    num_movies : val(a)  }}
參考資料

https://tour.dgraph.io/blocksvars/1/
https://github.com/rongfengliang/dgraph-docker-compose-deploy

    

dgraph 基本查詢文法 三

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.