Java实现greenplum数据库中csv文件的导入和导出

Java实现greenplum数据库中csv文件的导入和导出

背景:greenplum数据库是在开源的PostgreSQL的基础上采用MPP架构实现的关系型分布式数据库,在使用greenplum数据库进行开发的过程中,遇到了需要将本地csv文件导入到远程greenplum数据库中的需求场景,查看官方文档后,发现greenplum中提供了COPY语法在在一个文件和一个表之间复制数据,但是这个语句必须要求csv和greenplum处于同一个机器内部,不适用于本地csv和远程greenplum数据库的情况。

解决方法

使用org.postgresql.copy.CopyManager来实现将本地csv文件导入greenplum数据库的操作。代码实现入下,其中涉及到的gp连接需要替换,实现除了csv导入gp外,还提供了gp导出csv。

public class CsvToGpUtils {

    private String url; // gp的连接地址

    private String username; // gp用户名

    private String password; // gp密码

    /**
     * 获取connection
     */
    public Connection getConnection(){
        Connection connection = null;
        try {
            // 注册postgresql驱动
            Class.forName("org.postgresql.Driver").newInstance();
            connection = DriverManager.getConnection(url, username, password);
        } catch (InstantiationException | IllegalAccessException | ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }

    /**
     *
     * @param filePath  文件路径
     * @param table   数据库表名,数据存储的目标地址
     * @throws Exception
     */
    public void copyFromFile(String filePath, String table)
            throws Exception {
        FileInputStream fileInputStream = null;
        Connection conn = null;
        BufferedReader file = null;
        try {
            conn = getConnection();
            CopyManager copyManager = new CopyManager((BaseConnection) conn);
            fileInputStream = new FileInputStream(filePath);
            String charsetName = "UTF-8";
            System.out.println("charsetName = " + charsetName);
            // 获取字符流
            file = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), charsetName));
            // copy 语法,具体查看官方文档,STDIN:指定输入来自客户端应用
            String sql = "COPY " + table + "(column1, column2)  FROM STDIN  delimiter as',' csv header ";
            System.out.println("sql = " + sql);
            // 执行
            copyManager.copyIn(sql, file);
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(file!=null) {
                file.close();
            }
            if(conn != null) {
                conn.close();
            }
            System.out.println("执行完成");
        }
    }

    /**
     * 导出到文件
     * @param filePath
     * @param tableOrQuery 可以为tablename ,also (sql语句)
     * @return
     * @throws SQLException
     * @throws IOException
     */
    public long copyToFile(String filePath, String tableOrQuery)
            throws SQLException, IOException {

        FileOutputStream fileOutputStream = null;
        Connection connection = null;
        long count = 0;
        try {
            connection = getConnection();
            CopyManager copyManager = new CopyManager((BaseConnection)connection);
            fileOutputStream = new FileOutputStream(filePath);
            count = copyManager.copyOut("COPY (" + tableOrQuery + ") TO STDOUT  WITH DELIMITER ',' quote '\"' csv header", fileOutputStream);
        } finally {
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(connection != null) {
                connection.close();
            }
            System.out.println("执行完成");
        }
        return count;
    }

}
posted @ 2022-08-31 20:34  原生态咬死他  阅读(470)  评论(0)    收藏  举报