示例:通过本地文件导入导出数据

在使用Java语言基于openGauss进行二次开发时,可以使用CopyManager接口,通过流方式,将数据库中的数据导出到本地文件或者将本地文件导入数据库中,文件格式支持CSV、TEXT等格式。

样例程序如下,执行时需要加载openGauss JDBC驱动。

  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.io.IOException;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.sql.SQLException;
  7. import org.opengauss.copy.CopyManager;
  8. import org.opengauss.core.BaseConnection;
  9. public class Copy{
  10. public static void main(String[] args)
  11. {
  12. String urls = new String("jdbc:opengauss://10.180.155.74:8000/postgres"); //数据库URL
  13. String username = new String("jack"); //用户名
  14. String password = new String("Gauss@123"); //密码
  15. String tablename = new String("migration_table"); //定义表信息
  16. String tablename1 = new String("migration_table_1"); //定义表信息
  17. String driver = "org.opengauss.Driver";
  18. Connection conn = null;
  19. try {
  20. Class.forName(driver);
  21. conn = DriverManager.getConnection(urls, username, password);
  22. } catch (ClassNotFoundException e) {
  23. e.printStackTrace(System.out);
  24. } catch (SQLException e) {
  25. e.printStackTrace(System.out);
  26. }
  27. // 将SELECT * FROM migration_table查询结果导出到本地文件d:/data.txt
  28. try {
  29. copyToFile(conn, "d:/data.txt", "(SELECT * FROM migration_table)");
  30. } catch (SQLException e) {
  31. e.printStackTrace();
  32. } catch (IOException e) {
  33. e.printStackTrace();
  34. }
  35. //将d:/data.txt中的数据导入到migration_table_1中。
  36. try {
  37. copyFromFile(conn, "d:/data.txt", tablename1);
  38. } catch (SQLException e) {
  39. e.printStackTrace();
  40. } catch (IOException e) {
  41. e.printStackTrace();
  42. }
  43. // 将migration_table_1中的数据导出到本地文件d:/data1.txt
  44. try {
  45. copyToFile(conn, "d:/data1.txt", tablename1);
  46. } catch (SQLException e) {
  47. e.printStackTrace();
  48. } catch (IOException e) {
  49. e.printStackTrace();
  50. }
  51. }
  52. // 使用copyIn把数据从文件中导入数据库,
  53. public static void copyFromFile(Connection connection, String filePath, String tableName)
  54. throws SQLException, IOException {
  55. FileInputStream fileInputStream = null;
  56. try {
  57. CopyManager copyManager = new CopyManager((BaseConnection)connection);
  58. fileInputStream = new FileInputStream(filePath);
  59. copyManager.copyIn("COPY " + tableName + " FROM STDIN", fileInputStream);
  60. } finally {
  61. if (fileInputStream != null) {
  62. try {
  63. fileInputStream.close();
  64. } catch (IOException e) {
  65. e.printStackTrace();
  66. }
  67. }
  68. }
  69. }
  70. // 使用copyOut把数据从数据库中导出到文件中
  71. public static void copyToFile(Connection connection, String filePath, String tableOrQuery)
  72. throws SQLException, IOException {
  73. FileOutputStream fileOutputStream = null;
  74. try {
  75. CopyManager copyManager = new CopyManager((BaseConnection)connection);
  76. fileOutputStream = new FileOutputStream(filePath);
  77. copyManager.copyOut("COPY " + tableOrQuery + " TO STDOUT", fileOutputStream);
  78. } finally {
  79. if (fileOutputStream != null) {
  80. try {
  81. fileOutputStream.close();
  82. } catch (IOException e) {
  83. e.printStackTrace();
  84. }
  85. }
  86. }
  87. }
  88. }