Java Code Examples for org.postgresql.copy.CopyManager#copyIn()
The following examples show how to use
org.postgresql.copy.CopyManager#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: PGCopyPreparedStatement.java From phoebus with Eclipse Public License 1.0 | 7 votes |
@Override public int[] executeBatch() throws SQLException { long res = 0; try { CopyManager cpManager = ((PGConnection) connection).getCopyAPI(); PushbackReader reader = new PushbackReader(new StringReader(""), batchBuilder.length()); reader.unread(batchBuilder.toString().toCharArray()); res = cpManager.copyIn("COPY " + tableName + " FROM STDIN WITH CSV", reader); batchBuilder.setLength(0); reader.close(); } catch (IOException e) { throw new SQLException(e); } return new int[] { (int) res }; }
Example 2
Source File: InformationExtraction2Postgres.java From newsleak with GNU Affero General Public License v3.0 | 6 votes |
/** * Metadata is supposed to be presented in a four-tuple CSV format (docid, key, * value, type). @see uhh_lt.newsleak.reader.NewsleakReader should write a * temporary metadata file in that format (or assume it was produced by an * external process) * * The CSV file is imported via postgres directly. * * See <i>data/metadata_example.csv</i> for an example. */ private void metadataToPostgres() { try { // we need a mapping of document ids since ElasticsearchDocumentWriter generates // new Ids from an autoincrement-value String mappedMetadataFilepath = this.dataDirectory + File.separator + this.metadataFile + ".mapped"; mappingIdsInMetadata(mappedMetadataFilepath); // import csv into postgres db CopyManager cpManager = new CopyManager((BaseConnection) conn); st.executeUpdate("TRUNCATE TABLE metadata;"); this.logger.log(Level.INFO, "Importing metadata from " + mappedMetadataFilepath); Long n = cpManager.copyIn("COPY metadata FROM STDIN WITH CSV", new FileReader(mappedMetadataFilepath)); this.logger.log(Level.INFO, n + " metadata imported"); } catch (Exception e) { e.printStackTrace(); System.exit(1); } }
Example 3
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 4
Source File: JDBCTableEmitter.java From podyn with Apache License 2.0 | 5 votes |
public synchronized long copyFromReader(TableSchema tableSchema, Reader reader) { try { String query = tableSchema.copyFromStdin(); CopyManager copyManager = new CopyManager((BaseConnection) currentConnection); long numRows = copyManager.copyIn(query, reader); return numRows; } catch (Exception e) { throw new EmissionException(e); } }
Example 5
Source File: ChangeLoader.java From bireme with Apache License 2.0 | 5 votes |
@Override public Long call() throws SQLException, IOException { try { CopyManager mgr = new CopyManager((BaseConnection) conn); return mgr.copyIn(sql, pipeIn); } finally { try { pipeIn.close(); } catch (IOException ignore) { } } }