

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 連線至 Gremlin 主控台的替代方式
<a name="access-graph-gremlin-console-connect"></a>

**正常連線方法的缺點**

連線到 Gemlin 主控台的最常用方法是上面說明的方法，在 `gremlin>` 提示處使用如下的命令：

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

其效果很好，並且可讓您將查詢傳送至 Neptune。不過，它需要將 Groovy 指令碼引擎帶出迴圈，因此 Neptune 會將所有查詢視為純粹的 Grimlin。這表示下列查詢形式會失敗：

```
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]
```

 

**不同的連線方式**

您也可以透過不同的方式連線到 Gemlin 主控台，從而可能發現更好的方式，如下所示：

```
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 }
```

**注意**  
 序列化程式已從`gremlin-driver`模組移至 3.7.0 版的新`gremlin-util`模組。套件從 org.apache.tinkerpop.gremlin.driver.ser 變更為 org.apache.tinkerpop.gremlin.util.ser。

像這樣設定 Gimlin 主控台連線可讓您成功地進行以下類型的查詢：

```
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) 步驟，搭配這種類型的連線載入檔案。