github.com/Jeffail/benthos/v3@v3.65.0/website/docs/components/outputs/sql.md (about)

     1  ---
     2  title: sql
     3  type: output
     4  status: stable
     5  categories: ["Services"]
     6  ---
     7  
     8  <!--
     9       THIS FILE IS AUTOGENERATED!
    10  
    11       To make changes please edit the contents of:
    12       lib/output/sql.go
    13  -->
    14  
    15  import Tabs from '@theme/Tabs';
    16  import TabItem from '@theme/TabItem';
    17  
    18  
    19  Runs an SQL prepared query against a target database for each message.
    20  
    21  Introduced in version 3.33.0.
    22  
    23  
    24  <Tabs defaultValue="common" values={[
    25    { label: 'Common', value: 'common', },
    26    { label: 'Advanced', value: 'advanced', },
    27  ]}>
    28  
    29  <TabItem value="common">
    30  
    31  ```yaml
    32  # Common config fields, showing default values
    33  output:
    34    label: ""
    35    sql:
    36      driver: mysql
    37      data_source_name: ""
    38      query: ""
    39      args_mapping: ""
    40      max_in_flight: 1
    41      batching:
    42        count: 0
    43        byte_size: 0
    44        period: ""
    45        check: ""
    46  ```
    47  
    48  </TabItem>
    49  <TabItem value="advanced">
    50  
    51  ```yaml
    52  # All config fields, showing default values
    53  output:
    54    label: ""
    55    sql:
    56      driver: mysql
    57      data_source_name: ""
    58      query: ""
    59      args_mapping: ""
    60      max_in_flight: 1
    61      batching:
    62        count: 0
    63        byte_size: 0
    64        period: ""
    65        check: ""
    66        processors: []
    67  ```
    68  
    69  </TabItem>
    70  </Tabs>
    71  
    72  ## Alternatives
    73  
    74  For basic inserts use the [`sql_insert`](/docs/components/outputs/sql_insert) output instead. For more complex queries use the [`sql_raw`](/docs/components/outputs/sql_raw) output.
    75  
    76  ## Performance
    77  
    78  This output benefits from sending multiple messages in flight in parallel for
    79  improved performance. You can tune the max number of in flight messages with the
    80  field `max_in_flight`.
    81  
    82  This output benefits from sending messages as a batch for improved performance.
    83  Batches can be formed at both the input and output level. You can find out more
    84  [in this doc](/docs/configuration/batching).
    85  
    86  ## Examples
    87  
    88  <Tabs defaultValue="Table Insert (MySQL)" values={[
    89  { label: 'Table Insert (MySQL)', value: 'Table Insert (MySQL)', },
    90  { label: 'Table Insert (PostgreSQL)', value: 'Table Insert (PostgreSQL)', },
    91  ]}>
    92  
    93  <TabItem value="Table Insert (MySQL)">
    94  
    95  
    96  The following example inserts rows into the table footable with the columns foo,
    97  bar and baz populated with values extracted from messages:
    98  
    99  ```yaml
   100  output:
   101    sql:
   102      driver: mysql
   103      data_source_name: foouser:foopassword@tcp(localhost:3306)/foodb
   104      query: "INSERT INTO footable (foo, bar, baz) VALUES (?, ?, ?);"
   105      args_mapping: '[ this.document.foo, this.document.bar, meta("kafka_topic") ]'
   106      batching:
   107        count: 500
   108  ```
   109  
   110  </TabItem>
   111  <TabItem value="Table Insert (PostgreSQL)">
   112  
   113  
   114  The following example inserts rows into the table footable with the columns foo,
   115  bar and baz populated with values extracted from messages:
   116  
   117  ```yaml
   118  output:
   119    sql:
   120      driver: postgres
   121      data_source_name: postgres://foouser:foopassword@localhost:5432/foodb?sslmode=disable
   122      query: "INSERT INTO footable (foo, bar, baz) VALUES ($1, $2, $3);"
   123      args_mapping: '[ this.document.foo, this.document.bar, meta("kafka_topic") ]'
   124      batching:
   125        count: 500
   126  ```
   127  
   128  </TabItem>
   129  </Tabs>
   130  
   131  ## Fields
   132  
   133  ### `driver`
   134  
   135  A database [driver](#drivers) to use.
   136  
   137  
   138  Type: `string`  
   139  Default: `"mysql"`  
   140  Options: `mysql`, `postgres`, `clickhouse`, `mssql`.
   141  
   142  ### `data_source_name`
   143  
   144  A Data Source Name to identify the target database.
   145  
   146  
   147  Type: `string`  
   148  Default: `""`  
   149  
   150  ```yaml
   151  # Examples
   152  
   153  data_source_name: tcp://host1:9000?username=user&password=qwerty&database=clicks&read_timeout=10&write_timeout=20&alt_hosts=host2:9000,host3:9000
   154  
   155  data_source_name: foouser:foopassword@tcp(localhost:3306)/foodb
   156  
   157  data_source_name: postgres://foouser:foopass@localhost:5432/foodb?sslmode=disable
   158  ```
   159  
   160  ### `query`
   161  
   162  The query to run against the database.
   163  
   164  
   165  Type: `string`  
   166  Default: `""`  
   167  
   168  ```yaml
   169  # Examples
   170  
   171  query: INSERT INTO footable (foo, bar, baz) VALUES (?, ?, ?);
   172  ```
   173  
   174  ### `args_mapping`
   175  
   176  A [Bloblang mapping](/docs/guides/bloblang/about) that produces the arguments for the query. The mapping must return an array containing the number of arguments in the query.
   177  
   178  
   179  Type: `string`  
   180  Default: `""`  
   181  Requires version 3.47.0 or newer  
   182  
   183  ```yaml
   184  # Examples
   185  
   186  args_mapping: '[ this.foo, this.bar.not_empty().catch(null), meta("baz") ]'
   187  
   188  args_mapping: root = [ uuid_v4() ].merge(this.document.args)
   189  ```
   190  
   191  ### `max_in_flight`
   192  
   193  The maximum number of messages to have in flight at a given time. Increase this to improve throughput.
   194  
   195  
   196  Type: `int`  
   197  Default: `1`  
   198  
   199  ### `batching`
   200  
   201  Allows you to configure a [batching policy](/docs/configuration/batching).
   202  
   203  
   204  Type: `object`  
   205  
   206  ```yaml
   207  # Examples
   208  
   209  batching:
   210    byte_size: 5000
   211    count: 0
   212    period: 1s
   213  
   214  batching:
   215    count: 10
   216    period: 1s
   217  
   218  batching:
   219    check: this.contains("END BATCH")
   220    count: 0
   221    period: 1m
   222  ```
   223  
   224  ### `batching.count`
   225  
   226  A number of messages at which the batch should be flushed. If `0` disables count based batching.
   227  
   228  
   229  Type: `int`  
   230  Default: `0`  
   231  
   232  ### `batching.byte_size`
   233  
   234  An amount of bytes at which the batch should be flushed. If `0` disables size based batching.
   235  
   236  
   237  Type: `int`  
   238  Default: `0`  
   239  
   240  ### `batching.period`
   241  
   242  A period in which an incomplete batch should be flushed regardless of its size.
   243  
   244  
   245  Type: `string`  
   246  Default: `""`  
   247  
   248  ```yaml
   249  # Examples
   250  
   251  period: 1s
   252  
   253  period: 1m
   254  
   255  period: 500ms
   256  ```
   257  
   258  ### `batching.check`
   259  
   260  A [Bloblang query](/docs/guides/bloblang/about/) that should return a boolean value indicating whether a message should end a batch.
   261  
   262  
   263  Type: `string`  
   264  Default: `""`  
   265  
   266  ```yaml
   267  # Examples
   268  
   269  check: this.type == "end_of_transaction"
   270  ```
   271  
   272  ### `batching.processors`
   273  
   274  A list of [processors](/docs/components/processors/about) to apply to a batch as it is flushed. This allows you to aggregate and archive the batch however you see fit. Please note that all resulting messages are flushed as a single batch, therefore splitting the batch into smaller batches using these processors is a no-op.
   275  
   276  
   277  Type: `array`  
   278  Default: `[]`  
   279  
   280  ```yaml
   281  # Examples
   282  
   283  processors:
   284    - archive:
   285        format: lines
   286  
   287  processors:
   288    - archive:
   289        format: json_array
   290  
   291  processors:
   292    - merge_json: {}
   293  ```
   294  
   295