Skip to main content

sql_raw processor

Runs an arbitrary SQL query against a database and (optionally) returns the result as an array of objects, one for each row returned.

# Common config fields, showing default values
pipeline:
processors:
- label: ""
sql_raw:
driver: "" # No default (required)
dsn: "" # No default (required)
query: "" # No default (optional)
args_mapping: "" # No default (optional)
exec_only: false # No default (optional)
queries: [] # No default (optional)

If the query fails to execute then the message will remain unchanged and the error can be caught using error handling methods.

Examples

Table Insert (MySQL)

The following example inserts rows into the table footable with the columns foo, bar and baz populated with values extracted from messages.

pipeline:
processors:
- sql_raw:
driver: mysql
dsn: foouser:foopassword@tcp(localhost:3306)/foodb
query: "INSERT INTO footable (foo, bar, baz) VALUES (?, ?, ?);"
args_mapping: '[ document.foo, document.bar, meta("kafka_topic") ]'
exec_only: true

Table Query (PostgreSQL)

Here we query a database for columns of footable that share a user_id with the message field user.id. A branch processor is used in order to insert the resulting array into the original message at the path foo_rows.

pipeline:
processors:
- branch:
processors:
- sql_raw:
driver: postgres
dsn: postgres://foouser:foopass@localhost:5432/testdb?sslmode=disable
query: "SELECT * FROM footable WHERE user_id = $1;"
args_mapping: '[ this.user.id ]'
result_map: 'root.foo_rows = this'

Dynamically Creating Tables (PostgreSQL)

Here we query a database for columns of footable that share a user_id with the message field user.id. A branch processor is used in order to insert the resulting array into the original message at the path foo_rows.

pipeline:
processors:
- mapping: |
root = this
# Prevent SQL injection when using unsafe_dynamic_query
meta table_name = "\"" + metadata("table_name").replace_all("\"", "\"\"") + "\""
- sql_raw:
driver: postgres
dsn: postgres://localhost/postgres
unsafe_dynamic_query: true
queries:
- query: |
CREATE TABLE IF NOT EXISTS ${!metadata("table_name")} (id varchar primary key, document jsonb);
- query: |
INSERT INTO ${!metadata("table_name")} (id, document) VALUES ($1, $2)
ON CONFLICT (id) DO UPDATE SET document = EXCLUDED.document;
args_mapping: |
root = [ this.id, this.document.string() ]

Fields

driver

A database driver to use.

Type: string

Options: mysql, postgres, pgx, clickhouse, mssql, sqlite, oracle, snowflake, trino, gocosmos, spanner, databricks

dsn

A Data Source Name to identify the target database.

==== Drivers

The following is a list of supported drivers, their placeholder style, and their respective DSN formats:

|=== | Driver | Data Source Name Format

| clickhouse | [clickhouse://[username[:password\](https://github.com/ClickHouse/clickhouse-go#dsn)@\][netloc\][:port\]/dbname[?param1=value1&...&paramN=valueN\]^]

| mysql | [username[:password]@][protocol[(address)]]/dbname[?param1=value1&...&paramN=valueN]

| postgres and pgx | postgres://[user[:password]@][netloc][:port][/dbname][?param1=value1&...]

| mssql | sqlserver://[user[:password]@][netloc][:port][?database=dbname&param1=value1&...]

| sqlite | file:/path/to/filename.db[?param&=value1&...]

| oracle | oracle://[username[:password]@][netloc][:port]/service_name?server=server2&server=server3

| snowflake | username[:password]@account_identifier/dbname/schemaname[?param1=value&...&paramN=valueN]

| trino | [http[s\](https://github.com/trinodb/trino-go-client#dsn-data-source-name)://user[:pass\]@host[:port\][?parameters\]^]

| gocosmos | [AccountEndpoint=<cosmosdb-endpoint>;AccountKey=<cosmosdb-account-key>[;TimeoutMs=<timeout-in-ms>\](https://pkg.go.dev/github.com/microsoft/gocosmos#readme-example-usage)[;Version=<cosmosdb-api-version>\][;DefaultDb/Db=<db-name>\][;AutoId=<true/false>\][;InsecureSkipVerify=<true/false>\]^]

| spanner | projects/[PROJECT]/instances/[INSTANCE]/databases/[DATABASE]

| databricks | token:<access-token>@<server-hostname>:<port>/<http-path> |===

Please note that the postgres and pgx drivers enforce SSL by default, you can override this with the parameter sslmode=disable if required. The pgx driver is an alternative to the standard postgres (pq) driver and comes with extra functionality such as support for array insertion.

The snowflake driver supports multiple DSN formats. Please consult the docs for more details. For key pair authentication, the DSN has the following format: <snowflake_user>@<snowflake_account>/<db_name>/<schema_name>?warehouse=<warehouse>&role=<role>&authenticator=snowflake_jwt&privateKey=<base64_url_encoded_private_key>, where the value for the privateKey parameter can be constructed from an unencrypted RSA private key file rsa_key.p8 using openssl enc -d -base64 -in rsa_key.p8 | basenc --base64url -w0 (you can use gbasenc instead of basenc on OSX if you install coreutils via Homebrew). If you have a password-encrypted private key, you can decrypt it using openssl pkcs8 -in rsa_key_encrypted.p8 -out rsa_key.p8. Also, make sure fields such as the username are URL-encoded.

The gocosmos driver is still experimental, but it has support for hierarchical partition keys as well as cross-partition queries. Please refer to the SQL notes for details.

Type: string

query

The query to execute. The style of placeholder to use depends on the driver, some drivers require question marks (?) whereas others expect incrementing dollar signs ($1, $2, and so on) or colons (:1, :2 and so on). The style to use is outlined in this table:

DriverPlaceholder Style
clickhouseDollar sign
mysqlQuestion mark
postgresDollar sign
pgxDollar sign
mssqlQuestion mark
sqliteQuestion mark
oracleColon
snowflakeQuestion mark
trinoQuestion mark
gocosmosColon

Type: string

unsafe_dynamic_query

Whether to enable interpolation functions in the query. Great care should be made to ensure your queries are defended against injection attacks.

Type: bool
Default: false

args_mapping

An optional Bloblang mapping which should evaluate to an array of values matching in size to the number of placeholder arguments in the field query.

Type: string

exec_only

Whether the query result should be discarded. When set to true the message contents will remain unchanged, which is useful in cases where you are executing inserts, updates, etc. By default this is true for the last query, and previous queries don't change the results. If set to true for any query but the last one, the subsequent args_mappings input is overwritten.

Type: bool

queries

A list of statements to run in addition to query. When specifying multiple statements, they are all executed within a transaction. The output of the processor is always the last query that runs, unless exec_only is used.

Type: array of object

queries[].query

The query to execute. The style of placeholder to use depends on the driver, some drivers require question marks (?) whereas others expect incrementing dollar signs ($1, $2, and so on) or colons (:1, :2 and so on). The style to use is outlined in this table:

DriverPlaceholder Style
clickhouseDollar sign
mysqlQuestion mark
postgresDollar sign
pgxDollar sign
mssqlQuestion mark
sqliteQuestion mark
oracleColon
snowflakeQuestion mark
trinoQuestion mark
gocosmosColon

Type: string

queries[].args_mapping

An optional Bloblang mapping which should evaluate to an array of values matching in size to the number of placeholder arguments in the field query.

Type: string

queries[].exec_only

Whether the query result should be discarded. When set to true the message contents will remain unchanged, which is useful in cases where you are executing inserts, updates, etc. By default this is true for the last query, and previous queries don't change the results. If set to true for any query but the last one, the subsequent args_mappings input is overwritten.

Type: bool

init_files

An optional list of file paths containing SQL statements to execute immediately upon the first connection to the target database. This is a useful way to initialise tables before processing data. Glob patterns are supported, including super globs (double star).

Care should be taken to ensure that the statements are idempotent, and therefore would not cause issues when run multiple times after service restarts. If both init_statement and init_files are specified the init_statement is executed after the init_files.

If a statement fails for any reason a warning log will be emitted but the operation of this component will not be stopped.

Type: array of string

init_statement

An optional SQL statement to execute immediately upon the first connection to the target database. This is a useful way to initialise tables before processing data. Care should be taken to ensure that the statement is idempotent, and therefore would not cause issues when run multiple times after service restarts.

If both init_statement and init_files are specified the init_statement is executed after the init_files.

If the statement fails for any reason a warning log will be emitted but the operation of this component will not be stopped.

Type: string

conn_max_idle_time

An optional maximum amount of time a connection may be idle. Expired connections may be closed lazily before reuse. If value <= 0, connections are not closed due to a connections idle time.

Type: string

conn_max_life_time

An optional maximum amount of time a connection may be reused. Expired connections may be closed lazily before reuse. If value <= 0, connections are not closed due to a connections age.

Type: string

conn_max_idle

An optional maximum number of connections in the idle connection pool. If conn_max_open is greater than 0 but less than the new conn_max_idle, then the new conn_max_idle will be reduced to match the conn_max_open limit. If value <= 0, no idle connections are retained. The default max idle connections is currently 2. This may change in a future release.

Type: int
Default: 2

conn_max_open

An optional maximum number of open connections to the database. If conn_max_idle is greater than 0 and the new conn_max_open is less than conn_max_idle, then conn_max_idle will be reduced to match the new conn_max_open limit. If value <= 0, then there is no limit on the number of open connections. The default is 0 (unlimited).

Type: int