sample抽样步骤允许您随机选择一个或多个顶点,在笔者的测试中,至少是使用廷克图时,它常会选择索引值小的顶点。例如,在这个测试中,笔都运行了这个查询1000次。
While the sample step allows you to select one or more vertices at random, in my testing, at least
when using a TinkerGraph, it tends to favor vertices with lower index values. So for example, in a
test I ran this query 1000 times.
笔者发现,它总是返回小于100的ID。这让笔者相信sample(1)调用就是与此相似。
What I found was that I always got back an ID of less than 200. This leads me to believe that the
sample(1) call is doing something similar to this
看下面的代码。甚至笔者运行简单的实验多次,它总是返回类似这样的结果。
Look at the code below. Even if I run that simple experiment many times it always gives results
similar to these.
在航线图中有超过3300个机场顶点,笔都想用一个查询选择图中的的任意一个机场。利用Java Math类, 我们可以这样做:从穿越尽可能多机场的机场中任意的选一个。看一下Groovy/Gremlin 代码快照如下。
Given the air routes graph has over 3,300 airport vertices I wanted to come up with a query that
gave a more likely result of picking any one airport from across all of the possible airports in the
graph. By taking advantage of the Java Math class we can do something that does seem to much
more randomly pick one airport from across all of the possible airports. Take a look at the snippets
of Groovy/Gremlin code below.
更多的例子用变量来存储值,其它的混合使用Gremlin和Groovy类和方法的更多的例子将会在“使得小精灵更Groovy化”和“遍历中使用变量”这两节中提供。
More examples of using variables to store values and other ways to use additional
Groovy classes and methods with Gremlin are provided in the "Making Gremlin
even Groovier" and "Using a variable to feed a traversal" sections.
简单的实验表明用Math.random方法生成的数字看起来更平均的分布地分布地分布在所有可能的机场中。
This simple experiment shows that the numbers being generated using the Math.random approach
appears to be a lot more evenly distributed across all of the possible airports.
注意:这种方法只在未修改的加载到廷克图的航线图中生效。这是因炮,我们知道,廷克图认可了用户提供的ID在航线图中, 机场ID从1开始,顺序增加,没有间隔。然而,您可以轻构的修改这个方法,从而可以用在已知索引是顺序的其它的图中。例如,您可以抽取所有的ID到列表中,然后从列表中随机选取一个。
Note that this approach only works unmodified with the air-routes graph loaded into a
TinkerGraph. This is because we know that the TinkerGraph implementation honors user provided
IDs and that in the air-routes graph, airport IDs begin at one and are sequential with no gaps.
However, you could easily modify this approach to work with other graphs without relying on
knowing that the index values are sequential. For example you could extract all of the IDs into a list
and then select one randomly from that list.
看起来不够随机这个状态只是在廷克图中,实际上它廷克图比其它图更遵照用户提供的ID值,而其它的图会在更随机的顺序存储顶点。当笔者在杰森图上运行这些查询语句时,sample抽样步骤会从图中得到更好和选择。
It is likely that this apparent lack of randomness is more specific to TinkerGraph and the fact that it
will respect user provided ID values whereas other graph systems will probably store vertices in a
more random order to begin with. Indeed when I ran these queries on JanusGraph the sample step
did yield a better selection of airports from across the graph.
如果机场ID并不总是知道是以顺序排序,一个接着一个,我们可以创建所有机场的ID列表,然后用像使用数学库random方法一样的技术进行处下,任意地从中抽取一个。
If the airport IDs were not all known to be in a sequential order one after the other, we could create
a list of all the airport IDs and then select one at random by doing something like this if we wanted
to use our Math.random technique.
浙公网安备 33010602011771号