Substitute variables
Context¶
You have a set of SQL statements, and you want to vary the exact content depending on where you use them. To do that, ksqlDB supports variables so that you can supply different values.
In action¶
1 2 3 4 5 6 7 8 9 10 |
|
Use a variable¶
Begin by defining one or more variables with a compliant name:
1 2 |
|
Now reference each variable by enclosing it between ${}
characters:
1 2 3 4 5 6 7 |
|
Note
Variables are case-insensitive. A reference to ${replicas}
is the same as ${REPLICAS}
.
At any point, you can list all variables to see what is available:
1 |
|
Which should output:
1 2 3 4 5 |
|
If you want to undefine a variable, you can unbound the name like so:
1 |
|
Note
Single-quotes are removed during variable substitution. To escape single-quotes, enclose the value with triple-quotes.
Escape substitution variables¶
If you want to escape a variable reference so it is not substituted, use double $$
characters.
1 2 |
|
The above query will become SELECT '${format}' FROM stream
.
Context for substitution variables¶
Variable substitution is allowed in specific SQL statements. You can replace text and non-text literals, and identifiers like column names and stream/table names. You can't use variables as reserved keywords.
The following statements show examples of using variables for stream and column names, and in other places.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
Using a variable in a statement that doesn't support variables causes a SQL parsing error.
Disable substitution variables¶
Enable or disable variable substitution by setting the ksql.variable.substitution.enable
server configuration parameter.
1 2 3 |
|