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

版权声明:小哥哥小姐姐们,本文为小博主原创文章,转载请附上博主博文网址,并标注作者谢谢~~。违者必究 https://blog.csdn.net/HuHui_/article/details/85110828

前言

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

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

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

装备

  1. maven

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

Core-Code

工具类

 /**
     * 用文件导入数据到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);

github

https://github.com/ithuhui/hui-base-springboot/tree/master/hui-base-springboot-server/src/main/java/com/hui/base/springboot/server/common/utils

作者

 作者:HuHui
 转载:欢迎一起讨论web和大数据问题,转载请注明作者和原文链接,感谢

猜你喜欢

转载自blog.csdn.net/HuHui_/article/details/85110828