将pg库中的库表转存为nas盘中的文本文件,使用etl工具可以轻松实现。
使用pg自带的CopyManager也可以导入导出,程序实现的话成本比较低,不用重新部署一套etl工具。缺点:稳定性相对较差。
以下是用pgcopy实现的代码:
public static void main(String args[])throws Exception{
System.out.println(copyToFile(getConnection(),"/Users/apple/Downloads/aa.txt","( SELECT * from sys_user where create_date >= '2017-11-01'\n)"));;
}
public static Connection getConnection() throws Exception{
Class.forName("org.postgresql.Driver");
String url = "jdbc:postgresql://localhost:5432/db1";
Properties props = new Properties();
props.setProperty("user", "crms");
props.setProperty("password", "crms");
return DriverManager.getConnection(url, props);
}
/**
* 导入文件到数据库
* @param connection
* @param filePath
*