greenplum版本

greenplum: 4.3.17.1

依赖引入

org.apache.commons

commons-dbcp2

2.8.0

org.postgresql

postgresql

42.2.17

org.apache.commons

commons-lang3

3.11

gp导入导出工具类

package com.qingzhongli.gp;

import org.apache.commons.lang3.StringUtils;

import org.postgresql.copy.CopyManager;

import org.postgresql.core.BaseConnection;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import javax.sql.DataSource;

import java.io.*;

import java.sql.Connection;

import java.sql.SQLException;

/**

* @author qingzhongli.com

*/

public class GpDataUtils {

private static Logger logger = LoggerFactory.getLogger(GpDataUtils.class);

/**

* @param dataSource

* @param sql

* @param delimiter

* @param filePath

* @param encode

* @param header

* @return

*/

public static long exportData(DataSource dataSource,

String sql,

String delimiter,

String filePath,

String encode,

boolean header) {

Connection con = null;

OutputStream out = null;

Writer writer = null;

try {

con = dataSource.getConnection();

CopyManager cm = new CopyManager(con.unwrap(BaseConnection.class));

StringBuffer sb = new StringBuffer();

sb.append("copy (");

sb.append(sql);

sb.append(" ) TO STDOUT ");

sb.append("WITH DELIMITER '");

sb.append(delimiter);

sb.append("'");

if (header) {

sb.append(" HEADER ");

}

String copySql = sb.toString();

logger.info("exportData data begin , sql is {}", copySql);

long startTime = System.currentTimeMillis();

File file = new File(filePath);

if (!file.getParentFile().exists()) {

file.getParentFile().mkdirs();

}

out = new FileOutputStream(file);

long handledRowCount = 0;

if (StringUtils.isNotEmpty(encode)) {

writer = new OutputStreamWriter(out, encode);

handledRowCount = cm.copyOut(copySql, writer);

} else {

handledRowCount = cm.copyOut(copySql, out);

}

long elapsedTime = System.currentTimeMillis() - startTime;

logger.info("exportData data end, sql is {}, elapsed time = {}", copySql, elapsedTime);

return handledRowCount;

} catch (Exception e) {

logger.error(e.getMessage(), e);

return 0L;

} finally {

if (out != null) {

try {

out.close();

} catch (IOException e) {

logger.error(e.getMessage(), e);

}

}

if (writer != null) {

try {

writer.close();

} catch (IOException e) {

logger.error(e.getMessage(), e);

}

}

if (con != null) {

try {

con.close();

} catch (SQLException e) {

logger.error(e.getMessage(), e);

}

}

}

}

/**

* @param dataSource

* @param table

* @param delimiter

* @param file

* @return

*/

public static long importData(DataSource dataSource,

String table,

String delimiter,

String file) {

Connection con = null;

InputStream in = null;

try {

logger.info("import data begin");

con = dataSource.getConnection();

CopyManager cm = new CopyManager(con.unwrap(BaseConnection.class));

StringBuffer sb = new StringBuffer();

sb.append("copy ");

sb.append(table);

sb.append(" from STDIN ");

sb.append("WITH DELIMITER '");

sb.append(delimiter);

sb.append("'");

String copySql = sb.toString();

logger.info("import data begin, sql is {}", copySql);

long startTime = System.currentTimeMillis();

in = new FileInputStream(file);

long handledRowCount = cm.copyIn(copySql, in);

long elapsedTime = System.currentTimeMillis() - startTime;

logger.info("import data end, sql is {}, elapsed time = {}", copySql, elapsedTime);

return handledRowCount;

} catch (Exception e) {

logger.error(e.getMessage(), e);

return 0L;

} finally {

if (in != null) {

try {

in.close();

} catch (IOException e) {

logger.error(e.getMessage(), e);

}

}

if (con != null) {

try {

con.close();

} catch (SQLException e) {

logger.error(e.getMessage(), e);

}

}

}

}

}

测试验证

测试数据准备(E:\temp\test.txt),如下:

1,zhangsan

2,lisi

建立测试表,建表语句如下:

create table test (

id int,

name text

)

测试类,如下:

package com.qingzhongli.gp;

import org.apache.commons.dbcp2.BasicDataSource;

/**

* @author qingzhongli.com

*/

public class GpTest {

public static void main(String[] args) {

BasicDataSource ds = new BasicDataSource();

ds.setDriverClassName("org.postgresql.Driver");

ds.setUrl("jdbc:postgresql://192.168.37.90:5432/liqingzhong?gssEncMode=disable");

ds.setUsername("lqz");

ds.setPassword("Test123!@#");

ds.setMaxIdle(2);

ds.setInitialSize(1);

GpDataUtils.importData(ds, "test", ",", "E:\\temp\\test.txt");

}

}

运行测试类main方法,验证数据是否导入,如下:

[gpadmin@database-gp ~]$ psql -d liqingzhong -U test -h 192.168.37.90 -p 5432

Password for user test:

psql (8.2.15)

Type "help" for help.

liqingzhong=> select * from test;

id | name

----+----------

2 | lisi

1 | zhangsan

(2 rows)

liqingzhong=>

根据以上输出,数据已正常导入greenplum。

参考

java greenplum_java实现greenplum数据库导入实践相关推荐

  1. Java实现MySQL数据库导入

    距离上班还有一段时间.现在总结一下如何使用Java语言实现MySQL数据库导入: 首先新建名为test的数据库: 其次执行下面Java代码: [java] view plaincopy import  ...

  2. java实现 mysql导入数据库_Java实现MySQL数据库导入

    距离上班另一段时间.如今总结一下怎样使用Java语言实现MySQL数据库导入: 首先新建名为test的数据库: 其次运行以下Java代码: import java.io.File; import ja ...

  3. Greenplum【环境搭建 04】使用GPKafka实现Kafka数据导入Greenplum数据库(扩展安装文件网盘分享)

    分享资源地址及文件列表: 链接:https://pan.baidu.com/s/1XVTxKLkOYrL4pCZpFfs-Tg 提取码:sq90 包含文件: # 命令执行 gpkafka # 扩展安装 ...

  4. Greenplum【部署 04】GPSS扩展安装并使用GPKafka实现Kafka数据导入Greenplum数据库(安装包网盘分享)

    链接:https://pan.baidu.com/s/1MO-qL0Pxe6PojfZKsw3_qA 提取码:o7fl Greenplum Stream Server (GPSS)是一个ETL(提取. ...

  5. 11G数据库导入10G的操作实践

    Oracle11g数据库导入Oracle10g数据库操作笔记 用exp.imp命令时进行11g备份,导入10g的时候会抛错:不是有效的导出文件,头部验证失败:未成功终止导入. 我今天就遇到了这种情况, ...

  6. mysql 数据导入sqlserver_MySQL数据库导入SqlServer实践经验总结

    工作中有个任务是把MySQL数据库导入SqlServer中,因为任务紧急,这周一直在加班做,过程非常曲折,软件重装了几遍,网上资料也查了不少,基本上所有的问题都遇到了,不过总算是弄好了,期间也学到不少 ...

  7. Greenplum 实时数据仓库实践(6)——实时数据装载

    目录 6.1 建立数据仓库示例模型 6.1.1 业务场景 1. 操作型数据源 2. 销售订单数据仓库模型设计 6.1.2 建立数据库表 1. 在MySQL主库中创建源库对象并生成测试数据 2. 在Gr ...

  8. 农业银行数据库最佳实践和发展规划

    摘要 中国农业银行(以下简称:农行)在信息化系统建设过程中,先是把关系型数据库作为联机交易型数据库使用,后来为满足分析型应用需要开始使用分析型数据库,近几年来随着应用场景细分,对基于 Hadoop 的 ...

  9. Oracle数据库导入导出命令!

    oracle数据库导入导出命令! 转自:http://www.cnblogs.com/fjfzhkb/archive/2007/09/03/879807.html Oracle数据导入导出imp/ex ...

最新文章

  1. 泰国基础教育管理实行新体制
  2. 数据结构_栈与队列_总结
  3. 前端学习(975):bootstrap轮播图
  4. 《C#本质论》读书笔记(十二)构造器 @ Lennon
  5. 远程电脑备份与还原数据库
  6. 显示器颜色偏灰不鲜明_你离学会后期调色,只差了一块颜色“正确”的摄影显示器...
  7. 悲剧,当用cywin 写Linux脚本
  8. 人工智能十大算法_套磁amp;科研 |南加利福尼亚大学空间科学研究所终身教授YaoYi Chiang带来数据挖掘算法研究课题...
  9. LaTeX 在线编辑器(LaTeX online editors)
  10. 一些可以参考的文档集合5
  11. 基于Python的人脸识别
  12. Moving to Linux: Kiss the Blue Screen of Death Goodbye!读书笔记1
  13. rgb sw 线主板接口在哪_华硕ROG STRIX LC 360 RGB一体式水冷:与ROG主板更配
  14. proxy 服务器配置
  15. mysql查询近一年数据
  16. 软件开发中的思维僵化
  17. Vue_01_组件的使用
  18. oracle树状结构层级查询小结--connect by等之测试数据
  19. django安装xadmin及问题处理
  20. encodeURI()使用说明

热门文章

  1. 软件工程课程设计小结
  2. 分享一篇十分钟学会正则表达式教程
  3. throw 和 throws 的区别?
  4. 学计算机的需不需要考研?看完就明白了
  5. 从MAAS内调试curtin
  6. DirectX11 Alpha通道
  7. mac linux makefile,Makefile简单入门
  8. 关于js实现的Ajax调用,webService,一般处理程序,aspx
  9. Python 机器人学 —— 坐标系变换动画
  10. Java面试-多线程并发篇