Neo4j/ONgDB 图数据库快速处理 Excel 文件

Here’s the table of contents:

Neo4j/ONgDB 图数据库快速处理 Excel 文件

  使用过 Neo4j/ONgDB 图数据库的朋友应该对 CSV 文件的处理很熟悉,但是对于 Excel 文件的处理可能并不是很清楚,经常会编写一大堆 Excel 解析的代码去操作。在 3.x 版本的 APOC 组件中其实很早就开始支持对 Excel 的读取了,但是文档直到 Neo4j 4.x 版本以后才开始完善。

  在没有按照这篇文章介绍的方式操作安装相关依赖时,使用CALL dbms.procedures()在浏览器界面是查询不到apoc.load.xls过程的,因为后台安装失败了(debug.log中会有Failed to load...相关输出)。

  这篇文章主要介绍一下apoc.load.xls这个过程的使用方式。以下测试使用的图数据库版本为ongdb-enterprise-1.0.4,APOC版本为apoc-3.4.0.10-all

Neo4j apoc.load.xls 说明文档参考

参数说明

输入参数

名称 类型 默认值 说明
url STRING? null Excel文件地址,可以为本地或远程地址
selector STRING? null 指定sheet
config MAP? {} 配置参数

配置参数

名称 类型 默认值 说明 备注
skip boolean none skip result rows 跳过结果行
limit Long none limit result rows 限制结果行数
header booelan true indicates if file has a header Excel是否有头文件
sep String ‘,’ separator character or ‘TAB’ 分隔符或’TAB’
quoteChar String ‘"’ the char to use for quoted elements 用于引号元素的字符
arraySep String ‘;’ array separator 数组分隔符
ignore List<String> [] which columns to ignore 哪些列要忽略
nullValues List<String> [] which values to treat as null,
e.g. [‘na’,false]
将哪些值视为空值
mapping Map {} per field mapping, entry key is field name,
.e.g {mapping:{‘<sheet>’:{type:‘<type>’, dateFormat: ‘<format>’, dateParse: []}}}
对于字段映射的配置,输入键是字段名

mapping支持以下值:

  • <sheet> - name of the sheet 【表格名称】

  • <type> - type of the conversion requested (STRING, INTEGER, FLOAT, BOOLEAN, NULL, LIST, DATE, DATE_TIME, LOCAL_DATE, LOCAL_DATE_TIME, LOCAL_TIME, TIME)【请求转换的类型】

  • dateFormat: <format> - convert the Date into String (only String is allowed) 【将日期转换为字符串(只允许字符串)】

  • dateParse: [<formats>] - convert the String into Date (Array of strings are allowed) 【将字符串转换为日期(允许字符串数组)】

输出参数

名称 类型 说明
lineNo INTEGER? 行号
list LIST? OF ANY? 列表
map MAP? 映射表

XLS 文件读取

下载POI

  下载POI文件放置在图数据库安装目录的plugins文件夹,并重启图数据库。

https://mvnrepository.com/artifact/org.apache.poi/poi/4.1.2

使用远程地址加载

CALL apoc.load.xls("https://github.com/neo4j-contrib/neo4j-apoc-procedures/raw/5.0/extended/src/test/resources/load_test.xls",
  'Full',{ mapping: {
  Integer:{type:'int'},
  Array:{type:'int',array:true,arraySep:';'}
}});

本地文件加载

  本地文件放置在图数据库安装目录的import文件夹下,使用apoc.load.xls访问。

CALL apoc.load.xls("file:///load_test.xls",
  'Full',{ mapping: {
  Integer:{type:'int'},
  Array:{type:'int',array:true,arraySep:';'}
}});
CALL apoc.load.xls("load_test.xls",
  'Full',{ mapping: {
  Integer:{type:'int'},
  Array:{type:'int',array:true,arraySep:';'}
}});
CALL apoc.load.xls('load_test.xls','Kids');

XLSX 文件读取

下载依赖

  下载以下文件放置在图数据库安装目录的plugins文件夹,并重启图数据库。

https://mvnrepository.com/artifact/org.apache.poi/poi/4.1.2
https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml/4.1.2
https://mvnrepository.com/artifact/org.apache.xmlbeans/xmlbeans/3.1.0
https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-schemas/4.1.2

使用远程地址加载

CALL apoc.load.xls('https://github.com/neo4j-contrib/neo4j-apoc-procedures/raw/5.0/extended/src/test/resources/test_date.xlsx',
  'sheet',{ mapping:{
  Date:{type:'String'}
}});

本地文件加载

CALL apoc.load.xls('test_date.xlsx',
  'sheet',{ mapping:{
  Date:{type:'String'}
}})
CALL apoc.load.xls('test_date.xlsx',
  'sheet', { mapping: {
    Date:{type:'String',dateFormat:'iso_date'}
}});
CALL apoc.load.xls('test_date.xlsx',
  'sheet',{ mapping:{
  Date:{type:'String',dateParse:["wrongPath", "dd-MM-yyyy", "dd/MM/yyyy", "yyyy/MM/dd", "yyyy/dd/MM", "yyyy-dd-MM'T'hh:mm:ss"]}
}});

猜你喜欢

转载自blog.csdn.net/superman_xxx/article/details/130609632