This plugin provides support for interacting with SQL databases in Nextflow scripts.
The following databases are currently supported:
- AWS Athena (Setup guide here)
- DuckDB
- H2
- MySQL
- MariaDB
- PostgreSQL
- SQLite
This plugin requires Nextflow 26.04.0 or later. You can enable the plugin by adding the following snippet to your nextflow.config file:
plugins {
id 'nf-sqldb'
}Then define one or more databases under the sql.db configuration scope:
sql {
db {
foo {
url = 'jdbc:mysql://localhost:3306/demo'
user = 'my-user'
password = 'my-password'
}
}
}The sqlQuery function runs a query and returns its rows, while the sqlExecute function runs a statement that doesn't return rows, such as creating a table or inserting data.
Use sqlQuery to query a database. It returns a list of records, one for each row:
include { sqlQuery } from 'plugin/nf-sqldb'
channel.fromList(sqlQuery(db: 'foo', query: 'SELECT alpha, delta FROM sample WHERE omega > ?', params: [10]))
.view { row -> "${row.alpha} ${row.delta}" }The fromQuery channel factory is deprecated in favor of sqlQuery (see Migrating from fromQuery).
Use sqlInsert to insert each item of a channel into a table (deprecated, see Migrating from sqlInsert):
include { sqlInsert } from 'plugin/nf-sqldb'
channel
.of('Hello','world!')
.map { it -> tuple(it, it.size()) }
.sqlInsert( into: 'SAMPLE', columns: 'NAME, LEN', db: 'foo' )Use sqlExecute to run a statement that doesn't return a result set. It returns a map with success and either result or error:
include { sqlExecute } from 'plugin/nf-sqldb'
sqlExecute(db: 'foo', statement: 'CREATE TABLE IF NOT EXISTS sample_table (id INTEGER, name VARCHAR(100))')
// [success: true, result: null]
sqlExecute(db: 'foo', statement: 'INSERT INTO sample_table (id, name) VALUES (:id, :name)', params: [id: 1, name: 'alpha'])
// [success: true, result: 1]- Configuration: database options and the
defaultdatabase - Reference: all options for
sqlQuery,sqlExecute,fromQuery, andsqlInsert - Secrets: using Nextflow secrets for database credentials
- AWS Athena: querying AWS Athena
- Querying CSV files: querying CSV files with H2
See also the examples, the changelog, and the contributing guide.
This project is licensed under the Apache License 2.0.