简体   繁体   English

Neo4j Python界面py2neo和数据输入

[英]Neo4j python interface py2neo & data typing

I've got a curious issue with neo4j and python, it appears to be related to typing of numbers being put in by the python interface py2neo 我对neo4j和python有一个好奇的问题,它似乎与python接口py2neo输入的数字的输入有关

If I create a simple database using the cypher commands:- 如果我使用cypher命令创建一个简单的数据库:

create (n:Type {name:"foo1"});
create (n:Type {name:"foo2"});
match (n:Type {name:"foo1"}), (n2:Type {name:"foo2"})
 create (n)-[r:NUMBER {name: "flow1", value: 1000000000}]->(n2),
  (n)-[:NUMBER {name: "flow2", value: 1000000000}]->(n2),
  (n)-[:NUMBER {name: "flow3", value: 1000000000}]->(n2),
  (n)-[:NUMBER {name: "flow4", value: 1000000000}]->(n2),
  (n)-[:NUMBER {name: "flow5", value: 1000000000}]->(n2),
  (n)-[:NUMBER {name: "flow6", value: 1000000000}]->(n2),
  (n)-[:NUMBER {name: "flow7", value: 1000000000}]->(n2),
  (n)-[:NUMBER {name: "flow8", value: 1000000000}]->(n2),
  (n)-[:NUMBER {name: "flow9", value: 1000000000}]->(n2);

and run an aggregate query to sum the values of the relationships:- 并运行汇总查询以对关系的值求和:

match (n)-[r]->(n2) return n,sum(r.value),n2;

I get the expected result:- 我得到了预期的结果:

+--------------------------------------------------------------------+
| n                        | sum(r.value) | n2                       |
+--------------------------------------------------------------------+
| Node[20103]{name:"foo1"} | 9000000000   | Node[20104]{name:"foo2"} |
+--------------------------------------------------------------------+

However if I populate the same dataset using this python script:- 但是,如果我使用此python脚本填充相同的数据集:

#!/usr/bin/python

from py2neo import Graph, Path, Node, authenticate, Relationship

authenticate("localhost:7474", "neo4j", "password")

graph = Graph()


foo1 = Node('Type', name='foo1')
foo2 = Node('Type', name='foo2')

graph.create(foo1)
graph.create(foo2)

for i in range(1,10):
 r = Relationship.cast(foo1, 'NUMBER', foo2, { 'name': 'foo%d' % i,    'value': 1000000000 } )
 graph.create_unique(r)

And then run the same query I get the slightly surprising result:- 然后运行相同的查询,我得到稍微令人惊讶的结果:

neo4j-sh (?)$  match (n)-[r]->(n2) return n,sum(r.value),n2;
+--------------------------------------------------------------------+
| n                        | sum(r.value) | n2                       |
+--------------------------------------------------------------------+
| Node[20105]{name:"foo1"} | 410065408    | Node[20106]{name:"foo2"} |
+--------------------------------------------------------------------+

Which is consistent with sum() being constrained to 32bit. 这与sum()限制为32bit一致。

If any value is >32bit the sum is correct, but if all would fit within 32 bits the sum returns the wrong answer. 如果任何值> 32bit,则总和是正确的,但如果所有值都适合32位以内,则总和将返回错误的答案。

Any help appreciated. 任何帮助表示赞赏。

This is python 2.7.6 with neo4j 2.3.1 on ubuntu 14.04lts 这是ubuntu 14.04lts上带有neo4j 2.3.1的python 2.7.6

This works on your data added with py2neo: 这适用于您添加了py2neo的数据:

match (n)-[r]->(n2) return n,sum(toInt(r.value)),n2;

And if you cast the input to float in your Python code the original Cypher query works (without toInt() ): 而且,如果将输入toInt()为浮动在Python代码中,则原始Cypher查询有效(不使用toInt() ):

...
r = Relationship.cast(foo1, 'NUMBER', foo2, { 'name': 'foo%d' % i, 'value': float(1000000000) } )
...

I would assume that running the Cypher query from the console and adding nodes from neo4j create different data types? 我会假设从控制台运行Cypher查询并从neo4j添加节点会创建不同的数据类型吗?

This question explains what neo4j uses internally, integers are stored as JAVA long: Cypher creates number as a long. 这个问题解释了neo4j内部使用的是什么,整数存储为JAVA long: Cypher创建数字为long。 How do I create an integer? 如何创建整数?

If any value is >32bit the sum is correct, but if all would fit within 32 bits the sum returns the wrong answer. 如果任何值> 32bit,则总和是正确的,但如果所有值都适合32位以内,则总和将返回错误的答案。

I don't know much about JAVA data types, but shouldn't a long always be 64 bit? 我对JAVA数据类型了解不多,但是不应该总是64位吗? Or maybe the 1000000000 is stored as 32 bit and then the 'sum()' breaks if all values are 32 bit? 还是将1000000000存储为32位,然后如果所有值均为32位,则“ sum()”会中断吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM