

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# Gremlin コンソールに接続する別の方法
<a name="access-graph-gremlin-console-connect"></a>

**通常の接続方法の欠点**

Gremlin コンソールに接続する最も一般的な方法は上で説明したものであり、`gremlin>` プロンプトで次のようなコマンドを使用します。

```
gremlin> :remote connect tinkerpop.server conf/{{(file name)}}.yaml
gremlin> :remote console
```

これはうまく機能し、Neptune にクエリを送信できます。ただし、Groovy スクリプトエンジンがループから外れるため、Neptune はすべてのクエリを純粋な Gremlin として扱います。つまり、以下のクエリフォームは失敗します。

```
gremlin> 1 + 1
gremlin> x = g.V().count()
```

このように接続したときに変数を使うのに一番近いのは、コンソールに保持されている `result` 変数を使用し、次のようにして `:>` を使用してクエリを送信することです。

```
gremlin> :remote console
==>All scripts will now be evaluated locally - type ':remote console' to return to remote mode for Gremlin Server - [krl-1-cluster.cluster-ro-cm9t6tfwbtsr.us-east-1.neptune.amazonaws.com/172.31.19.217:8182]
gremlin> :> g.V().count()
==>4249

gremlin> println(result)
[result{object=4249 class=java.lang.Long}]

gremlin> println(result['object'])
[4249]
```

 

**別の接続方法**

次のように、別の方法で Gremlin コンソールに接続することもできます。

```
gremlin> g = traversal().withRemote('conf/neptune.properties')
```

ここでは、`neptune.properties` は次の形式です。

```
gremlin.remote.remoteConnectionClass=org.apache.tinkerpop.gremlin.driver.remote.DriverRemoteConnection
gremlin.remote.driver.clusterFile=conf/my-cluster.yaml
gremlin.remote.driver.sourceName=g
```

`my-cluster.yaml` ファイル名は次のようになります。

```
hosts: [{{my-cluster-abcdefghijk.us-east-1.neptune.amazonaws.com}}]
port: 8182
serializer: { className: org.apache.tinkerpop.gremlin.util.ser.GraphBinaryMessageSerializerV1,
              config: { serializeResultToString: false } }
connectionPool: { enableSsl: true }
```

**注記**  
 バージョン 3.7.0 では、シリアライザーが `gremlin-driver` モジュールから新しい `gremlin-util` モジュールに移動されました。パッケージが org.apache.tinkerpop.gremlin.driver.ser から org.apache.tinkerpop.gremlin.util.ser に変更されました。

Gremlin コンソール接続をこのように設定すると、以下の種類のクエリを正常に実行できます。

```
gremlin> 1+1
==>2

gremlin> x=g.V().count().next()
==>4249

gremlin> println("The answer was ${x}")
The answer was 4249
```

次のように、結果を表示せずに済みます。

```
gremlin> x=g.V().count().next();[]
gremlin> println(x)
4249
```

通常のクエリ方法 (ターミナルステップなし) はすべて引き続き機能します。例えば、次のようになります。

```
gremlin> g.V().count()
==>4249
```

[https://tinkerpop.apache.org/docs/current/reference/#io-step](https://tinkerpop.apache.org/docs/current/reference/#io-step) ステップを使用して、このような接続でファイルを読み込むこともできます。