使用TinkerPop3您可以把图保存成GraphML或者Graphson格式。GraphML是一个用于描述图的工工业标准的XML格式。它可以被用多的应用识别,便如Gephi。GraphSON做为TinkePop项目的一部分被定义,它在支持Tinkerpop的工具和图之外没有很广泛的被支持。然而GraphML被认为缺失了一些图,(它不能支持Tinkerpop3的一些数据类型和结构)。GraphSON没有这些缺陷
Using TinkerPop 3 you can save a graph either in GraphML or Graphson format. GraphML is an
industry standard XML format for describing a graph and is recognized by many other applications
such as Gephi. GraphSON was defined as part of the TinkerPop project and is less broadly
supported outside of TinkerPop enabled tools and graphs. However, whereas GraphML is
considered lossee for some graphs (it does not support all of the data types and structures used by
TinkerPop 3). GraphSON is not considered lossee.
可以使用下面的小精灵表达式把一个图保存到了GraphML文件中。您可以尝试保存您的一张图,并看一下它生成的输出。您可以看一下本书附带的air-routes.graphml 文件,如果您想要看一个好的GraphML文件的布局(适合人们阅读的)。要牢记,默认情况,在滑使用code美化之前,Tinkerpop会把您的图保存成一种人们不容易阅读的形式。现代的文本编辑器也可以美化XML文件。
Saving a graph to a GraphML file can be done using the following Gremlin expression. You might
want to try it on one of your graphs and look at the output generated. You can also take a look at the
air-routes.graphml file distribued with this book if you want to look at a well laid out (for human
readability) GraphML file. Bear in mind that by default, TinkerPop will save your graph in a way
that is not easily human readable without using a code beautifier first. Most modern text editors
can also beautify XML files well.
TinkerPop 3提供了两种不同的JSON打包选项。这不会与这不会与三个不同句法版本相混淆。默认的码选项是把图中每个顶点和每个边都做为一个单独的JSON文件。这将依次地处理图中的每个顶点。这本质上就是众所周知的邻接表形式。如果您用这种方式把一个图序列化到文件,之后您再看这些文件,您将会发现,每一行都是一个单独的JSON对象。
TinkerPop 3 offers two different JSON packaging options. These are not to be confused with the
three different syntax versions. The default encoding option stores each vertex in a graph and all of
its edges as a single JSON document. This is repeated for every vertex in the graph. This is
essentially what is known as adjacency list format. If you serialize a graph to file using this method
and look at the file afterwards you will see that each line (which could be very wide) is a standalone
JSON obect.
第二个选项是压缩的领接表形式,在这种方式中,所有的顶点和边都保存在了一个大的JSON对象中,放在了一个封闭的顶点对象内。
The second variant is referred to as a wrapped adjacency list format. In this flavor all of the vertices
and edges are stored in a single large JSON oject inside of an enclosing vertices object.生成的GraphSON文件不是适合人们阅读的,如果没有用类似Python的json工具(
python -m json.tool
my-graph.json
)
等做一些美化打印的处理,或者使用可以美化JSON的用文本编辑器。
The GraphSON file generated will not be very human readable without doing some
pretty printing on it using something like the Python json tool (python -m json.tool
my-graph.json) or using a text editor that can beautify JSON.
下面的小精灵代码行会创建一个文件,它包含了邻接表形式的GraphSON.
The Gremlin line below will create a file containing the adjacency list form of GraphSON.
下面的代码会创建一个包含了压缩领接表形式的GraphSON
The following will create a file containing the wrapped adjacency list form of GraphSON.
如果您想接收大量的数据到支持Tinkerpop3的图存储中,比起GraphML或者封装的邻接表形式的GraphSON,未封装的,邻接表形式的GraphSON格式可能是一个更好的选项。使用这种格式您可每次把数据导入到图的一个顶点,而不用再尝试把整个图可能是很大的JSON文件一次性导入。
If you are ingesting large amounts of data into a TinkerPop 3 enabled graph, the
unwrapped flavor of GraphSON is probably a much better choice than GraphML
or wrapped GraphSON. Using this format you can stream data into a graph one
vertex at a time rather than having to try and send the entire graph as a
potentially huge JSON file all in one go.
注意默认情况您的图会使用GraphSON3.0格式保存。如果您想使用以前老的格式之一也是可以的,但是需要指出来您想让小精灵生成哪个版本的。下面的这个例子,图是用GraphSON 1.0格式保存的。这么做需要创建和使用一个mapper,它将生成我们需要的格式。
Note that by default your graph will be saved using the GraphSON 3.0 format. Should you wish to
use one of the older formats you can still do so but will need to explicitly specify which version you
want Gremlin to generate. In the example below the graph is saved using GraphSON 1.0 format.
Doing this requires the creation and use of a mapper that will produce the format we need.
如果您想学习更多关于GraphML和GraphSON格式的内容,您可以在接近本书最后的“常见的图序列化格式”这一节中看到更多的细节。
If you want to learn more about the specifics of the GraphML and GraphSON formats, they are
covered in detail near the end of this book in the "COMMON GRAPH SERIALIZATION FORMATS"
section.