Cassandra - Execute

Overview

This Snap allows you to execute arbitrary Cassandra query language (CQL) supported by JDBC.
Important: This Snap works only with single queries.
Warning: You can drop your database with it, so be careful.


Valid JSON paths that are defined in the where clause for queries/statements will be substituted with values from an incoming document. Documents will be written to the error view if the document is missing a value to be substituted into the query/statement.

If a select query is executed, the query's results are merged into the incoming document and any existing keys will have their values overwritten. On the other hand, the original document is written if there are no results from the query.

Snaps in this Snap Pack display an exception (raised by the JDBC driver) when you query a map column that has a timestamp as the key.

Example
  1. Create a table containing a map whose key is a timestamp:

    CREATE COLUMNFAMILY t (userid text PRIMARY KEY, todo map<timestamp,
            text>);
  2. Insert values into the newly-created table.

    INSERT INTO t (userid, todo) VALUES ('a', {'2013-09-22T12:01:00.000+0000':
                'text'});
  3. Once the insert operation succeeds, query the map column:SELECT userid, todo FROM t;

The Snap displays the following exception:

com.datastax.driver.core.exceptions.CodecNotFoundException:

Codec not found for requested operation: [timestamp <->
    java.sql.Timestamp]
Warning: The Cassandra Execute Snap is for simple DML (SELECT, INSERT, UPDATE, DELETE) type statements.

Prerequisites

Validated Cassandra account, verified network connectivity to Cassandra server and port, and the Cassandra server running.

Known issues

When the SQL statement property is an expression, the Pipeline parameters are shown in the suggest, but not the input schema.

Limitations

  • The Cassandra-Execute Snap using Apache Cassandra V3 does not support $$ as an escape character for enclosing string input, as $ is a reserved character for variable substitution in SnapLogic.
  • The Cassandra Snap Pack does not support the following data types introduced in Apache Cassandra V3.x, as the underlying SnapLogic JDBC driver is designed to work with Apache Cassandra V2.1:
    • DateRange

    • Duration

    • Geo-spatial data types such as Point, Polygon and LineString

Snap views

View Description Examples of upstream and downstream Snaps
Input This Snap has at most one document input view. If the input view is defined, then the where clause can substitute incoming values for a given expression.
  • Snaps that will either input data into Cassandra Execute, merge information with Cassandra, or required to precede execution before Cassandra execution.
Output This Snap has at most one document output view. If an output view is available and an update/insert/merge/delete statement was executed, then the original document that was used to create the statement will be output with the status of the statement executed.
  • Snaps that will use information gained from Cassandra or that is dependent on Cassandra having executed prior.
Error

Error handling is a generic way to handle errors without losing data or failing the Snap execution. You can handle the errors that the Snap might encounter when running the pipeline by choosing one of the following options from the When errors occur list under the Views tab. The available options are:

  • Stop Pipeline Execution Stops the current pipeline execution when an error occurs.
  • Discard Error Data and Continue Ignores the error, discards that record, and continues with the remaining records.
  • Route Error Data to Error View Routes the error data to an error view without stopping the Snap execution.

Learn more about Error handling in Pipelines.

Snap settings

Legend:
  • Expression icon (): Allows using pipeline parameters to set field values dynamically (if enabled). SnapLogic Expressions are not supported. If disabled, you can provide a static value.
  • SnapGPT (): Generates SnapLogic Expressions based on natural language using SnapGPT. Learn more.
  • Suggestion icon (): Populates a list of values dynamically based on your Snap configuration. You can select only one attribute at a time using the icon. Type into the field if it supports a comma-separated list of values.
  • Upload : Uploads files. Learn more.
Learn more about the icons in the Snap settings dialog.
Field / Field set Type Description
Label String

Required. Specify a unique name for the Snap. Modify this to be more appropriate, especially if more than one of the same Snaps is in the pipeline.

Default value: Cassandra - Execute

Example: Cassandra - Execute
SQL statement String/Expression
Required. Specifies the SQL statement to execute on the server.
Tip:

We recommend you to add a single query in the SQL Statement field.

Cassandra Snaps do not support batch operations, which is why this field does not support SQL bind variables in it. There are two possible scenarios that you encounter when working with SQL statements in SnapLogic.

Refer below : Scenarios to successfully execute your SQL statements.

Default value: N/A

Example: mydatabase
Query type Dropdown list/Expression

Select the type of query for your SQL statement (Read or Write).

When Auto is selected, the Snap tries to determine the query type automatically.If the execution result of the query is not as expected, you can change the query type to Read or Write.

Default value: Auto

Example: Read

Pass through Checkbox

If checked, the input document will be passed through to the output view under the key 'original'. This property applies only to the Execute Snaps with SELECT statement.

Default status: Selected

Ignore empty result Checkbox If selected, no document will be written to the output view when a SELECT operation does not produce any result. If this property is not selected and the Pass through property is selected, the input document will be passed through to the output view.

Default status: Deselected

Number of retries Integer/Expression Specify the maximum number of retry attempts the Snap must make in case of network failure.
When you set the Number of retries to more than 0, the Snap generates duplicate records when the connection is not established. To prevent duplicate records, we recommend that you follow one of the following:
  • Set the Number of retries to 0 (default value) to prevent duplicate records from being passed downstream while executing a pipeline.

  • Use a Primary key to prevent records from being inserted into the database.

  • Use an Upsert instead of an Insert statement.
Retry interval (seconds) Integer/Expression Number of seconds between retries.
Snap execution Dropdown list
Choose one of the three modes in which the Snap executes. Available options are:
  • Validate & Execute: Performs limited execution of the Snap and generates a data preview during pipeline validation. Subsequently, performs full execution of the Snap (unlimited records) during pipeline runtime.
  • Execute only: Performs full execution of the Snap during pipeline execution without generating preview data.
  • Disabled: Disables the Snap and all Snaps that are downstream from it.

Default value: Execute only

Example: Validate & Execute

Scenarios to successfully execute your SQL statements

Scenario 1: Executing SQL statements without expressions

If the expression toggle of the SQL statement field is not selected:
  • The SQL statement must not be within quotes.
  • The $<variable_name> parts of the SQL statement are expressions. In the below example, $id and $book.

Examples:



Additionally, the JSON path is allowed only in the WHERE clause. If the SQL statement starts with SELECT (case-insensitive), the Snap regards it as a select-type query and executes once per input document. If not, it regards it as write-type query and executes in batch mode.

Scenario 2: Executing SQL queries with expressions

If the expression toggle of the SQL statement field is selected:

The SQL statement must be within quotes.

  • The + $<variable_name> + parts of the SQL statement are expressions, and must not be within quotes.

Examples:



  • "EMPNO=$EMPNO and ENAME=$EMPNAME"
  • "emp='" + $emp + "'"
  • "EMPNO=" + $EMPNO + " and ENAME='" + $EMPNAME+ "'"
Warning: Using expressions that join strings together to create SQL queries or conditions has a potential SQL injection risk and hence unsafe. Ensure that you understand all implications and risks involved before using concatenation of strings with '=' Expression enabled.
Warning: Single quotes in values must be escaped

Any relational database (RDBMS) treats single quotes (') as special symbols. So, single quotes in the data or values passed through a DML query may cause the Snap to fail when the query is executed. Ensure that you pass two consecutive single quotes in place of one within these values to escape the single quote through these queries.

For example:

If String To pass this value Use
Has no single quotes Schaum Series 'Schaum Series'
Contains single quotes O'Reilly's Publication 'O''Reilly''s Publication'

This Snap does not allow you to inject SQL, such as select * from people where $columnName = "abc".

Only values can be substituted since it uses prepared statements for execution, which, for example, results in select * from people where address = ?.
Warning: The '$' sign and identifier characters, such as double quotes (“), single quotes ('), or back quotes (`), are reserved characters and should not be used in comments or for purposes other than their originally intended purpose.

Troubleshooting

  • Run Cassandra JDBC driver using another JDBC tool to verify syntax and results.