加载 / 导入 Apache Parquet

库要求

Apache Parquet 过程依赖于一个客户端库,该库未包含在 APOC 扩展库中。

这些依赖项包含在 apoc-hadoop-dependencies-2025.10.0-all.jar 中,可从 发布页面 下载。

下载该文件后,应将其放入 plugins 目录并重启 Neo4j 服务器。

可用过程

下表描述了可用的过程:

名称 描述

apoc.load.parquet

从提供的 Parquet 文件或二进制数据加载 Parquet

apoc.import.parquet

从提供的 Parquet 文件或二进制数据导入 Parquet

与其他过程类似,apoc.load.parquet 仅检索 Parquet 结果,而 apoc.import.parquet 则在数据库中创建节点和关系。

这些过程旨在与 apoc.export.parquet.* 过程配合使用。

配置参数

该过程支持以下配置参数

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

batchSize

long

20000

事务批处理大小

mapping(映射)

Map

20000

用于映射复杂文件。请参阅下方的 Mapping config(映射配置)部分

使用

给定以下示例图

CREATE (TheMatrix:Movie {title:'The Matrix', released:1999, tagline:'Welcome to the Real World'})
CREATE (Keanu:Person {name:'Keanu Reeves', born:1964})
CREATE (Carrie:Person {name:'Carrie-Anne Moss', born:1967})
CREATE (Laurence:Person {name:'Laurence Fishburne', born:1961})
CREATE (Hugo:Person {name:'Hugo Weaving', born:1960})
CREATE (LillyW:Person {name:'Lilly Wachowski', born:1967})
CREATE (LanaW:Person {name:'Lana Wachowski', born:1965})
CREATE (JoelS:Person {name:'Joel Silver', born:1952})
CREATE
(Keanu)-[:ACTED_IN {roles:['Neo']}]->(TheMatrix),
(Carrie)-[:ACTED_IN {roles:['Trinity']}]->(TheMatrix),
(Laurence)-[:ACTED_IN {roles:['Morpheus']}]->(TheMatrix),
(Hugo)-[:ACTED_IN {roles:['Agent Smith']}]->(TheMatrix),
(LillyW)-[:DIRECTED]->(TheMatrix),
(LanaW)-[:DIRECTED]->(TheMatrix),
(JoelS)-[:PRODUCED]->(TheMatrix);

如果我们通过 CALL apoc.export.parquet.all('test.parquet') 过程创建了一个 test.parquet,我们可以通过以下方式加载结果

CALL apoc.load.parquet('test.parquet')
表 2. 结果

{id: 0, tagline: "Welcome to the Real World", title: "The Matrix", released: 1999, labels: ["Movie"]

{id: 1, born: 1964, name: "Keanu Reeves", labels: ["Person"]}

{id: 2, born: 1967, name: "Carrie-Anne Moss", labels: ["Person"]}

{id: 3, born: 1961, name: "Laurence Fishburne", labels: ["Person"]}

{id: 4, born: 1960, name: "Hugo Weaving", labels: ["Person"]}

{id: 5, born: 1967, name: "Lilly Wachowski", labels: ["Person"]}

{id: 6, born: 1965, name: "Lana Wachowski", labels: ["Person"]}

{id: 7, born: 1952, name: "Joel Silver", labels: ["Person"]}

{type: "ACTED_IN", roles: ["Neo"], target_id: 0, __source_id: 1}

{type: "ACTED_IN", roles: ["Trinity"], target_id: 0, __source_id: 2}

{type: "ACTED_IN", roles: ["Morpheus"], target_id: 0, __source_id: 3}

{type: "ACTED_IN", roles: ["Agent Smith"], target_id: 0, __source_id: 4}

{type: "DIRECTED", target_id: 0, __source_id: 5}

{type: "DIRECTED", target_id: 0, __source_id: 6}

{type: "PRODUCED", target_id: 0, __source_id: 7}

或者,我们可以通过以下方式重新导入 test.parquet 节点/关系

CALL apoc.load.parquet('test.parquet')
表 3. 结果
file source format 节点 relationships 属性 time rows batchSize batches data

"file:///import/testQuery.parquet"

"file"

"parquet"

8

7

0

0

0

0

0

null

上述过程也可以加载/导入由例如 CALL apoc.export.parquet.all.stream 过程产生的 Parquet 字节数组。例如,以下过程将产生与上述过程相同的结果

加载过程
// create a byte array
call apoc.export.parquet.all.stream()
YIELD value with value as bytes
// load the byte array
call apoc.load.parquet(bytes)
YIELD value return value
导入过程
// create a byte array
CALL apoc.export.parquet.all.stream()
YIELD value with value as bytes
// import the byte array
CALL apoc.import.parquet(bytes)
YIELD source return source

映射配置

为了导入 Parquet 不支持的复杂类型(如 Point、Duration、List of Duration 等),我们可以使用映射配置将其转换为所需的数据类型。例如,如果我们有一个节点 (:MyLabel {durationProp: duration('P5M1.5D')},并且将其导出到 parquet 文件/二进制文件中,我们可以在导入时通过显式指定一个映射来转换它,其中键为属性键,值为属性类型。

也就是说,在此示例中,通过使用加载过程

CALL apoc.load.parquet(fileOrBinary, {mapping: {durationProp: 'Duration'}})

或者使用导入过程

CALL apoc.import.parquet(fileOrBinary, {mapping: {durationProp: 'Duration'}})

映射值类型可以是以下之一

  • Point

  • LocalDateTime

  • LocalTime

  • DateTime

  • Time

  • Date

  • Duration

  • Char(字符)

  • Byte(字节)

  • 双精度浮点数

  • 浮点数

  • Short(短整型)

  • Int(整型)

  • Long

  • 节点

  • 关系

  • BaseType 后跟 Array(数组),用于映射值列表,其中 BaseType 可以是上述类型之一,例如 DurationArray

© . This site is unofficial and not affiliated with Neo4j, Inc.