【Postgresql】PG文件导入导出数据库

Github

地址:https://github.com/ithuhui/hui-base-springboot/
分支:master

Note

Postgresql用的不习惯。这里记录一下Postgresql的通过文件导入到表,或者Postgresql表导出文件。

自己做了一点小改造,可以指定列名

这里有个注意,如果使用mybatis获取Connection,不能直接使用SqlSession.getConnection()

Code

maven

<!--PG SQL-->
<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>${postgresql.version}</version>
</dependency>

工具类

 /**
     * 用文件导入数据到PG.(可以指定列名)
     *
     * @param connection the connection
     * @param filePath   the file path
     * @param tableName  the table name
     * @author : Hu weihui
     * @since hui_project v1
     */
    public static void importFromFile(Connection connection, String filePath, String tableName, String columnInfo) {
        try (FileInputStream fileInputStream = new FileInputStream(filePath)) {
            CopyManager copyManager = new CopyManager((BaseConnection) connection.getMetaData().getConnection());
            String sql = "";
            if (StringUtils.isEmpty(columnInfo)) {
                sql = String.format("COPY %s FROM STDIN WITH CSV", tableName);
            } else {
                sql = String.format("COPY %s (%s) FROM STDIN WITH CSV", tableName, columnInfo);
            }
            copyManager.copyIn(sql, fileInputStream);
        } catch (FileNotFoundException e) {
            log.error("[PostgreImportDataUtil]-copyFromFile Can not to find :{}", filePath);
        } catch (IOException e) {
            log.error("[PostgreImportDataUtil]-copyFromFile fail to get FileInputStream");
        } catch (SQLException e) {
            log.error("[PostgreImportDataUtil]-copyFromFile BAD SQL,{}", e);
        }
    }

    /**
     * 导出到文件.(tableName 或者 querySQL)
     *
     * @param connection   the connection
     * @param filePath     the file path
     * @param tableOrQuery the table or query
     * @throws SQLException the sql exception
     * @throws IOException  the io exception
     * @author : Hu weihui
     * @since hui_project v1
     */
    public static void copyToFile(Connection connection, String filePath, String tableOrQuery) throws SQLException, IOException {
        try (FileOutputStream fileOutputStream = new FileOutputStream(filePath)) {
            String sql = "";
            if (tableOrQuery.trim().startsWith("select")){
                sql = String.format("COPY (%s) TO STDOUT ",tableOrQuery);
            }else {
                sql = String.format("COPY %s TO STDOUT ",tableOrQuery);
            }
            CopyManager copyManager = new CopyManager((BaseConnection) connection.getMetaData().getConnection());
            copyManager.copyOut(sql, fileOutputStream);
        }
    }

Mybatis使用该工具

@Autowired
private SqlSessionTemplate sqlSessionTemplate;

Connection connection = sqlSessionTemplate.getConfiguration().getEnvironment().getDataSource().getConnection();

PostgreImportDataUtil.importFromFile(connection,"D:/xxx/xxx",null);

Author

 作者:HuHui
 转载:欢迎一起讨论web和大数据问题,转载请注明作者和原文链接,感谢
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值