> ## Documentation Index
> Fetch the complete documentation index at: https://clickhouse.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

> Stream data into ClickHouse using NiFi data pipelines

# Connect Apache NiFi to ClickHouse

export const CommunityMaintainedBadge = () => {
  return <div className="CommunityMaintainedBadge">
            <div className="CommunityMaintainedIcon">
            <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="currentColor" viewBox="0 0 256 256">
                <path d="M244.8,150.4a8,8,0,0,1-11.2-1.6A51.6,51.6,0,0,0,192,128a8,8,0,0,1-7.37-4.89,8,8,0,0,1,0-6.22A8,8,0,0,1,192,112a24,24,0,1,0-23.24-30,8,8,0,1,1-15.5-4A40,40,0,1,1,219,117.51a67.94,67.94,0,0,1,27.43,21.68A8,8,0,0,1,244.8,150.4ZM190.92,212a8,8,0,1,1-13.84,8,57,57,0,0,0-98.16,0,8,8,0,1,1-13.84-8,72.06,72.06,0,0,1,33.74-29.92,48,48,0,1,1,58.36,0A72.06,72.06,0,0,1,190.92,212ZM128,176a32,32,0,1,0-32-32A32,32,0,0,0,128,176ZM72,120a8,8,0,0,0-8-8A24,24,0,1,1,87.24,82a8,8,0,1,0,15.5-4A40,40,0,1,0,37,117.51,67.94,67.94,0,0,0,9.6,139.19a8,8,0,1,0,12.8,9.61A51.6,51.6,0,0,1,64,128,8,8,0,0,0,72,120Z"></path>
            </svg>
        </div>
            Community Maintained
        </div>;
};

export const Image = ({img, alt, size = "lg"}) => {
  const normalizedSize = ["sm", "md", "lg"].includes(size) ? size : "lg";
  return <div className={`ch-image-${normalizedSize}`}>
      <Frame>
        <img src={img} alt={alt} />
      </Frame>
    </div>;
};

<CommunityMaintainedBadge />

<a href="https://nifi.apache.org/" target="_blank">Apache NiFi</a> is an open-source workflow management software designed to automate data flow between software systems. It allows the creation of ETL data pipelines and is shipped with more than 300 data processors. This step-by-step tutorial shows how to connect Apache NiFi to ClickHouse as both a source and destination, and to load a sample dataset.

<Steps>
  <Step title="Gather your connection details" id="1-gather-your-connection-details">
    To connect to ClickHouse with HTTP(S) you need this information:

    | Parameter(s)              | Description                                                                                                    |
    | ------------------------- | -------------------------------------------------------------------------------------------------------------- |
    | `HOST` and `PORT`         | Typically, the port is 8443 when using TLS or 8123 when not using TLS.                                         |
    | `DATABASE NAME`           | Out of the box, there is a database named `default`, use the name of the database that you want to connect to. |
    | `USERNAME` and `PASSWORD` | Out of the box, the username is `default`. Use the username appropriate for your use case.                     |

    The details for your ClickHouse Cloud service are available in the ClickHouse Cloud console.
    Select a service and click **Connect**:

    <div className="ch-image-md">
      <Frame>
        <img src="https://mintcdn.com/private-7c7dfe99/CFFsa2agBPbviR4r/images/_snippets/cloud-connect-button.webp?fit=max&auto=format&n=CFFsa2agBPbviR4r&q=85&s=ec0a298a33ca841e947fa5e8bae47362" alt="ClickHouse Cloud service connect button" width="998" height="932" data-path="images/_snippets/cloud-connect-button.webp" />
      </Frame>
    </div>

    Choose **HTTPS**. Connection details are displayed in an example `curl` command.

    <div className="ch-image-md">
      <Frame>
        <img src="https://mintcdn.com/private-7c7dfe99/CFFsa2agBPbviR4r/images/_snippets/connection-details-https.webp?fit=max&auto=format&n=CFFsa2agBPbviR4r&q=85&s=cb0fbd98aa2b5b7ca484c9f53395ee07" alt="ClickHouse Cloud HTTPS connection details" width="1320" height="1184" data-path="images/_snippets/connection-details-https.webp" />
      </Frame>
    </div>

    If you're using self-managed ClickHouse, the connection details are set by your ClickHouse administrator.
  </Step>

  <Step title="Download and run Apache NiFi" id="2-download-and-run-apache-nifi">
    For a new setup, download the binary from [https://nifi.apache.org/download.html](https://nifi.apache.org/download.html) and start by running `./bin/nifi.sh start`
  </Step>

  <Step title="Download the ClickHouse JDBC driver" id="3-download-the-clickhouse-jdbc-driver">
    1. Visit the <a href="https://github.com/ClickHouse/clickhouse-java/releases" target="_blank">ClickHouse JDBC driver release page</a> on GitHub and look for  the latest JDBC release version
    2. In the release version, click on "Show all xx assets" and look for the JAR file containing the keyword "shaded" or "all", for example, `clickhouse-jdbc-0.5.0-all.jar`
    3. Place the JAR file in a folder accessible by Apache NiFi and take note of the absolute path
  </Step>

  <Step title={<>Add <code>DBCPConnectionPool</code> Controller Service and configure its properties</>} id="4-add-dbcpconnectionpool-controller-service-and-configure-its-properties">
    1. To configure a Controller Service in Apache NiFi, visit the NiFi Flow Configuration page by clicking on the "gear" button

           <Image img="https://mintcdn.com/private-7c7dfe99/pIetLsS_hOGHqoPJ/images/integrations/data-ingestion/etl-tools/nifi_01.webp?fit=max&auto=format&n=pIetLsS_hOGHqoPJ&q=85&s=3ecb50806baacf0177cdde10d0bea34c" size="sm" border alt="NiFi Flow Configuration page with gear button highlighted" width="564" height="409" data-path="images/integrations/data-ingestion/etl-tools/nifi_01.webp" />

    2. Select the Controller Services tab and add a new Controller Service by clicking on the `+` button at the top right

           <Image img="https://mintcdn.com/private-7c7dfe99/pIetLsS_hOGHqoPJ/images/integrations/data-ingestion/etl-tools/nifi_02.webp?fit=max&auto=format&n=pIetLsS_hOGHqoPJ&q=85&s=e49f17a3ccf81855ddf6f33ae71f8b2d" size="lg" border alt="Controller Services tab with add button highlighted" width="1999" height="636" data-path="images/integrations/data-ingestion/etl-tools/nifi_02.webp" />

    3. Search for `DBCPConnectionPool` and click on the "Add" button

           <Image img="https://mintcdn.com/private-7c7dfe99/pIetLsS_hOGHqoPJ/images/integrations/data-ingestion/etl-tools/nifi_03.webp?fit=max&auto=format&n=pIetLsS_hOGHqoPJ&q=85&s=a214a72eb65b893a71426c349c6851a4" size="lg" border alt="Controller Service selection dialog with DBCPConnectionPool highlighted" width="1999" height="1287" data-path="images/integrations/data-ingestion/etl-tools/nifi_03.webp" />

    4. The newly added `DBCPConnectionPool` will be in an Invalid state by default. Click on the "gear" button to start configuring

           <Image img="https://mintcdn.com/private-7c7dfe99/pIetLsS_hOGHqoPJ/images/integrations/data-ingestion/etl-tools/nifi_04.webp?fit=max&auto=format&n=pIetLsS_hOGHqoPJ&q=85&s=5bc4d67491b1e5958664762878c41a87" size="lg" border alt="Controller Services list showing invalid DBCPConnectionPool with gear button highlighted" width="1893" height="607" data-path="images/integrations/data-ingestion/etl-tools/nifi_04.webp" />

    5. Under the "Properties" section, input the following values

    | Property                   | Value                                                                                    | Remark                                               |
    | -------------------------- | ---------------------------------------------------------------------------------------- | ---------------------------------------------------- |
    | Database Connection URL    | jdbc:ch:[https://HOSTNAME:8443/default?ssl=true](https://HOSTNAME:8443/default?ssl=true) | Replace HOSTNAME in the connection URL accordingly   |
    | Database Driver Class Name | com.clickhouse.jdbc.ClickHouseDriver                                                     |                                                      |
    | Database Driver Locations  | /etc/nifi/nifi-X.XX.X/lib/clickhouse-jdbc-0.X.X-patchXX-shaded.jar                       | Absolute path to the ClickHouse JDBC driver JAR file |
    | Database User              | default                                                                                  | ClickHouse username                                  |
    | Password                   | password                                                                                 | ClickHouse password                                  |

    6. In the Settings section, change the name of the Controller Service to "ClickHouse JDBC" for easy reference

           <Image img="https://mintcdn.com/private-7c7dfe99/pIetLsS_hOGHqoPJ/images/integrations/data-ingestion/etl-tools/nifi_05.webp?fit=max&auto=format&n=pIetLsS_hOGHqoPJ&q=85&s=869cce96c0ee8ccd527695c8dea21af7" size="lg" border alt="DBCPConnectionPool configuration dialog showing properties filled in" width="1586" height="1184" data-path="images/integrations/data-ingestion/etl-tools/nifi_05.webp" />

    7. Activate the `DBCPConnectionPool` Controller Service by clicking on the "lightning" button and then the "Enable" button

           <Image img="https://mintcdn.com/private-7c7dfe99/pIetLsS_hOGHqoPJ/images/integrations/data-ingestion/etl-tools/nifi_06.webp?fit=max&auto=format&n=pIetLsS_hOGHqoPJ&q=85&s=2460399dc3113d995786c373f7df4d37" size="lg" border alt="Controller Services list with lightning button highlighted" width="2878" height="812" data-path="images/integrations/data-ingestion/etl-tools/nifi_06.webp" />

           <br />

           <Image img="https://mintcdn.com/private-7c7dfe99/pIetLsS_hOGHqoPJ/images/integrations/data-ingestion/etl-tools/nifi_07.webp?fit=max&auto=format&n=pIetLsS_hOGHqoPJ&q=85&s=485f45409ead4ff9cfb316e8ee2bc8d9" size="lg" border alt="Enable Controller Service confirmation dialog" width="1586" height="1172" data-path="images/integrations/data-ingestion/etl-tools/nifi_07.webp" />

    8. Check the Controller Services tab and ensure that the Controller Service is enabled

           <Image img="https://mintcdn.com/private-7c7dfe99/pIetLsS_hOGHqoPJ/images/integrations/data-ingestion/etl-tools/nifi_08.webp?fit=max&auto=format&n=pIetLsS_hOGHqoPJ&q=85&s=b7d9e1ce84d3bbd46d913e940847a610" size="lg" border alt="Controller Services list showing enabled ClickHouse JDBC service" width="1999" height="636" data-path="images/integrations/data-ingestion/etl-tools/nifi_08.webp" />
  </Step>

  <Step title={<>Read from a table using the <code>ExecuteSQL</code> processor</>} id="5-read-from-a-table-using-the-executesql-processor">
    1. Add an ​`​ExecuteSQL` processor, along with the appropriate upstream and downstream processors

           <Image img="https://mintcdn.com/private-7c7dfe99/pIetLsS_hOGHqoPJ/images/integrations/data-ingestion/etl-tools/nifi_09.webp?fit=max&auto=format&n=pIetLsS_hOGHqoPJ&q=85&s=abf11db5bb1dc248a233669a23aef33e" size="md" border alt="NiFi canvas showing ExecuteSQL processor in a workflow" width="768" height="316" data-path="images/integrations/data-ingestion/etl-tools/nifi_09.webp" />

    2. Under the "Properties" section of the ​`​ExecuteSQL` processor, input the following values

       | Property                            | Value                         | Remark                                                  |
       | ----------------------------------- | ----------------------------- | ------------------------------------------------------- |
       | Database Connection Pooling Service | ClickHouse JDBC               | Select the Controller Service configured for ClickHouse |
       | SQL select query                    | SELECT \* FROM system.metrics | Input your query here                                   |

    3. Start the `​​ExecuteSQL` processor

           <Image img="https://mintcdn.com/private-7c7dfe99/pIetLsS_hOGHqoPJ/images/integrations/data-ingestion/etl-tools/nifi_10.webp?fit=max&auto=format&n=pIetLsS_hOGHqoPJ&q=85&s=81d02e73ee6ca1d543015bc0667eb442" size="lg" border alt="ExecuteSQL processor configuration with properties filled in" width="858" height="220" data-path="images/integrations/data-ingestion/etl-tools/nifi_10.webp" />

    4. To confirm that the query has been processed successfully, inspect one of the `FlowFile` in the output queue

           <Image img="https://mintcdn.com/private-7c7dfe99/pIetLsS_hOGHqoPJ/images/integrations/data-ingestion/etl-tools/nifi_11.webp?fit=max&auto=format&n=pIetLsS_hOGHqoPJ&q=85&s=270caae3211e7b81b50d1e309aba8606" size="lg" border alt="List queue dialog showing flowfiles ready for inspection" width="1956" height="794" data-path="images/integrations/data-ingestion/etl-tools/nifi_11.webp" />

    5. Switch view to "formatted" to view the result of the output `FlowFile`

           <Image img="https://mintcdn.com/private-7c7dfe99/pIetLsS_hOGHqoPJ/images/integrations/data-ingestion/etl-tools/nifi_12.webp?fit=max&auto=format&n=pIetLsS_hOGHqoPJ&q=85&s=1dc1c90d3bb71ab84465021653fddadf" size="lg" border alt="FlowFile content viewer showing query results in formatted view" width="1762" height="1500" data-path="images/integrations/data-ingestion/etl-tools/nifi_12.webp" />
  </Step>

  <Step title={<>Write to a table using <code>MergeRecord</code> and <code>PutDatabaseRecord</code> processor</>} id="6-write-to-a-table-using-mergerecord-and-putdatabaserecord-processor">
    1. To write multiple rows in a single insert, we first need to merge multiple records into a single record. This can be done using the `MergeRecord` processor

    2. Under the "Properties" section of the `MergeRecord` processor, input the following values

       | Property                  | Value               | Remark                                                                                                                 |
       | ------------------------- | ------------------- | ---------------------------------------------------------------------------------------------------------------------- |
       | Record Reader             | `JSONTreeReader`    | Select the appropriate record reader                                                                                   |
       | Record Writer             | `JSONReadSetWriter` | Select the appropriate record writer                                                                                   |
       | Minimum Number of Records | 1000                | Change this to a higher number so that the minimum number of rows are merged to form a single record. Default to 1 row |
       | Maximum Number of Records | 10000               | Change this to a higher number than "Minimum Number of Records". Default to 1,000 rows                                 |

    3. To confirm that multiple records are merged into one, examine the input and output of the `MergeRecord` processor. Note that the output is an array of multiple input records

       Input

           <Image img="https://mintcdn.com/private-7c7dfe99/pIetLsS_hOGHqoPJ/images/integrations/data-ingestion/etl-tools/nifi_13.webp?fit=max&auto=format&n=pIetLsS_hOGHqoPJ&q=85&s=2ccc4198a02b0b3b7402ee79e5ae12c8" size="sm" border alt="MergeRecord processor input showing single records" width="624" height="306" data-path="images/integrations/data-ingestion/etl-tools/nifi_13.webp" />

       Output

           <Image img="https://mintcdn.com/private-7c7dfe99/pIetLsS_hOGHqoPJ/images/integrations/data-ingestion/etl-tools/nifi_14.webp?fit=max&auto=format&n=pIetLsS_hOGHqoPJ&q=85&s=0b02a554dceeb6c594fd800d04dcfb89" size="sm" border alt="MergeRecord processor output showing merged array of records" width="707" height="501" data-path="images/integrations/data-ingestion/etl-tools/nifi_14.webp" />

    4. Under the "Properties" section of the `PutDatabaseRecord` processor, input the following values

       | Property                            | Value            | Remark                                                                                                                                    |
       | ----------------------------------- | ---------------- | ----------------------------------------------------------------------------------------------------------------------------------------- |
       | Record Reader                       | `JSONTreeReader` | Select the appropriate record reader                                                                                                      |
       | Database Type                       | Generic          | Leave as default                                                                                                                          |
       | Statement Type                      | INSERT           |                                                                                                                                           |
       | Database Connection Pooling Service | ClickHouse JDBC  | Select the ClickHouse controller service                                                                                                  |
       | Table Name                          | tbl              | Input your table name here                                                                                                                |
       | Translate Field Names               | false            | Set to "false" so that field names inserted must match the column name                                                                    |
       | Maximum Batch Size                  | 1000             | Maximum number of rows per insert. This value shouldn't be lower than the value of "Minimum Number of Records" in `MergeRecord` processor |

    5. To confirm that each insert contains multiple rows, check that the row count in the table is incrementing by at least the value of "Minimum Number of Records" defined in `MergeRecord`.

           <Image img="https://mintcdn.com/private-7c7dfe99/pIetLsS_hOGHqoPJ/images/integrations/data-ingestion/etl-tools/nifi_15.webp?fit=max&auto=format&n=pIetLsS_hOGHqoPJ&q=85&s=36b01a981712543038b0c3f5d2f06679" size="sm" border alt="Query results showing row count in the destination table" width="828" height="1306" data-path="images/integrations/data-ingestion/etl-tools/nifi_15.webp" />

    6. Congratulations - you have successfully loaded your data into ClickHouse using Apache NiFi !
  </Step>
</Steps>
