简体   繁体   English

Py2Neo 没有正确创建日期时间数据类型?

[英]Py2Neo not creating Datetime data type correctly?

I trying build out some functions to populate my new Neo4j graph and I am having a hard time getting dates to populate as the correct data type in Neo4j using Py2Neo v4 and Neo4j 3.4.7.我尝试构建一些函数来填充我的新 Neo4j 图,但我很难使用 Py2Neo v4 和 Neo4j 3.4.7 将日期填充为 Neo4j 中的正确数据类型。 According to the Neo4j docummention there are Datetime data types... and spatial points which I want to get too as well根据 Neo4j 文档,有日期时间数据类型......以及我也想获得的空间点

I can not for the life of me find any documentation in Py2Neo for using either spatial points or temporal points.我一生都无法在 Py2Neo 中找到任何使用空间点或时间点的文档。 I found that there was back in v2 of Py2Neo a plugin for these data types but haven't found anything else on it.我发现在 Py2Neo 的 v2 中有一个用于这些数据类型的插件,但没有找到其他任何东西。

I can send a Python datetime.datetime object to Neo4j as a node property but when I try to query with Cypher it doesn't acknowledge it as being in the correct format.我可以将 Python datetime.datetime对象作为节点属性发送到 Neo4j,但是当我尝试使用 Cypher 进行查询时,它不会确认它的格式正确。

# python/py2neo code being used to make the node
example_node = Node("Example", date=datetime.datetime.now())
tx.create(example_node)
# cypher query
MATCH (e:Example)
WHERE e.date > datetime("2018-12-31")
RETURN e

Note: if I cast e.date to datetime like this datetime(e.date) I get the syntax error:注意:如果我像这样datetime(e.date)那样将e.datedatetime我会收到语法错误:

Neo.ClientError.Statement.SyntaxError: Text cannot be parsed to a DateTime
"2019-01-14 13:00:52"

Any help in finding proper documentation in Py2neo, or maybe even a better driver to use would be appreciated.任何在 Py2neo 中找到正确文档的帮助,或者甚至更好的驱动程序都将不胜感激。

Thank you谢谢

Turns out that py2neo uses the neotime module under the hood for returning/creating Neo4j date/time types.事实证明, py2neo使用neotime下的neotime模块来返回/创建 Neo4j 日期/时间类型。 (Link to documentation here ) I discovered this by converting an existing string field to a date type using Cypher and seeing what py2neo returned when I queried the graph. (链接到此处的文档)我通过使用 Cypher 将现有字符串字段转换为date类型并查看查询图形时py2neo返回的内容来发现这py2neo

from py2neo import Graph, Node
import neotime
import uuid

# Create graph object
graph = Graph()

# Create example node for a blog post
post = Node(
    'Post', 
    id=str(uuid.uuid4()), 
    title='Neo4j Date Post', 
    text='Here is some text',
    # Use neotime to create Neo4j date/time fields
    timestamp=neotime.DateTime.now(), 
    date=neotime.Date(2020, 5, 23)
)
graph.create(post)

Queries on your graph will return neotime.Date or neotime.DateTime objects, which thankfully have a to_native() method you can call, which converts them to datetime objects图表上的查询将返回neotime.Dateneotime.DateTime对象,幸好它们有一个可以调用的to_native()方法,它将它们转换为datetime对象

import neotime

print(neotime.Date(2020, 5, 24).to_native())
# datetime.date(2020, 5, 24)

print(neotime.DateTime.now().to_native())
# datetime.datetime(2020, 5, 24, 11, 43, 30, 373512)

With the current version of py2neo neotime does not work anymore Just use datetime and for timezone aware dates incl pytz使用当前版本的py2neo neotime 不再工作只需使用datetime和时区感知日期,包括pytz

from py2neo import Graph, Node
import time
import datetime
import pytz

g = Graph()
g.run("MATCH(n:DateNode) delete n")
n = Node(
    "DateNode",
    id=12345,
    time_time=time.time(),
    date='date("2019-06-04")',
    datetime_datetime=datetime.datetime(
        year=2019, month=12, day=23, hour=11, minute=49, second=30
    ),
    datetime_date=datetime.date.fromisoformat("2019-12-04"),
    datetime_fromtimestamp=datetime.date.fromtimestamp(time.time()),
    datetime_datetime_now=datetime.datetime.now(),
    datetime_datetime_strptime=datetime.datetime.strptime(
        "December 25, 2010", "%B %d, %Y"
    ),
    datetime_datetime_UTC=datetime.datetime(2011, 8, 15, 8, 15, 12, 0, pytz.UTC),
)
g.create(n)

print(
    g.run(
        "MATCH (n:DateNode{id:12345}) WITH n LIMIT 1 UNWIND keys(n) AS key RETURN key, apoc.meta.type(n[key]) as val"
    ).data()
)

Results in结果是

[
   {
      "key":"datetime_datetime_UTC",
      "val":"ZonedDateTime"
   },
   {
      "key":"datetime_datetime",
      "val":"LocalDateTime"
   },
   {
      "key":"datetime_datetime_now",
      "val":"LocalDateTime"
   },
   {
      "key":"datetime_datetime_strptime",
      "val":"LocalDateTime"
   },
   {
      "key":"date",
      "val":"STRING"
   },
   {
      "key":"id",
      "val":"INTEGER"
   },
   {
      "key":"time_time",
      "val":"FLOAT"
   },
   {
      "key":"datetime_fromtimestamp",
      "val":"LocalDate"
   },
   {
      "key":"datetime_date",
      "val":"LocalDate"
   }
]

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

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