Class PostgreSqlUpsertWriter


public class PostgreSqlUpsertWriter extends PostgreSqlInsertWriter
Generates PostgreSQL upsert statement from records.
  • Constructor Details

    • PostgreSqlUpsertWriter

      public PostgreSqlUpsertWriter(String table, File file, boolean append, String... keyFieldNames)
      Creates a writer of upserts into table that use the given key fields for ON CONFLICT; at least one is required.
    • PostgreSqlUpsertWriter

      public PostgreSqlUpsertWriter(String table, File file, boolean append, List<String> keyFieldNames)
      Creates a writer of upserts into table that use the given key fields for ON CONFLICT; at least one is required.
    • PostgreSqlUpsertWriter

      public PostgreSqlUpsertWriter(String table, File file, String... keyFieldNames)
      Creates a writer of upserts into table that use the given key fields for ON CONFLICT; at least one is required.
    • PostgreSqlUpsertWriter

      public PostgreSqlUpsertWriter(String table, File file, List<String> keyFieldNames)
      Creates a writer of upserts into table that use the given key fields for ON CONFLICT; at least one is required.
    • PostgreSqlUpsertWriter

      public PostgreSqlUpsertWriter(String table, Writer writer, String... keyFieldNames)
      Creates a writer of upserts into table that use the given key fields for ON CONFLICT; at least one is required.
    • PostgreSqlUpsertWriter

      public PostgreSqlUpsertWriter(String table, Writer writer, List<String> keyFieldNames)
      Creates a writer of upserts into table that use the given key fields for ON CONFLICT; at least one is required.
  • Method Details

    • writeRecord

      protected void writeRecord(Record record) throws Throwable
      Description copied from class: AbstractWriter
      Writes one row; called first for the header row (see AbstractWriter.isHeaderRow()) when field names are written, then for each record.
      Overrides:
      writeRecord in class PostgreSqlInsertWriter
      Throws:
      Throwable
    • close

      public void close()
      Description copied from class: DataEndpoint
      Indicates that this endpoint has finished reading or writing.
      Overrides:
      close in class PostgreSqlInsertWriter
    • setPretty

      public PostgreSqlUpsertWriter setPretty(boolean pretty)
      Indicates if line breaks and indentations should be added to output upsert statement (default false).

      Example output (pretty = true):


      INSERT INTO "tableName" ("col1", "col2", "col3")
      VALUES (val1, val2, val3),
             (val1, val2, val3),
             (val1, val2, val3)
      ON CONFLICT ("id") DO UPDATE SET "col1" = EXCLUDED."col1", "col2" = EXCLUDED."col2", "col3" = EXCLUDED."col3";

      Example output (pretty = false):
      INSERT INTO "tableName" ("col1", "col2", "col3") VALUES (val1, val2, val3), (val1, val2, val3), (val1, val2, val3) ON CONFLICT ("id") DO UPDATE SET "col1" = EXCLUDED."col1", "col2" = EXCLUDED."col2", "col3" = EXCLUDED."col3";
      Overrides:
      setPretty in class PostgreSqlInsertWriter
    • setBatchSize

      public PostgreSqlUpsertWriter setBatchSize(int batchSize)
      Sets the number of records to chunk together in batch upserts. Upserts are not batched if this value is 1 or less. PostgreSqlUpsertWriter defaults to non batching upserts.

      Example output (batchSize = 2):
      INSERT INTO "tableName" ("col1", "col2", "col3")
      VALUES (val1, val2, val3),
             (val1, val2, val3),
      ON CONFLICT ("id") DO UPDATE SET "col1" = EXCLUDED."col1", "col2" = EXCLUDED."col2", "col3" = EXCLUDED."col3";

      Example output (batchSize = 0):
      INSERT INTO "tableName" ("col1", "col2", "col3") VALUES (val1, val2, val3) ON CONFLICT ("id") DO UPDATE SET "col1" = EXCLUDED."col1", "col2" = EXCLUDED."col2", "col3" = EXCLUDED."col3";
      INSERT INTO "tableName" ("col1", "col2", "col3") VALUES (val1, val2, val3) ON CONFLICT ("id") DO UPDATE SET "col1" = EXCLUDED."col1", "col2" = EXCLUDED."col2", "col3" = EXCLUDED."col3";
      Overrides:
      setBatchSize in class PostgreSqlInsertWriter
    • getKeyFieldNames

      public List<String> getKeyFieldNames()
      Returns the key fields listed in the ON CONFLICT clause.
    • getConflictAction

      public ConflictAction getConflictAction()
      Indicates the action to perform on conflict, or null if not set, in which case ConflictAction.UPDATE is used.
    • setConflictAction

      public PostgreSqlUpsertWriter setConflictAction(ConflictAction conflictAction)
      Indicates the action to perform on conflict (default ConflictAction.UPDATE).