Execute multiple SQL statements in a single transaction
This example demonstrates how to use the Vertica - Multi Execute Snap to execute multiple SQL statements as one transaction per document.
Download this pipeline.Example Input
[
{
"ORDER_DETAIL": { "SO_LN_ITM_NR": "000010", "SO_DTL_CRT_DT": "2014-02-12", "SO_DTL_CLOSE_DT": "2014-02-12", "FXD_VALU_DT": "2014-02-12", "ORD_LN_CNCL_DT": "" },
"ADJUSTMENTS": [ { "SO_ADJ_CD": "ZNAJ", "SO_ADJ_AM": "0.00", "LGCL_DEL_FG": "N", "CR_FG": "N", "MAIN_MCC_FG": "", "BUS_AREA_CD": "9600", "FUNC_AREA_CD": "A051", "PR_EFF_DT": "" }, { "SO_ADJ_CD": "ZNAK", "SO_ADJ_AM": "0.00", "LGCL_DEL_FG": "N", "CR_FG": "N", "MAIN_MCC_FG": "", "BUS_AREA_CD": "9600", "FUNC_AREA_CD": "A051", "PR_EFF_DT": "" } ] }
]
In row one we select the adjustments; given the input, it returns a list of objects. The insert statement will then create a batch for every object in the list and execute it. In row two we select the order detail; given the input, it returns one object. The insert statement will then execute one statement using the order detail object.
The statement can use variables that are the result of the path execution. As an example, the below is the result of jsonPath($, "$ADJUSTMENTS[*]):
{
"SO_ADJ_CD": "ZNAJ",
"SO_ADJ_AM": "0.00",
"LGCL_DEL_FG": "N",
"CR_FG": "N",
"MAIN_MCC_FG": "",
"BUS_AREA_CD": "9600",
"FUNC_AREA_CD": "A051",
"PR_EFF_DT": ""
},
{
"SO_ADJ_CD": "ZNAK",
"SO_ADJ_AM": "0.00",
"LGCL_DEL_FG": "N",
"CR_FG": "N",
"MAIN_MCC_FG": "",
"BUS_AREA_CD": "9600",
"FUNC_AREA_CD": "A051",
"PR_EFF_DT": ""
}
The statement then refers to the variables of the result using, for example,
$BUS_AREA_CD, not as one would assume the whole path to the
variable in the context of the input document. jsonPath($,
"$ADJUSTMENTS[*].BUS_AREA_CD"), instead the context is the result of
the path evaluation.
A successful execution of all statements will issue a commit of the transaction. Any failure of any of the statements will cause the rollback of all issued operations for the given input document.
[
{
"execution_1": { "status": "success", "sqlStatement": "Insert into ORDER_DETAIL VALUES (?, ?, ?, ?, ?)", "successfulDocuments": [ { "message": "success", "status": 1, "original": { "SO_LN_ITM_NR": "000010", "SO_DTL_CRT_DT": "2014-02-12", "SO_DTL_CLOSE_DT": "2014-02-12", "FXD_VALU_DT": "2014-02-12", "ORD_LN_CNCL_DT": "" } } ] },
"execution_0": { "status": "success", "sqlStatement": "Insert into ADJUSTMENTS VALUES (?, ?, ?, ?, ?, ?, ?, ?)", "successfulDocuments": [ { "message": "success", "status": 2, "original": { "SO_ADJ_CD": "ZNAJ", "SO_ADJ_AM": "0.00", "LGCL_DEL_FG": "N", "CR_FG": "N", "MAIN_MCC_FG": "", "BUS_AREA_CD": "9600", "FUNC_AREA_CD": "A051", "PR_EFF_DT": "" } }, { "message": "success", "status": 2, "original": { "SO_ADJ_CD": "ZNAK", "SO_ADJ_AM": "0.00", "LGCL_DEL_FG": "N", "CR_FG": "N", "MAIN_MCC_FG": "", "BUS_AREA_CD": "9600", "FUNC_AREA_CD": "A051", "PR_EFF_DT": "" } } ] } }
]
One output document is written per input document. The output document provides
executions, each reflecting the execution of one row. The index of the
execution_<index> represents the index of the statement
in the Executions table.
Each execution provides information about the statement execution. In the above case, all statements were successful. Original shows the data that was used as part of the statement execution (not the input document, but the data of the path evaluation).
The SQL statement shows the statement that was executed.
Unsuccessful Output with Error Handling
In an unsuccessful case, you can enable the error view to debug the pipeline:
[
{
"execution_1": { "status": "failed", "sqlStatement": "Insert into wrongname VALUES (?, ?, ?, ?, ?)", "errorDocuments": [ { "error": "Failed to flush batch.", "reason": "Table 'integration.wrongname' doesn't exist", "original": { "SO_LN_ITM_NR": "000010", "SO_DTL_CRT_DT": "2014-02-12", "SO_DTL_CLOSE_DT": "2014-02-12", "FXD_VALU_DT": "2014-02-12", "ORD_LN_CNCL_DT": "" }, "resolution": "Please address the issue reported by the database.", "stacktrace": "org.jooq.exception.DataAccessException: SQL [Insert into wrongname VALUES (?, ?, ?, ?, ?)]; Table 'integration.wrongname' doesn't exist\n...." } ] },
"execution_0": { "status": "success", "sqlStatement": "Insert into ADJUSTMENTS VALUES (?, ?, ?, ?, ?, ?, ?, ?)", "successfulDocuments": [ { "message": "success", "status": 2, "original": { "SO_ADJ_CD": "ZNAJ", "SO_ADJ_AM": "0.00", "LGCL_DEL_FG": "N", "CR_FG": "N", "MAIN_MCC_FG": "", "BUS_AREA_CD": "9600", "FUNC_AREA_CD": "A051", "PR_EFF_DT": "" } }, { "message": "success", "status": 2, "original": { "SO_ADJ_CD": "ZNAK", "SO_ADJ_AM": "0.00", "LGCL_DEL_FG": "N", "CR_FG": "N", "MAIN_MCC_FG": "", "BUS_AREA_CD": "9600", "FUNC_AREA_CD": "A051", "PR_EFF_DT": "" } } ] } }
]
Here we see that execution_1 of the current input document failed. The reason and resolution are provided, as well as the stack trace for debugging purpose.