[ 
https://issues.apache.org/jira/browse/NIFI-3704?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15975828#comment-15975828
 ] 

ASF GitHub Bot commented on NIFI-3704:
--------------------------------------

Github user ijokarumawak commented on the issue:

    https://github.com/apache/nifi/pull/1677
  
    @mattyb149 Thanks for the updated commit. I confirmed that my comments are 
incorporated.
    
    It works as expected in most cases, however, I found a case which needs to 
be addressed.
    
    Let's say I have a table with a primary key like this:
    
    ```sql
    create table tutorials_tbl(
       tutorial_id INT NOT NULL,
       tutorial_title VARCHAR(100) NOT NULL,
       tutorial_author VARCHAR(40) NOT NULL,
       submission_date DATE,
       PRIMARY KEY ( tutorial_id )
    );
    ```
    
    And insert some rows, then update a row, especially update its primary key:
    
    ```sql
    update tutorials_tbl set tutorial_id = 110 where tutorial_id = 11;
    ```
    
    Above update query generates following JSON via CaptureChangeMySQL:
    
    ```json
    { "type" : "update",
      "timestamp" : 1492648209000,
      "binlog_filename" : "mysql-server-bin.000004",
      "binlog_position" : 97152,
      "database" : "nifi_test",
      "table_name" : "tutorials_tbl",
      "table_id" : 222,
      "columns" : [ {
        "id" : 1,    "name" : "tutorial_id",    "column_type" : 4,    
"last_value" : 11,    "value" : 110
      }, {
        "id" : 2,    "name" : "tutorial_title",    "column_type" : 12,    
"last_value" : "11th",    "value" : "11th"
      }, {
        "id" : 3,    "name" : "tutorial_author",    "column_type" : 12,    
"last_value" : "koji",    "value" : "koji"
      }, {
        "id" : 4,    "name" : "submission_date",    "column_type" : 91,    
"last_value" : null,    "value" : null
      } ]}
    ```
    
    `Transform to Flat JSON` (JoltTransform) flattens the event JSON as below. 
At this point, the record image before update is dropped:
    
    ```json
    [ {  "tutorial_id" : 110,  "tutorial_title" : "11th",  "tutorial_author" : 
"koji",  "submission_date" : null } ]
    ```
    
    Finally, PutDatabaseRecord generates an update sql statement with `where 
tutorial_id = 110`. But it doesn't update anything, because it should have used 
`where tutorial_id = 11` with before update row image.
    
    We might be able to handle this by generating two delete and insert records 
in a NiFi flow, or do something smart at PutDatabaseRecord.
    How do you think?


> Add PutDatabaseRecord processor
> -------------------------------
>
>                 Key: NIFI-3704
>                 URL: https://issues.apache.org/jira/browse/NIFI-3704
>             Project: Apache NiFi
>          Issue Type: New Feature
>          Components: Extensions
>            Reporter: Matt Burgess
>            Assignee: Matt Burgess
>             Fix For: 1.2.0
>
>
> With the inclusion of NIFI-1280, which added Controller Services for 
> RecordReaders and RecordWriters, we could now support a processor that reads 
> records in, generates SQL statements for those records (with a specified verb 
> such as INSERT, UPDATE, DELETE, etc.), and can execute all the records in one 
> flow file as a batch. This would allow the processor to use a single 
> PreparedStatement and, for a flow file containing multiple records, would be 
> able to execute them all at once. This is in contrast to PutSQL which handles 
> batches across flow files (if fragmented transactions are enabled) or with a 
> discrete set (by taking at most a specified number of flow files at a time).
> This processor (called PutDatabaseRecord) would effectively act like the 
> combination of ConvertJSONToSQL and PutSQL, with the added features of being 
> able to take records in an arbitrary format (given that there is a 
> RecordReader implementation for that format) such as Avro, JSON, CSV, etc. 
> and execute all the statements for the flow file at once.
> Another improvement upon what can be done in ConvertJSONToSQL would be to 
> support BEGIN, COMMIT, and SQL verbs. This could be accomplished by adding an 
> AllowableValue to the dropdown, letting the user select "Use statement.type 
> Attribute". If this was selected, then the verb would be expected to be in 
> the value of the "statement.type" attribute of the incoming flow file.  Note 
> that this may supercede or deprecate the need for NIFI-3676, unless this 
> capability is also desired for that processor.
> For BEGIN and COMMIT verbs, the contents of the record(s) are not needed, as 
> the type itself should be enough to generate the appropriate SQL commands. 
> For the "SQL" Statement type, the processor could either expect the flow file 
> to contain a SQL statement (so the RecordReader would not be used), or it 
> could expect a field called "sql" that contains the SQL statement as its 
> value.



--
This message was sent by Atlassian JIRA
(v6.3.15#6346)

Reply via email to