org.postgresql.copy.CopyIn Java Examples
The following examples show how to use
org.postgresql.copy.CopyIn.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PostgresqlManager.java From ReplicaDB with Apache License 2.0 | 5 votes |
@Override public int insertDataToTable(ResultSet resultSet, int taskId) throws SQLException, IOException { CopyIn copyIn = null; try { ResultSetMetaData rsmd = resultSet.getMetaData(); String tableName; // Get table name and columns if (options.getMode().equals(ReplicationMode.COMPLETE.getModeText())) { tableName = getSinkTableName(); } else { tableName = getQualifiedStagingTableName(); } String allColumns = getAllSinkColumns(rsmd); // Get Postgres COPY meta-command manager PgConnection copyOperationConnection = this.connection.unwrap(PgConnection.class); CopyManager copyManager = new CopyManager(copyOperationConnection); String copyCmd = getCopyCommand(tableName, allColumns); copyIn = copyManager.copyIn(copyCmd); char unitSeparator = 0x1F; int columnsNumber = rsmd.getColumnCount(); StringBuilder row = new StringBuilder(); StringBuilder cols = new StringBuilder(); byte[] bytes; String colValue; if (resultSet.next()) { // Create Bandwidth Throttling bandwidthThrottlingCreate(resultSet, rsmd); do { bandwidthThrottlingAcquiere(); // Get Columns values for (int i = 1; i <= columnsNumber; i++) { if (i > 1) cols.append(unitSeparator); switch (rsmd.getColumnType(i)) { case Types.CLOB: colValue = clobToString(resultSet.getClob(i)); break; case Types.BINARY: case Types.BLOB: colValue = blobToPostgresHex(resultSet.getBlob(i)); break; default: colValue = resultSet.getString(i); break; } if (!resultSet.wasNull() || colValue != null) cols.append(colValue); } // Escape special chars if (this.options.isSinkDisableEscape()) row.append(cols.toString()); else row.append(cols.toString().replace("\\", "\\\\").replace("\n", "\\n").replace("\r", "\\r").replace("\u0000", "")); // Row ends with \n row.append("\n"); // Copy data to postgres bytes = row.toString().getBytes(StandardCharsets.UTF_8); copyIn.writeToCopy(bytes, 0, bytes.length); // Clear StringBuilders row.setLength(0); // set length of buffer to 0 row.trimToSize(); cols.setLength(0); // set length of buffer to 0 cols.trimToSize(); } while (resultSet.next()); } copyIn.endCopy(); } catch (Exception e) { if (copyIn != null && copyIn.isActive()) { copyIn.cancelCopy(); } this.connection.rollback(); throw e; } finally { if (copyIn != null && copyIn.isActive()) { copyIn.cancelCopy(); } } this.getConnection().commit(); return 0; }
Example #2
Source File: DbPersistorPostgres.java From rdf2x with Apache License 2.0 | 4 votes |
private static void copyBatch(CopyIn copyIn, StringBuilder buffer) throws UnsupportedEncodingException, SQLException { byte[] bytes = buffer.toString().getBytes("UTF-8"); buffer.setLength(0); log.debug("Writing batch of {} MB", String.format("%.2f", bytes.length / 1000000.0)); copyIn.writeToCopy(bytes, 0, bytes.length); }