使用杰森图管理API,它可以指定属性来接收列表值。这可以通过在创建属性键的时候指定基类型是列表。
Using the JanusGraph Management API it is possible to specify that a property can accept as list of
values. This can be done by specifying a cardinality of LIST when the property key is created.
除非我们明确这样做,无论何时创建的属性的基类型默认是单精度值SINGLE。下面的代码可以从连接了杰森图的小精灵控制台运行。名称是mylist的属性键被创建了来接收字符值。在键创建前,它的基类型指定为列表。总要记住:当您做了变更您要提交管理事务。
Unless we explicitly do this, whenever a property is created the cardinality will default to SINGLE.
The code below can be run from a Gremlin Console connected to a JanusGraph instance. A property
key called mylist is created that can accept String values'. Before the key is created, its cardinality is
specified as LIST. Always remember to commit the management transaction when you are done
making changes.
注意前边的步骤可以串联起来,如下所示。
Note that the previous steps could be chained together as shown below.
现在我们已创建了一个新的键,我们可以用管理API来验证它的基类型的确是列表。无论我们用API做了什么,我们都应当调用commit来关闭事务。
Now that we have created a new key, when can use the Management API to check that its
cardinality is indeed set to LIST. As always, whenever we are done using the API we should close
the transaction with a call to commit.
我们现在创建了一个新顶点使用新的mylist 属性增加了一些值。注意基类型是列表而不是集合,我们可以把重复的值与新的属性关联。
We can now create a new vertex and add some values using our new mylist property. Note that as
our cardinality is LIST and not SET that we can have duplicate values associated with our new
property.
6.3.7. 创建集合类型的属性 Creating a property with cardinality SET
使用杰森图管理API,我们也可以指定属性包含集合类型的值。基类型是集合与基类型是列表的差异是集合不允许重复的值。
Using the JanusGraph Management API we can also specify that a property can contain a SET of
values. The difference between a cardinality of SET and a cardinality of LIST is that sets do not
allow duplicate values.
让我们创建一个名为numbers的属性键,它将接收整数集合。
Let’s create a new property key called numbers that will accept a set of integer values.
和前边一样我们验证新属性的基类型是集合。
As before we can double check the cardinality of our new property.
和前边一样,我们做了变更就必须提交我们的管理事务。
Also as before once we are done making changes we need to commit our management transaction.
现在让我们创建一个新的顶点,做一些测试确保杰森图确实遵照了我们对于集合的规则。首先我们创建了一个 新的顶点,加入了一些值1,2,3进入到属性中。它的工作与预期是一致的。
Let’s now create a new vertex and do some testing to make sure that JanusGraph does enforce the
rules we expect from a set. First of all we create a new vertex and put the values 1,2 and 3 into the
property. This works as expected.
现在让我们再试着第二次加入值2,看看会发生什么。如您所见,我们的第二个数字2没有加到集合中,集合中只出现了一个数字2.
Now let’s try adding a second value of 2 and see what happens. As you can see, our second 2 was
not added to our set as there was already a 2 present.
我们再试着加了数字4,它生效了,因为在集合中还没有数字4存在。
Let’s try adding a 4 instead. This works as there is no existing value of 4 already in the set.
最后我们要提交我们的图事务,因为属性的创建我们已做完了。
Finally we can commit our graph transaction as we are all done creating properties.
浙公网安备 33010602011771号