简体   繁体   English

使用 Java 从另一台 postgres 服务器向 postgresql 服务器插入更多一百万行的有效方法是什么?

[英]What's the effective way to insert more a million rows into postgresql server from another postgres server using Java?

I have two postgresql servers and I need to copy table rows with from first server format and convert to another server format (different column names).我有两个 postgresql 服务器,我需要从第一个服务器格式复制表行并转换为另一种服务器格式(不同的列名)。

I use java application with spring boot and jpa repository, which implemented method findAll with stream read fetch size 1000.我使用 java 应用程序和 spring 引导和 jpa 存储库,它实现了方法 findAll stream 读取提取大小 1000。

    @Query("select c from ExternalFormatEntity c")
    @QueryHints(@javax.persistence.QueryHint(name = "org.hibernate.fetchSize",
            value = Constants.DEFAULT_FETCH_SIZE))
    Stream<ExternalFormatEntity> findAllEntities();

After reading I convert and insert 1000 rows in batch.阅读后我批量转换并插入 1000 行。

try (Stream<ExternalFormatEntity> allExtEntitiesStream = extFormatService.getAllEntities()) {
    LinkedList<CanonicalFormatEntity> canonicalEntityList = new LinkedList<>();
        allExtEntitiesStream.forEach(extEntity -> {
            if (Objects.nonNull(extEntity)) {
                canonicalEntityList.add(SomeConverter.convert(extEntity));
            }
            if (canonicalEntityList.size() >= DEFAULT_BATCH_SIZE) {
                List<CanonicalFormatEntity> copyList = new LinkedList<>(canonicalEntityList);
                canonicalEntityList.clear();
                Thread thread = new Thread(() -> {
                    canonicalEntityRepository.saveAll(copyList);
                    canonicalEntityRepository.flush();
                    copyList.clear();
                });
                thread.start();
            }
        });
}

For my opinion, current speed of this operation can be faster than 1 hour for 1 million records.在我看来,对于 100 万条记录,此操作的当前速度可以快于 1 小时。 Can I speed up this operation, if yes, how to do it?我可以加快这个操作吗,如果可以,该怎么做?

Foremost, I tried to convert table records from first database to CSV file, save it on another server and use Postgres Copy Api for downloading but the summary time is still unacceptable due to additional operations with the hard disk.首先,我尝试将第一个数据库的表记录转换为CSV文件,保存在另一台服务器上,使用Postgres Copy Api下载,但由于额外操作硬盘,汇总时间仍然无法接受。

Maybe postgres have stream writing or something else?也许 postgres 有 stream 写作或其他东西? I cant find answer in official postgresql docs.我无法在官方 postgresql 文档中找到答案。

For my case helped next solution:对于我的案例帮助下一个解决方案:

  1. export external table to csv file with zip compression (example from StackOverflow answer: https://stackoverflow.com/a/3981807/3744622 )使用 zip 压缩将外部表导出到 csv 文件(来自 StackOverflow 答案的示例: https://stackoverflow.com/a/3981807/3744622

  2. copy small zip file to postgres server in /tmp folder scp root@ext_server:/path/to/file root@target_server:/tmp/将小 zip 文件复制到 /tmp 文件夹中的 postgres 服务器scp root@ext_server:/path/to/file root@target_server:/tmp/

  3. import table from csv zipped file (example from StackOverflow answer: https://stackoverflow.com/a/46228247/3744622 )从 csv 压缩文件导入表(来自 StackOverflow 答案的示例: https://stackoverflow.com/a/46228247/3744622

I achieved summary time about 10 minutes.我实现了大概10分钟的总结时间。

Thank you all, this is wonderful place)谢谢大家,这是个好地方)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从服务器加载谷歌地图标记的最有效方法是什么 - What is the most effective way to load google maps markers from server 使用Java解析和检索Policy Server响应的有效方法 - Effective way to parse & retrieve Policy Server response using Java 使用存储过程从 sql 服务器快速读取百万条记录,并使用 java 和 Z2A2D595E6ED9A0B24DZ7F2B63B 将其写入 csv - Quickly read million records from sql server using stored procedure and write it to csv using java and spring boot 在Java集群环境中管理会话数据的有效方法是什么? - what's the effective way of managing session data in java clustering environment? 使用Java在Sql Server表中插入多行 - Insert multiple rows in Sql Server Tables using java 什么是从Java中的字符串读取字符的最佳和有效方法 - What is the best and effective way to read chars from string in java 从Java批量复制到SQL Server的最有效方法是什么? - What's the most efficient way to bulk-copy to SQL Server from Java? 在Java中设置Web服务器的最简单方法是什么? - What's the simplest way to setup a web server in java? 使用 JDBC 在 Postgres 中插入具有复杂数据类型的大量数据的有效方法是什么 - what is the effective way of inserting huge data with complex data types in Postgres using JDBC 使用GWT将数据对象从客户端传输到服务器以保持数据的方式是什么? - What's the way you transfer data object from client to server for persisting datas using GWT?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM