apoc.convert.fromYaml

函数 Apoc 扩展

apoc.convert.fromYaml(value, $config) - 将 YAML 字符串反序列化为 Neo4j 值

签名

apoc.convert.fromYaml(value :: STRING, config = {} :: MAP) :: ANY

输入参数

名称 类型 默认

STRING

null

config

MAP

{}

配置参数

该过程支持以下配置参数

表 1. 配置参数
名称 (name) type 默认 description(描述)

disable

字符串列表

空列表

用于禁用底层所用库中默认启用的一个或多个配置。请参阅此处

enable

字符串列表

空列表

用于启用底层所用库的一个或多个配置。请参阅此处

mapping(映射)

字符串映射

空映射

用于映射复杂的 YAML。

为了读取 FasterXML Jackson 不支持的复杂类型(如 Long 等),我们可以使用 mapping 配置将其转换为所需的数据类型。例如,如果我们有一个 YAML a: 42 b: foo,我们可以定义一个映射 {mapping: {a: "Long"} }

使用示例

我们可以将任何 YAML 字符串转换为节点/关系的列表或映射

将 YAML 字符串转换为映射
RETURN apoc.convert.fromYaml("a: 42
b: foo") AS value
表 2. 结果
{
  "b": "foo",
  "a": 42
}
使用自定义映射将 YAML 字符串转换为映射
RETURN apoc.convert.fromYaml("a: 42
b: foo", {mapping: {a: "Long"} }) AS value
表 3. 结果
{
  "b": "foo",
  "a": 42
}
使用自定义特性将 YAML 字符串转换为映射
RETURN apoc.convert.fromYaml("a: 42
b: foo", {enable: ['MINIMIZE_QUOTES'], disable: ['WRITE_DOC_START_MARKER']}) AS value
表 4. 结果
{
  "b": "foo",
  "a": 42
}
将 YAML 字符串转换为列表
RETURN apoc.convert.fromYaml("---
- 1
- 2
- 3") as value
表 5. 结果
[1, 2, 3]
将 YAML 字符串转换为列表并将类型映射为 Long
RETURN apoc.convert.fromYaml("---
- 1
- 2
- 3",
{mapping: {_: "Long"}}) as value
表 6. 结果
[1, 2, 3]
从 YAML 字符串转换为映射
RETURN apoc.convert.fromYaml("---
                            a: 42
                            b: \"foo\"
                            c:
                            - 1
                            - 2
                            - 3") as value
表 7. 结果
{
  "b": "foo",
  "c": [
    1,
    2,
    3
  ],
  "a": 42
}
从 YAML 字符串转换为节点
RETURN apoc.convert.fromYaml("---
id: \"<elementID>\"
type: \"node\"
labels:
- \"Test\"
properties:
foo: 7") as value
表 8. 结果
{
    "id": "<elementID>",
    "labels": [
        "Test"
    ],
    "properties": {
        "foo": 7
    },
    "type": "node"
}
从带有空值的 YAML 字符串转换为映射
RETURN apoc.convert.fromYaml("---
a: null
b: \"myString\"
c:
- 1
- \"2\"
- null") as value
表 9. 结果
{
  "b": "myString",
  "c": [
    1,
    "2",
    null
  ],
  "a": null
}
从 YAML 字符串转换为节点映射
RETURN apoc.convert.fromYaml("---
one:
    id: \"8d3a6b87-39ad-4482-9ce7-5684fe79fc57\"
    type: \"node\"
    labels:
    - \"Test\"
    properties:
    foo: 7
two:
    id: \"3fc16aeb-629f-4181-97d2-a25b22b28b75\"
    type: \"node\"
    labels:
    - \"Test\"
    properties:
    bar: 9
") as value
表 10. 结果
{
    "two": {
        "id": "3fc16aeb-629f-4181-97d2-a25b22b28b75",
        "labels": [
            "Test"
        ],
        "properties": null,
        "bar": 9,
        "type": "node"
    },
    "one": {
        "id": "8d3a6b87-39ad-4482-9ce7-5684fe79fc57",
        "labels": [
            "Test"
        ],
        "foo": 7,
        "properties": null,
        "type": "node"
    }
}
从 YAML 字符串转换为关系
RETURN apoc.convert.fromYaml("---
id: \"94996be1-7200-48c2-81e8-479f28bba84d\"
type: \"relationship\"
label: \"KNOWS\"
start:
    id: \"8d3a6b87-39ad-4482-9ce7-5684fe79fc57\"
    type: \"node\"
    labels:
    - \"User\"
    properties:
    name: \"Adam\"
end:
    id: \"3fc16aeb-629f-4181-97d2-a25b22b28b75\"
    type: \"node\"
    labels:
    - \"User\"
    properties:
        name: \"Jim\"
        age: 42
properties:
    bffSince: \"P5M1DT12H\"
    since: 1993.1
") as value
表 11. 结果
{
  "id": "94996be1-7200-48c2-81e8-479f28bba84d",
  "start": {
    "id": "8d3a6b87-39ad-4482-9ce7-5684fe79fc57",
    "name": "Adam",
    "labels": [
      "User"
    ],
    "properties": null,
    "type": "node"
  },
  "label": "KNOWS",
  "properties": {
    "bffSince": "P5M1DT12H",
    "since": 1993.1
  },
  "type": "relationship",
  "end": {
    "id": "3fc16aeb-629f-4181-97d2-a25b22b28b75",
    "labels": [
      "User"
    ],
    "properties": {
      "name": "Jim",
      "age": 42
    },
    "type": "node"
  }
}
© . This site is unofficial and not affiliated with Neo4j, Inc.