加载 / 导入 Apache Parquet

库要求

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

这些依赖项包含在 apoc-hadoop-dependencies-5.26.1-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. 配置参数
名称 类型 默认值 描述

batchSize

long

20000

事务批量大小

mapping

Map

20000

用于映射复杂文件。参见下面的“映射配置”部分

使用方法

假设有以下示例图

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. 结果
文件 格式 节点数 关系数 属性数 时间 行数 batchSize 批次数 数据

"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、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

  • Double

  • Float

  • Short

  • Int

  • Long

  • Node

  • Relationship

  • BaseType 后跟 Array,用于映射值列表,其中 BaseType 可以是前面的类型之一,例如 DurationArray

© . All rights reserved.