加载/导入 Apache Parquet
库要求
Apache Parquet 过程依赖于 APOC 扩展库中未包含的客户端库。
这些依赖项包含在 apoc-hadoop-dependencies-5.21.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.* 过程 结合使用。 |
配置参数
这些过程支持以下配置参数
名称 | 类型 | 默认值 | 描述 |
---|---|---|---|
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')
值 |
---|
{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')
文件 | 源 | 格式 | 节点 | 关系 | 属性 | 时间 | 行 | batchSize | 批次 | 数据 |
---|---|---|---|---|---|---|---|---|---|---|
"file:///import/testQuery.parquet" |
"file" |
"parquet" |
8 |
7 |
0 |
0 |
0 |
0 |
0 |
null |
以上过程也可以从 Parquet 字节数组中加载/导入,例如由 `CALL apoc.export.parquet.all.stream` 过程生成的字节数组。例如,以下过程将产生与上述过程相同的结果
// 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