如何插入 780k 条记录 Spring Data Postgres
How to insert 780k records Spring Data Postgres
CSV 文件将上传到 Amazon S3 存储桶中。
当前实施:
- 使用 OpenCSV 实用程序的 readAll which returns List
读取 CSV 文件
- 将每个列值映射到一个实体object
- 创建列表object(当前大小:: 15000)
- 调用 Spring 数据 CRUD 存储库 saveAll(List objects)
- 在 780k 条记录中,只有 570k 条记录保存在数据库中。
- 插入这么多记录后,我们没有看到任何异常/错误。
完全不知道之后发生了什么
谷歌搜索后发现 PostgreSQL 中的 copy
命令是读取/写入 PostgreSQL 数据库的最快方式。
这也支持多种语言 Java、C、Python。
尝试使用 CopyManager
API 的 PostgreSQL,它有重载版本的 copyIn 方法。
copyIn(String sql, InputStream is)
- 我们不能使用它,因为我们的 csv 在第一行包含 Header 列,我们想跳过第一行。
copyIn(String sql, Reader rd)
- 这不接受 CSVReader object 因为它不是 java.io.Reader
类型
技术
- Java 8
- Spring-Boot2.0
- Spring-数据2.x
- Spring-JPA 2.x
- PostgreSQL - 9.6
We cannot use this since our csv contains Header Columns in the first line & we want to skip first line.
那么跳过第一行。 copyIn()
没有 "rewind" 它得到的 reader。您也不需要使用 CSVReader。 copy
语句将负责解析 CSV 文件。您需要做的就是提供位于应处理的第一行的 reader。
大致情况:
CopyManager mgr = new CopyManager(...);
BufferedReader in = new BufferedReader(new FileReader(...));
in.readLine(); // skip the header line
mgr.copyIn("copy target_table from stdin with (...)", in);
确保在 copy
命令中提供适当的选项,以便正确解析文件。
我能够找出代码缺陷。
我们已经提供了其中一个数据库列作为唯一约束,而在 CSV 文件中,此列有重复值。由于这个原因,记录插入失败。
谢谢,
法恩德拉
We cannot use this since our csv contains Header Columns in the first line & we want to skip first line.
这就是 COPY ... WITH HEADER
的目的。
Specifies that the file contains a header line with the names of each column in the file. On output, the first line contains the column names from the table, and on input, the first line is ignored. This option is allowed only when using CSV format.
CSV 文件将上传到 Amazon S3 存储桶中。
当前实施:
- 使用 OpenCSV 实用程序的 readAll which returns List 读取 CSV 文件
- 将每个列值映射到一个实体object
- 创建列表object(当前大小:: 15000)
- 调用 Spring 数据 CRUD 存储库 saveAll(List objects)
- 在 780k 条记录中,只有 570k 条记录保存在数据库中。
- 插入这么多记录后,我们没有看到任何异常/错误。
完全不知道之后发生了什么
谷歌搜索后发现 PostgreSQL 中的 copy
命令是读取/写入 PostgreSQL 数据库的最快方式。
这也支持多种语言 Java、C、Python。
尝试使用 CopyManager
API 的 PostgreSQL,它有重载版本的 copyIn 方法。
copyIn(String sql, InputStream is)
- 我们不能使用它,因为我们的 csv 在第一行包含 Header 列,我们想跳过第一行。
copyIn(String sql, Reader rd)
- 这不接受 CSVReader object 因为它不是 java.io.Reader
技术
- Java 8
- Spring-Boot2.0
- Spring-数据2.x
- Spring-JPA 2.x
- PostgreSQL - 9.6
We cannot use this since our csv contains Header Columns in the first line & we want to skip first line.
那么跳过第一行。 copyIn()
没有 "rewind" 它得到的 reader。您也不需要使用 CSVReader。 copy
语句将负责解析 CSV 文件。您需要做的就是提供位于应处理的第一行的 reader。
大致情况:
CopyManager mgr = new CopyManager(...);
BufferedReader in = new BufferedReader(new FileReader(...));
in.readLine(); // skip the header line
mgr.copyIn("copy target_table from stdin with (...)", in);
确保在 copy
命令中提供适当的选项,以便正确解析文件。
我能够找出代码缺陷。 我们已经提供了其中一个数据库列作为唯一约束,而在 CSV 文件中,此列有重复值。由于这个原因,记录插入失败。
谢谢, 法恩德拉
We cannot use this since our csv contains Header Columns in the first line & we want to skip first line.
这就是 COPY ... WITH HEADER
的目的。
Specifies that the file contains a header line with the names of each column in the file. On output, the first line contains the column names from the table, and on input, the first line is ignored. This option is allowed only when using CSV format.