Bulbflow - 如何将列表/字典保存为属性

4
我正在使用Bulbflow通过Python访问Neo4j
我现在正在尝试将Python列表保存为节点属性,但始终遇到错误。从文档中可以看出,在模型中定义时,列表是一种被接受的类型,但我想在定义模型后保存列表属性。
anode = g.vertices.get(123)
anode.specs = [(2, 0.27911702036756064), (5, 0.6708785014712791)]
anode.save()

但是我遇到了以下错误:
SystemError: (
   {'status': '200',
    'content-length': '142',
    'content-type': 'application/json; charset=UTF-8',
    'access-control-allow-origin': '*',
    'server': 'Jetty(6.1.25)'},
    '"java.lang.IllegalArgumentException:
        Unknown property type on: [[2, 0.27911702036756064], [5, 0.6708785014712791]],
        class java.util.ArrayList"')

我尝试使用convert_to_db函数,但不确定语法是什么。

有什么想法可以实现这个吗?问题在于我有一个元组列表吗?

谢谢!

============== 更新 ==============

按照Peter的建议,我尝试使用一个简单的平面列表,并遇到了同样的错误:

SystemError: (
    {'status': '200',
     'content-length': '172',
     'content-type': 'application/json; charset=UTF-8',
     'access-control-allow-origin': '*',
     'server': 'Jetty(6.1.25)'},
     '"java.lang.IllegalArgumentException:
         Unknown property type on: [0.0, 0.0, 0.0, 0.42659109777029425, 0.0, 0.0, 0.0, 0.0, 0.5234052770685714, 0.0],
         class java.util.ArrayList"')

任何想法?

这是一个嵌套的数组,也许你可以自己将其转换为简单的字符串、长整型或整型数组? - Peter Neubauer
@PeterNeubauer,很不幸我在使用简单的平面列表时遇到了相同的错误。 - zanbri
1个回答

4
Neo4j仅支持包含原始类型(如stringintbool等)的列表,不允许在列表中混合使用多种类型。
以下是Neo4j支持的属性类型:

http://docs.neo4j.org/chunked/preview/graphdb-neo4j-properties.html

要在Neo4j中存储混合类型的list,您可以将其保存为JSON文档字符串。

Bulbs具有Document属性类型,可为您执行dict<->json转换。

请参见...

如果您正在使用通用的 VertexEdge,在保存之前需要手动进行此转换:
specs = [(2, 0.27911702036756064), (5, 0.6708785014712791)]
anode = g.vertices.get(123)
anode.specs = g.client.type_system.database.to_document(specs)
anode.save()

然而,如果您使用的是Model,Bulbs将为您进行转换。只需使用Document属性定义您的模型,而不是List:

# people.py

from bulbs.model import Node, Relationship
from bulbs.property import String, DateTime, Document
from bulbs.utils import current_datetime

    class Person(Node):

        element_type = "person"

        name = String(nullable=False)
        specs = Document()

    class Knows(Relationship):

        label = "knows"

        timestamp = DateTime(default=current_datetime, nullable=False)

...然后你可以像这样使用你的模型...

>>> from people import Person, Knows
>>> from bulbs.neo4jserver import Graph

>>> g = Graph()
>>> g.add_proxy("people", Person)
>>> g.add_proxy("knows", Knows)

>>> specs = [(2, 0.27911702036756064), (5, 0.6708785014712791)]

# You can save specs when you create it...
>>> james = g.people.create(name="James", specs=specs)

# ...or save it after creation...
>>> julie = g.people.create(name="Julie")
>>> julie.specs = specs
>>> julie.save()

查看 http://bulbflow.com/docs/api/bulbs/model/


1
这对我帮助很大。你真的创造了很棒的软件!谢谢!! - zanbri

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接