

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 连接到 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) 步骤通过这种连接加载文件。