gremlin - 使用 writeGraph() 将 Tinkerpop 图导出到不同的 OutputStream
问题描述
以下语句将在图形服务器上创建一个新文件:
graph.io(IoCore.graphml()).writer().normalize(true).create().writeGraph(new FileOutputStream("export.graphml"), graph)
我想使用不同的OutputStream直接在我的 gremlin 客户端中查看输出。我试过 DataOutputStream() 但得到了 NullPointerException。我将如何从 writeGraph() 获得响应?
解决方案
我最初读这个问题是想知道如何创建一个远程OutputStream
从服务器写回客户端上的某个本地文件,这可能有一些解决方案,但我不确定答案是什么。当我碰巧再次查看这个问题时,尽管赏金指出您对“将完整的 tinkerpop 图导出到控制台”感兴趣,在这种情况下,可能会采用不同的方法。
我会简单地GraphMLWriter
使用它直接构造 a Builder
,然后写入 abyte[]
并将其作为 a 返回String
:
baos = new ByteArrayOutputStream()
graph.io(IoCore.graphml()).writer().normalize(true).create().writeGraph(baos, graph)
baos.toByteArray()
这是完整的控制台会话,尽管我省略了一些 graphml 以帮助提高可读性:
gremlin> :remote connect tinkerpop.server conf/remote-objects.yaml session
==>Configured localhost/127.0.0.1:8182-[df3107c1-b25b-4f1c-a1f3-552353e9023d]
gremlin> :remote console
==>All scripts will now be sent to Gremlin Server - [localhost/127.0.0.1:8182]-[df3107c1-b25b-4f1c-a1f3-552353e9023d] - type ':remote console' to return to local mode
gremlin> baos = new ByteArrayOutputStream();[]
gremlin> graph.io(IoCore.graphml()).writer().normalize(true).create().writeGraph(baos, graph);[]
gremlin> new String(baos.toByteArray(), java.nio.charset.StandardCharsets.UTF_8)
==><?xml version="1.0" ?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.1/graphml.xsd">
<key id="age" for="node" attr.name="age" attr.type="int"></key>
<key id="labelV" for="node" attr.name="labelV" attr.type="string"></key>
<key id="lang" for="node" attr.name="lang" attr.type="string"></key>
<key id="name" for="node" attr.name="name" attr.type="string"></key>
<key id="labelE" for="edge" attr.name="labelE" attr.type="string"></key>
<key id="weight" for="edge" attr.name="weight" attr.type="double"></key>
<graph id="G" edgedefault="directed">
<node id="1">
<data key="labelV">person</data>
<data key="age">29</data>
<data key="name">marko</data>
</node>
...
</graphml>
gremlin> :remote console
==>All scripts will now be evaluated locally - type ':remote console' to return to remote mode for Gremlin Server - [localhost/127.0.0.1:8182]-[df3107c1-b25b-4f1c-a1f3-552353e9023d]
gremlin> result
==>result{object=<?xml version="1.0" ?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.1/graphml.xsd">
<key id="age" for="node" attr.name="age" attr.type="int"></key>
<key id="labelV" for="node" attr.name="labelV" attr.type="string"></key>
<key id="lang" for="node" attr.name="lang" attr.type="string"></key>
<key id="name" for="node" attr.name="name" attr.type="string"></key>
<key id="labelE" for="edge" attr.name="labelE" attr.type="string"></key>
<key id="weight" for="edge" attr.name="weight" attr.type="double"></key>
<graph id="G" edgedefault="directed">
<node id="1">
<data key="labelV">person</data>
<data key="age">29</data>
<data key="name">marko</data>
</node>
...
</graphml> class=java.lang.String}
gremlin> result.get(0).getString()
==><?xml version="1.0" ?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.1/graphml.xsd">
<key id="age" for="node" attr.name="age" attr.type="int"></key>
<key id="labelV" for="node" attr.name="labelV" attr.type="string"></key>
<key id="lang" for="node" attr.name="lang" attr.type="string"></key>
<key id="name" for="node" attr.name="name" attr.type="string"></key>
<key id="labelE" for="edge" attr.name="labelE" attr.type="string"></key>
<key id="weight" for="edge" attr.name="weight" attr.type="double"></key>
<graph id="G" edgedefault="directed">
<node id="1">
<data key="labelV">person</data>
<data key="age">29</data>
<data key="name">marko</data>
</node>
...
</graph>
</graphml>
gremlin>
我的 Gremlin 控制台示例将脚本发送到服务器以在会话中执行(因此,这种方法也可以作为从驱动程序发送的脚本,假设它作为一个脚本发送,或者如果使用会话,每一行都可以单独发送)。请注意,从服务器返回的每一行的结果都存储在result
控制台的一个变量中。为了使其正常工作,重要的是您连接到一个配置,例如默认remote-objects.yaml
返回对象的配置,而不仅仅是字符串表示,尽管在这种情况下,由于最终对象是 aString
它可能并不重要。我想这是否取决于你打算对结果本身做些什么。
需要考虑的一些注意事项:
- 对于大型图,此操作将相当昂贵,因为它不仅是完整的图扫描,而且您还将整个图写入内存,然后再将其流回。
- 不建议使用
Graph
API(即graph.io()
)和/或直接使用GraphWriter
实现,所以我想这些 API 可能会在未来的版本中消失,但目前没有更好的解决方案,我没有太多选择我害怕。
推荐阅读
- sql - sql表触发器
- java - JavaFX:在多个 fxml 场景中使用单个变量
- c# - 当我尝试将 SaveChanges() 保存到上下文时,EF 中的“更新条目时发生错误”
- c++ - 如何让 if() 由 && 而不是 || 分割
- java - spring boot 跟踪更新值
- go - nano 上的 golang 格式搞砸了,而在 vscode 上还可以。nano 格式被推送到 git
- google-maps - 向数据层添加几何图形 - 如何在添加过程中设置特征样式?
- php - 在 localhost laravel 上安装新的 Laravel 源
- sql - ActiveRecord 多对多我应该制作另一个表来保存关系信息吗?
- sql - 在 postgres 中合并查询