組み込みのクエリの実行

データが読み込まれたグラフが完成しました。次にビルトインのRESTエンドポイントコールを使って、クエリを実行してみましょう。

頂点とエッジに関する統計データを取得する

ここでは、次の2つの関数、stat_vertex_numberとstat_edge_numberを呼び出して、各頂点タイプと各エッジタイプのカーディナリティを調べます。

RESTエンドポイントの結果は、JSON形式で応答されます。JSONデータはさまざまな用途に使われますが、ほかの普通のファイルのようにbashスクリプトを使ってJSONファイルから簡単に読むことはできません。代わりにjqツールを使ってください。

jqをインストールして、RESTコールの結果が出力される前に、jqに転送することをお勧めします。

Linuxシェル
#頂点のカーディナリティを取得する
curl -X POST 'http://localhost:9000/builtins/social' -d  '{"function":"stat_vertex_number","type":"*"}'  | jq .
#結果
{
  "version": {
    "edition": "enterprise",
    "api": "v2",
    "schema": 0
  },
  "error": false,
  "message": "",
  "results": [
    {
      "v_type": "person",
      "count": 7
    }
  ]
}
Linuxシェル
#エッジのカーディナリティを取得する
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
    }
  ]
}

頂点を選択する

任意の頂点のプライマリIDの詳細を調べたいときには、次のRESTコールを使います。

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

プライマリIDが「Tom」というpersonの頂点を検索する

Linuxシェル
 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"
      }
    }
  ]
}

エッジを選択する

上の例と同様に、エッジの詳細を表示できます。エッジを特定するには、URLの2つまたは3つの部分で、頂点タイプとエッジタイプの名前を指定します。

Linuxシェル
#2つの部分を使う場合
curl -X GET "http://localhost:9000/graph/edges/{source_vertex_type}/{source_vertex_id}/{edge_type}/"

#3つの部分を使う場合
curl -X GET "http://localhost:9000/graph/edges/{source_vertex_type}/{source_vertex_id}/{edge_type}/{target_vertex_type}/{target_vertex_id}"

ソース頂点のprimary_idが「Tom」であるfriendshipのエッジを検索する

Linuxシェル
curl -X GET "http://localhost:9000/graph/social/edges/person/Tom/friendship/" | jq .
#結果
{
  "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"
      }
    }
  ]
}

この他のビルトインのRESTエンドポイントは、ビルトインエンドポイントのページに記載されています。