@EventDriven @InputRequirement(value=INPUT_REQUIRED) @Tags(value={"sql","record","jdbc","put","database","update","insert","delete"}) @CapabilityDescription(value="The PutDatabaseRecord processor uses a specified RecordReader to input (possibly multiple) records from an incoming flow file. These records are translated to SQL statements and executed as a single transaction. If any errors occur, the flow file is routed to failure or retry, and if the records are transmitted successfully, the incoming flow file is routed to success. The type of statement executed by the processor is specified via the Statement Type property, which accepts some hard-coded values such as INSERT, UPDATE, and DELETE, as well as \'Use statement.type Attribute\', which causes the processor to get the statement type from a flow file attribute. IMPORTANT: If the Statement Type is UPDATE, then the incoming records must not alter the value(s) of the primary keys (or user-specified Update Keys). If such records are encountered, the UPDATE statement issued to the database may do nothing (if no existing records with the new primary key values are found), or could inadvertently corrupt the existing data (by changing records for which the new values of the primary keys exist).") @ReadsAttribute(attribute="statement.type", description="If \'Use statement.type Attribute\' is selected for the Statement Type property, the value of this attribute will be used to determine the type of statement (INSERT, UPDATE, DELETE, SQL, etc.) to generate and execute.") @WritesAttribute(attribute="putdatabaserecord.error", description="If an error occurs during processing, the flow file will be routed to failure or retry, and this attribute will be populated with the cause of the error.") public class PutDatabaseRecord extends AbstractProcessor
Modifier and Type | Class and Description |
---|---|
(package private) static class |
PutDatabaseRecord.DMLSettings |
(package private) static class |
PutDatabaseRecord.PreparedSqlAndColumns |
private static class |
PutDatabaseRecord.RecordPathStatementType |
(package private) static class |
PutDatabaseRecord.SchemaKey |
(package private) static class |
PutDatabaseRecord.SqlAndIncludedColumns
A holder class for a SQL prepared statement and a BitSet indicating which columns are being updated (to determine which values from the record to set on the statement)
A value of null for getIncludedColumns indicates that all columns/fields should be included.
|
Constructor and Description |
---|
PutDatabaseRecord() |
onTrigger
getControllerServiceLookup, getIdentifier, getLogger, getNodeTypeProvider, init, initialize, isConfigurationRestored, isScheduled, toString, updateConfiguredRestoredTrue, updateScheduledFalse, updateScheduledTrue
equals, getPropertyDescriptor, getPropertyDescriptors, hashCode, onPropertyModified, validate
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
isStateful
getPropertyDescriptor, getPropertyDescriptors, onPropertyModified, validate
public static final String UPDATE_TYPE
public static final String INSERT_TYPE
public static final String DELETE_TYPE
public static final String UPSERT_TYPE
public static final String INSERT_IGNORE_TYPE
public static final String SQL_TYPE
public static final String USE_ATTR_TYPE
public static final String USE_RECORD_PATH
static final String STATEMENT_TYPE_ATTRIBUTE
static final String PUT_DATABASE_RECORD_ERROR
static final AllowableValue IGNORE_UNMATCHED_FIELD
static final AllowableValue FAIL_UNMATCHED_FIELD
static final AllowableValue IGNORE_UNMATCHED_COLUMN
static final AllowableValue WARNING_UNMATCHED_COLUMN
static final AllowableValue FAIL_UNMATCHED_COLUMN
public static final Relationship REL_SUCCESS
static final Relationship REL_RETRY
static final Relationship REL_FAILURE
protected static Set<Relationship> relationships
static final PropertyDescriptor RECORD_READER_FACTORY
static final PropertyDescriptor STATEMENT_TYPE
static final PropertyDescriptor STATEMENT_TYPE_RECORD_PATH
static final PropertyDescriptor DATA_RECORD_PATH
static final PropertyDescriptor DBCP_SERVICE
static final PropertyDescriptor CATALOG_NAME
static final PropertyDescriptor SCHEMA_NAME
static final PropertyDescriptor TABLE_NAME
static final PropertyDescriptor TRANSLATE_FIELD_NAMES
static final PropertyDescriptor UNMATCHED_FIELD_BEHAVIOR
static final PropertyDescriptor UNMATCHED_COLUMN_BEHAVIOR
static final PropertyDescriptor UPDATE_KEYS
static final PropertyDescriptor FIELD_CONTAINING_SQL
static final PropertyDescriptor ALLOW_MULTIPLE_STATEMENTS
static final PropertyDescriptor QUOTE_IDENTIFIERS
static final PropertyDescriptor QUOTE_TABLE_IDENTIFIER
static final PropertyDescriptor QUERY_TIMEOUT
static final PropertyDescriptor TABLE_SCHEMA_CACHE_SIZE
static final PropertyDescriptor MAX_BATCH_SIZE
static final PropertyDescriptor DB_TYPE
protected static final Map<String,DatabaseAdapter> dbAdapters
protected static List<PropertyDescriptor> propDescriptors
private com.github.benmanes.caffeine.cache.Cache<PutDatabaseRecord.SchemaKey,TableSchema> schemaCache
private DatabaseAdapter databaseAdapter
private volatile RecordPath dataRecordPath
public Set<Relationship> getRelationships()
getRelationships
in interface Processor
getRelationships
in class AbstractSessionFactoryProcessor
protected List<PropertyDescriptor> getSupportedPropertyDescriptors()
getSupportedPropertyDescriptors
in class AbstractConfigurableComponent
protected PropertyDescriptor getSupportedDynamicPropertyDescriptor(String propertyDescriptorName)
getSupportedDynamicPropertyDescriptor
in class AbstractConfigurableComponent
protected Collection<ValidationResult> customValidate(ValidationContext validationContext)
customValidate
in class AbstractConfigurableComponent
@OnScheduled public void onScheduled(ProcessContext context)
public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException
onTrigger
in class AbstractProcessor
ProcessException
private void executeSQL(ProcessContext context, FlowFile flowFile, Connection connection, RecordReader recordReader) throws IllegalArgumentException, MalformedRecordException, IOException, SQLException
private void executeDML(ProcessContext context, ProcessSession session, FlowFile flowFile, Connection con, RecordReader recordReader, String explicitStatementType, PutDatabaseRecord.DMLSettings settings) throws IllegalArgumentException, MalformedRecordException, IOException, SQLException
private void setParameter(PreparedStatement ps, int index, Object value, int fieldSqlType, int sqlType) throws IOException
IOException
private String getJdbcUrl(Connection connection)
private String getStatementType(ProcessContext context, FlowFile flowFile)
private void putToDatabase(ProcessContext context, ProcessSession session, FlowFile flowFile, Connection connection) throws Exception
Exception
String generateTableName(PutDatabaseRecord.DMLSettings settings, String catalog, String schemaName, String tableName, TableSchema tableSchema)
private Set<String> getNormalizedColumnNames(RecordSchema schema, boolean translateFieldNames)
PutDatabaseRecord.SqlAndIncludedColumns generateInsert(RecordSchema recordSchema, String tableName, TableSchema tableSchema, PutDatabaseRecord.DMLSettings settings) throws IllegalArgumentException, SQLException
IllegalArgumentException
SQLException
PutDatabaseRecord.SqlAndIncludedColumns generateUpsert(RecordSchema recordSchema, String tableName, String updateKeys, TableSchema tableSchema, PutDatabaseRecord.DMLSettings settings) throws IllegalArgumentException, SQLException, MalformedRecordException
PutDatabaseRecord.SqlAndIncludedColumns generateInsertIgnore(RecordSchema recordSchema, String tableName, String updateKeys, TableSchema tableSchema, PutDatabaseRecord.DMLSettings settings) throws IllegalArgumentException, SQLException, MalformedRecordException
PutDatabaseRecord.SqlAndIncludedColumns generateUpdate(RecordSchema recordSchema, String tableName, String updateKeys, TableSchema tableSchema, PutDatabaseRecord.DMLSettings settings) throws IllegalArgumentException, MalformedRecordException, SQLException
PutDatabaseRecord.SqlAndIncludedColumns generateDelete(RecordSchema recordSchema, String tableName, TableSchema tableSchema, PutDatabaseRecord.DMLSettings settings) throws IllegalArgumentException, MalformedRecordException, SQLDataException
private void checkValuesForRequiredColumns(RecordSchema recordSchema, TableSchema tableSchema, PutDatabaseRecord.DMLSettings settings)
private Set<String> getUpdateKeyColumnNames(String tableName, String updateKeys, TableSchema tableSchema) throws SQLIntegrityConstraintViolationException
private Set<String> normalizeKeyColumnNamesAndCheckForValues(RecordSchema recordSchema, String updateKeys, PutDatabaseRecord.DMLSettings settings, Set<String> updateKeyColumnNames, String quoteString) throws MalformedRecordException
MalformedRecordException
Copyright © 2023 Apache NiFi Project. All rights reserved.