Skip to main content
  1. Refs/

Dgraph

··3 mins

Dgraph Tour #

Dgraph’s Query Language (DQL) Tour

Start Dgraph #

Ref: Dgraph DQL Tour - Intro - Run Dgraph, Dgraph DQL Tour - More Data - A bigger dataset

Save the below aliases in your .bashrc/.zshrc file, then run in two separate terminals rdg0 and rdga

Launch Dgraph Zero

alias rdg0="docker run --rm -it \
  -p 5080:5080 -p 6080:6080 -p 8080:8080 \
  -p 9080:9080 -p 8000:8000 \
  -v $PROG/data/dgraph:/dgraph \
  --name dgraph \
  dgraph/dgraph:v21.03.0 dgraph zero"

Launch Dgraph Alpha with increased cache size

alias rdga="docker exec -it dgraph dgraph alpha \
  --cache size-mb=4096 --zero localhost:5080 --security whitelist=0.0.0.0/0"

Load 1 million movie dataset

curl -L -o 1million.rdf.gz "https://github.com/dgraph-io/tutorial/blob/master/resources/1million.rdf.gz?raw=true"
docker exec -it dgraph dgraph live -f 1million.rdf.gz --alpha localhost:9080 --zero localhost:5080 -c 1

Reference Queries #

  1. Dgraph DQL Tour - More Data - Movies Schema Query to find movies directed by Kathryn Bigelow, including actors in those films
{
  kathryn(func: eq(name@., "Kathryn Bigelow")) { # find Kathryn
    uid
    name@.
    director.film (first: 1) { # get first movie they directed
      name@.
      initial_release_date
      genre { name@. }
      starring { # include all starring / Performances directly
        uid
        performance.film { name@. }
        performance.actor { name@. }
        performance.character { name@. }
      }
    }
  }
}

OR

{
  kathryn(func: eq(name@., "Kathryn Bigelow")) { # find Kathryn
    uid
    name@.
    director.film (first: 1) { # get first movie they directed
      name@.
      initial_release_date
      genre { name@. }
      starring {  # include all starring / Performances with expand function
        expand(Performance) {
            uid
            expand(_all_)
        }
      }
    }
  }
}
  1. Dgraph DQL Tour - Var Blocks - Exercise : Latest Movies Query to find the most recently released movie by each director ordered by release dates
{
  var(func: has(director.film)) @cascade { # variable block; find all nodes with a director.film predicate; cascade filters out nodes without specified predicates
    director.film {
     release as initial_release_date # save release date of each film as variable
    }
    latest_release as max(val(release)) # finds biggest release date and saves as new variable
    daysSince as math(since(latest_release)/(24*60*60)) # calculates number of days to/from release date and saves as variable
  }
  latest_movies(func: uid(latest_release), orderdesc: val(latest_release)) @normalize { # finds all person nodes from previous var block; orders by release date; filters results to only include aliases
    dir: name@. # name of director
    days: val(daysSince) # days to/from release date
    director.film(first: 1, orderdesc: initial_release_date) { # returns latest movie
      film: name@. # film name
      release: initial_release_date # release date
    }
  }
}

Math functions #

Ref: Dgraph DQL Tour - Value variables: math functions

OPERATOR ACCEPTED TYPE NOTES
+ - * / % int and float
min max All types except geo and bool
< > <= >= == != All, except geo and bool boolean result
floor ceil ln exp sqrt int and float
since date number of seconds (float) from the time specified
pow(a, b) int and float a^b
logbase(a,b) int and float log(a) to the base b
cond(a, b, c) a must be a boolean selects b if a is true else c