Run Built-in Queries - GSQL 101

You now have a graph with data! You can run some queries using the built-in REST endpoint calls.

Get vertex/edge statistics

Here we call two functions, stat_vertex_number and stat_edge_number to return the number of vertices and edges of each vertex and edge type.

REST endpoints return results in JSON format. However, JSON data can’t be read easily from JSON files by using bash script like other normal files. The jq tool is used to solve this problem.

We recommend you install jq and redirect the REST call result to jq before it is output.

Linux Shell
// Get vertex cardinality
curl -X POST 'http://localhost:9000/builtins/Social' -d  '{"function":"stat_vertex_number","type":"*"}'  | jq .
// results
{
  "version": {
    "edition": "enterprise",
    "api": "v2",
    "schema": 0
  },
  "error": false,
  "message": "",
  "results": [
    {
      "v_type": "Person",
      "count": 7
    }
  ]
}
Linux Shell
# get edge cardinality
curl -X POST 'http://localhost:9000/builtins/Social' -d  '{"function":"stat_edge_number","type":"*"}' | jq .
{
  "version": {
    "edition": "enterprise",
    "api": "v2",
    "schema": 0
  },
  "error": false,
  "message": "",
  "results": [
    {
      "e_type": "Friendship",
      "count": 7
    }
  ]
}

Select vertices

If you want to look up the details about a vertex with its primary ID, you can use the following REST call.

Linux Shell
curl -X GET "http://localhost:9000/graph/{graph_name}/vertices/{vertex_type}/{vertex_id}"

Example. Find a Person vertex whose primary_id is "Tom".

Linux Shell
 curl -X GET "http://localhost:9000/graph/Social/vertices/Person/Tom" | jq .
 {
  "version": {
    "edition": "enterprise",
    "api": "v2",
    "schema": 0
  },
  "error": false,
  "message": "",
  "results": [
    {
      "v_id": "Tom",
      "v_type": "Person",
      "attributes": {
        "name": "Tom",
        "age": 40,
        "gender": "male",
        "state": "ca"
      }
    }
  ]
}

Select edges

In similar fashion, we can see details about edges. To describe an edge, you name the types of vertices and edges in the two parts or three parts of a URL.

Linux Shell
#two parts
curl -X GET "http://localhost:9000/graph/edges/{source_vertex_type}/{source_vertex_id}/{edge_type}/"

#three parts
curl -X GET "http://localhost:9000/graph/edges/{source_vertex_type}/{source_vertex_id}/{edge_type}/{target_vertex_type}/{target_vertex_id}"

Example. Find all friendship edges whose source vertex’s primary_id is "Tom".

Linux Shell
curl -X GET "http://localhost:9000/graph/Social/edges/Person/Tom/Friendship/" | jq .
// results
{
    "version": {
        "edition": "enterprise",
        "api": "v2",
        "schema": 0
    },
    "error": false,
    "message": "",
    "results": [
    {
        "e_type": "Friendship",
        "directed": false,
        "from_id": "Tom",
        "from_type": "Person",
        "to_id": "Dan",
        "to_type": "Person",
        "attributes": {
        "connect_day": "2017-06-03 00:00:00"
        }
    },
    {
        "e_type": "Friendship",
        "directed": false,
        "from_id": "Tom",
        "from_type": "Person",
        "to_id": "Jenny",
        "to_type": "Person",
        "attributes": {
        "connect_day": "2015-01-01 00:00:00"
        }
    }
    ]
}

For more built-in REST endpoints, visit the Built-in Endpoints page.