Class JdbcMultiUpsertWriter
java.lang.Object
com.northconcepts.datapipeline.core.DataObject
com.northconcepts.datapipeline.core.Endpoint
com.northconcepts.datapipeline.core.DataEndpoint
com.northconcepts.datapipeline.core.DataWriter
com.northconcepts.datapipeline.core.ProxyWriter
com.northconcepts.datapipeline.jdbc.JdbcMultiUpsertWriter
Writes records to a database table using the upsert idiom (attempt to insert, but update if duplicate key exists).
It uses 1 or more connections; each connection writing asynchronously using a separate thread.
The default strategy used is
GenericUpsert, however several other are provided: MergeUpsert, VariableFieldsUpsert, MySqlUpsert.-
Nested Class Summary
Nested classes/interfaces inherited from class com.northconcepts.datapipeline.core.DataEndpoint
DataEndpoint.State -
Field Summary
Fields inherited from class com.northconcepts.datapipeline.core.DataEndpoint
lastRecord, PRODUCT, PRODUCT_VERSION, VENDOR, XML_INPUT_FACTORY_KEYFields inherited from class com.northconcepts.datapipeline.core.Endpoint
BUFFER_SIZE, captureElapsedTime, DEFAULT_READ_BUFFER_SIZEFields inherited from class com.northconcepts.datapipeline.core.DataObject
id, log, name, TIMESTAMP_FORMAT -
Constructor Summary
ConstructorsConstructorDescriptionJdbcMultiUpsertWriter(JdbcConnectionFactory factory, int connections, int maxQueuedRecordsPerConnection, String tableName, IUpsert upsert, String... keyFieldNames) Creates a writer that obtains its connections from the factory when opened; at least one key field is required.JdbcMultiUpsertWriter(JdbcConnectionFactory factory, int connections, int maxQueuedRecordsPerConnection, String tableName, String... keyFieldNames) Creates a writer usingGenericUpsertthat obtains its connections from the factory when opened.JdbcMultiUpsertWriter(DataSource dataSource, int connections, int maxQueuedRecordsPerConnection, String tableName, IUpsert upsert, String... keyFieldNames) Creates a writer that obtains its connections from the data source when opened; at least one key field is required.JdbcMultiUpsertWriter(DataSource dataSource, int connections, int maxQueuedRecordsPerConnection, String tableName, String... keyFieldNames) Creates a writer usingGenericUpsertthat obtains its connections from the data source when opened. -
Method Summary
Modifier and TypeMethodDescriptionaddExceptionProperties(DataException exception) Adds this endpoint's current state to aDataException.protected JdbcUpsertWritercreateJdbcUpsertWriter(Connection connection, String tableName, IUpsert upsertStrategy) getAsyncWriter(int index) Returns the asynchronous writer feeding the connection at the 0-based index ornullbeforeopen().intReturns the number of records to chunk together in batch updates; 1 or less (the default is -1) disables batching.intReturns the number of connections, each written to by its own thread.Returns an exception thrown by one of the internal AsyncWriters if any.Returns all exceptions thrown by the internal AsyncWriters (the list will not be null, but may contain no elements).getJdbcType(Class<?> type) TheTypesconstant (or a database specific value) assigned to the specified Java class type usingsetJdbcType(Class, int)or null if none was set.getJdbcWriter(int index) Returns the writer for the connection at the 0-based index ornullbeforeopen().Returns the list of fields that must not be updated if the recored already exists (default null).Returns the upsert strategy; each connection's writer uses its own clone.booleanIndicates if this writer should call commit on each connection after every batch is sent, otherwise, commit is called after the final batch update (default is false).booleanisDebug()Indicates if the generated SQL should be logged (default false).voidopen()Makes this endpoint ready for reading or writing.voidRe-throws an exception thrown by one of the internal AsyncWriters or returns silently if no exceptions were thrown.setBatchSize(int batchSize) Sets the number of records to chunk together in batch updates.setCommitBatch(boolean commitBatch) Indicates if this writer should call commit on each connection after every batch is sent, otherwise, commit is called after the final batch update (default is false).setDebug(boolean debug) Indicates if the generated SQL should be logged (default false).setJdbcType(Class<?> type, int jdbcType) Forces field values of the specified Java class type to be sent to the database usingPreparedStatement.setObject(parameterIndex, fieldValue, jdbcType)(instead of the appropriatePreparedStatement.setXXX()method for its field's type).setJdbcType(String fieldName, int jdbcType) The given Java object will be converted to the given targetSqlType before being sent to the database.setNonUpdateFields(FieldList nonUpdateFields) Assigns the list of fields that must not be updated if the recored already exists (default null).setNonUpdateFields(String... nonUpdateFields) Assigns the list of fields that must not be updated if the recored already exists (default null).protected voidOverridden by subclasses to write the specified record to thisDataWriter.Methods inherited from class com.northconcepts.datapipeline.core.ProxyWriter
available, close, getNestedWriter, interceptRecord, map, map, setNestedDataWriter, setNestedDataWriterMethods inherited from class com.northconcepts.datapipeline.core.DataWriter
getNestedEndpoint, getRootEndpoint, getRootWriter, getWriter, writeMethods inherited from class com.northconcepts.datapipeline.core.DataEndpoint
decrementRecordCount, enableJmx, getLastRecord, getRecordCount, getRecordCountAsBigInteger, getRecordCountAsString, incrementRecordCount, isRecordCountBigInteger, resetRecordCount, toStringMethods inherited from class com.northconcepts.datapipeline.core.Endpoint
addElapsedtime, assertClosed, assertNotOpened, assertOpened, finalize, getClosedOn, getDescription, getElapsedTime, getElapsedTimeAsString, getOpenedOn, getOpenElapsedTime, getOpenElapsedTimeAsString, getSelfTime, getSelfTimeAsString, getState, isCaptureElapsedTime, isClosed, isOpen, setCaptureElapsedTime, setDescription
-
Constructor Details
-
JdbcMultiUpsertWriter
public JdbcMultiUpsertWriter(DataSource dataSource, int connections, int maxQueuedRecordsPerConnection, String tableName, IUpsert upsert, String... keyFieldNames) Creates a writer that obtains its connections from the data source when opened; at least one key field is required. -
JdbcMultiUpsertWriter
public JdbcMultiUpsertWriter(DataSource dataSource, int connections, int maxQueuedRecordsPerConnection, String tableName, String... keyFieldNames) Creates a writer usingGenericUpsertthat obtains its connections from the data source when opened. -
JdbcMultiUpsertWriter
public JdbcMultiUpsertWriter(JdbcConnectionFactory factory, int connections, int maxQueuedRecordsPerConnection, String tableName, IUpsert upsert, String... keyFieldNames) throws DataException Creates a writer that obtains its connections from the factory when opened; at least one key field is required.- Throws:
DataException
-
JdbcMultiUpsertWriter
public JdbcMultiUpsertWriter(JdbcConnectionFactory factory, int connections, int maxQueuedRecordsPerConnection, String tableName, String... keyFieldNames) throws DataException Creates a writer usingGenericUpsertthat obtains its connections from the factory when opened.- Throws:
DataException
-
-
Method Details
-
getConnections
public int getConnections()Returns the number of connections, each written to by its own thread. -
getJdbcWriter
Returns the writer for the connection at the 0-based index ornullbeforeopen(). -
getAsyncWriter
Returns the asynchronous writer feeding the connection at the 0-based index ornullbeforeopen(). -
getBatchSize
public int getBatchSize()Returns the number of records to chunk together in batch updates; 1 or less (the default is -1) disables batching. -
setBatchSize
Sets the number of records to chunk together in batch updates. Updates are not batched if this value is 1 or less. JdbcWriter defaults to non batching updates. -
isCommitBatch
public boolean isCommitBatch()Indicates if this writer should call commit on each connection after every batch is sent, otherwise, commit is called after the final batch update (default is false). -
setCommitBatch
Indicates if this writer should call commit on each connection after every batch is sent, otherwise, commit is called after the final batch update (default is false). -
getNonUpdateFields
Returns the list of fields that must not be updated if the recored already exists (default null). This method may returnnullor a list of empty fields. -
setNonUpdateFields
Assigns the list of fields that must not be updated if the recored already exists (default null). -
setNonUpdateFields
Assigns the list of fields that must not be updated if the recored already exists (default null). -
getUpsert
Returns the upsert strategy; each connection's writer uses its own clone. -
setJdbcType
The given Java object will be converted to the given targetSqlType before being sent to the database.
Note that this method may be used to pass database-specific abstract data types.
- Parameters:
fieldName-jdbcType- the object containing the JDBC Type parameter value
-
setJdbcType
Forces field values of the specified Java class type to be sent to the database usingPreparedStatement.setObject(parameterIndex, fieldValue, jdbcType)(instead of the appropriatePreparedStatement.setXXX()method for its field's type). This method may be used to pass database-specific abstract data types.- Parameters:
type- the Java class type to override.jdbcType- one of theTypesconstants or a database specific value.
-
getJdbcType
TheTypesconstant (or a database specific value) assigned to the specified Java class type usingsetJdbcType(Class, int)or null if none was set.- Returns:
- one of the
Typesconstants or a database specific value.
-
isDebug
public boolean isDebug()Indicates if the generated SQL should be logged (default false). -
setDebug
Indicates if the generated SQL should be logged (default false). -
open
Description copied from class:DataEndpointMakes this endpoint ready for reading or writing.- Overrides:
openin classProxyWriter- Throws:
DataException
-
createJdbcUpsertWriter
protected JdbcUpsertWriter createJdbcUpsertWriter(Connection connection, String tableName, IUpsert upsertStrategy) -
writeImpl
Description copied from class:DataWriterOverridden by subclasses to write the specified record to thisDataWriter.Contract for subclasses (see also
docs/authoring/DataWriter.md):- Do not call this method directly —
DataWriter.write(Record)is the template method that wraps exceptions, increments the record count, and attaches the offending record to thrownDataExceptions. - Do not call
DataEndpoint.incrementRecordCount()here;DataWriter.write(Record)already does. - Throw raw exceptions;
DataWriter.write(Record)wraps them viaexception(throwable).setRecord(record).
- Overrides:
writeImplin classProxyWriter- Throws:
Throwable
- Do not call this method directly —
-
getException
Returns an exception thrown by one of the internal AsyncWriters if any. -
getExceptions
Returns all exceptions thrown by the internal AsyncWriters (the list will not be null, but may contain no elements). -
rethrowAsyncException
Re-throws an exception thrown by one of the internal AsyncWriters or returns silently if no exceptions were thrown.- Throws:
DataException
-
addExceptionProperties
Description copied from class:EndpointAdds this endpoint's current state to aDataException. Since this method is called whenever an exception is thrown, subclasses should override it to add their specific information.- Overrides:
addExceptionPropertiesin classProxyWriter
-