Simple Change Data Capture (CDC) with SQL Selects via Apache NiFi (FLaNK)
Published: 2020-12-23 17:14:23

---
title: Simple Change Data Capture (CDC) with SQL Selects via Apache NiFi (FLaNK)
published: true
date: 2020-12-23 17:12:00 UTC
tags: 
canonical_url: 
---

##  Simple Change Data Capture (CDC) with SQL Selects via Apache NiFi (FLaNK)

[![](https://1.bp.blogspot.com/-pwTzyAz1xQU/X-N09JBwQXI/AAAAAAAAcKA/SWtuKVvB6AkplNstt_asywOZRFcAeb98gCLcBGAsYHQ/w679-h480/allflow.png)](https://1.bp.blogspot.com/-pwTzyAz1xQU/X-N09JBwQXI/AAAAAAAAcKA/SWtuKVvB6AkplNstt_asywOZRFcAeb98gCLcBGAsYHQ/s2048/allflow.png)
  

Sometimes you need real CDC and you have access to transaction change logs and you use a tool like QLIK REPLICATE or GoldenGate to pump out records to Kafka and then Flink SQL or NiFi can read them and process them.

Other times you need something easier for just some basic changes and inserts to some tables you are interested in receiving new data as events.   Apache NiFi can do this easily for you with QueryDatabaseTableRecord, you don't need to know anything but the database connection information, table name and what field may change.  NiFi will query, watch state and give you new records.   Nothing is hardcoded, parameterize those values and you have a generic Any RDBMS to Any Other Store data pipeline.   We are reading as records which means each FlowFile in NiFi can have thousands of records that we know all the fields, types and schema related information for.   This can be ones that NiFi infers the schema or ones we use from a Schema Registry like Cloudera's amazing Open Source Schema Registry.

Let's see what data is in our Postgresql table?

[![](https://1.bp.blogspot.com/-eb63XDnao3Y/X-N26mcxMII/AAAAAAAAcLI/hJuOv1Eh1dQLAof_MsdmOmYeHGLbT_HHgCLcBGAsYHQ/w640-h492/postgresqldata.png)](https://1.bp.blogspot.com/-eb63XDnao3Y/X-N26mcxMII/AAAAAAAAcLI/hJuOv1Eh1dQLAof_MsdmOmYeHGLbT_HHgCLcBGAsYHQ/s2020/postgresqldata.png)

## **How to **

1. **QueryDatabaseTableRecord (**we will output Json records, but could have done Parquet, XML, CSV or AVRO)
2. **UpdateAttribute** - optional - set a table and schema name, can do with parameters as well.
3. **MergeRecord** - optional - let's batch these up.
4. **PutORC** - let's send these records to HDFS (which could be on bare metal disks, GCS, S3, Azure or ADLS).   This will build us an external hive table.

[![](https://1.bp.blogspot.com/-IYWe_2ImRHs/X-N1Ga16l0I/AAAAAAAAcKE/ZKPzQiwA7Po-aXRj5gmmfqKtv7LWI9H4QCLcBGAsYHQ/w618-h989/queryToStorage.png)](https://1.bp.blogspot.com/-IYWe_2ImRHs/X-N1Ga16l0I/AAAAAAAAcKE/ZKPzQiwA7Po-aXRj5gmmfqKtv7LWI9H4QCLcBGAsYHQ/s1373/queryToStorage.png)

  
  

  

  

**PutORC**
  

[![](https://1.bp.blogspot.com/-6cUN2E7loKM/X-N2U4-KYYI/AAAAAAAAcKk/PPkE0ZsqDiMBvjxuxdxWzJb7Ps0irF_QACLcBGAsYHQ/w640-h400/putorc.png)](https://1.bp.blogspot.com/-6cUN2E7loKM/X-N2U4-KYYI/AAAAAAAAcKk/PPkE0ZsqDiMBvjxuxdxWzJb7Ps0irF_QACLcBGAsYHQ/s1061/putorc.png)
[![](https://1.bp.blogspot.com/-SmDqpVy_rZ8/X-N2U8CvVBI/AAAAAAAAcKc/8AwnJF6qmwscVj85D9hwQux6Pk6y9tJbACLcBGAsYHQ/w640-h154/putorc2.png)](https://1.bp.blogspot.com/-SmDqpVy_rZ8/X-N2U8CvVBI/AAAAAAAAcKc/8AwnJF6qmwscVj85D9hwQux6Pk6y9tJbACLcBGAsYHQ/s1095/putorc2.png)
  

As you can see we are looking at the "prices" table and checking maximum values to increment on the updated\_on date and the item\_id sequential key.  We then output JSON records.

  

[![](https://1.bp.blogspot.com/--G4d5KNw7XE/X-N2VZWJoZI/AAAAAAAAcKo/aUDDaxrsf4gUL0-Qp_OwJJ0GlKCFslo0ACLcBGAsYHQ/w640-h382/queryDatabaseRecord.png)](https://1.bp.blogspot.com/--G4d5KNw7XE/X-N2VZWJoZI/AAAAAAAAcKo/aUDDaxrsf4gUL0-Qp_OwJJ0GlKCFslo0ACLcBGAsYHQ/s1135/queryDatabaseRecord.png)
  

  

  

We could then:

### **Add-Ons Examples**

1. **PutKudu**
2. **PutHDFS** (send as JSON, CSV, Parquet) and build an Impala or Hive table on top as external
3. **PutHive3Streaming** (Hive 3 ACID Tables)
4. **PutS3**
5. **PutAzureDataLakeStorage**
6. **PutHBaseRecord**
7. **PublishKafkaRecord\_2\_** \* - send a copy to Kafka for Flink SQL, Spark Streaming, Spring, etc...
8. **PutBigQueryStreaming** (Google)
9. **PutCassandraRecord**
10. **PutDatabaseRecord** - let's send to another JDBC Datastore
11. **PutDruidRecord** - Druid is a cool datastore, check it out on CDP Public Cloud
12. **PutElasticSearchRecord**
13. **PutMongoRecord**
14. **PutSolrRecord**
15. **PutRecord (**to many RecordSinkServices like Databases, Kafka, Prometheus, Scripted and Site-to-Site)
16. **PutParquet** (store to HDFS as Parquet files)

You can do any number or all of these or multiple copies of each to other clouds or clusters.    You can also enrichment, transformation, alerts, queries or routing.

These records can be also manipulated ETL/ELT style with Record processing in stream with options such as:

1. **QueryRecord (**use Calcite ANSI SQL to query and transform records and can also change output type)
2. **JoltTransformRecord** (use JOLT against any record not just JSON)
3. **LookupRecord** (to match against Lookup services like caches, Kudu, REST services, ML models, HBase and more)
4. **PartitionRecord** (to break up into like groups)
5. **SplitRecord** (to break up record groups into records)
6. **UpdateRecord** (update values in fields, often paired with LookupRecord)
7. **ValidateRecord** (check against a schema and check for extra fields)
8. **GeoEnrichIPRecord**
9.   **ConvertRecord** (change between types like JSON to CSV)  

When you use PutORC, it will give you the details on building your external table.   You can do a PutHiveQL to auto-build this table, but most companies want this done by a DBA.

CREATE EXTERNAL TABLE IF NOT EXISTS `pricesorc` (`item_id` BIGINT, `price` DOUBLE, `created_on` BIGINT, `updated_on` BIGINT)   
STORED AS ORC  
LOCATION '/user/tspann/prices'

[![](https://1.bp.blogspot.com/-wEZbiCDMUa8/X-N27nfZGMI/AAAAAAAAcLU/-yW4LvZst44U7f2ppfKeCPxA1LcXJH49wCLcBGAsYHQ/w640-h220/tableoutput.png)](https://1.bp.blogspot.com/-wEZbiCDMUa8/X-N27nfZGMI/AAAAAAAAcLU/-yW4LvZst44U7f2ppfKeCPxA1LcXJH49wCLcBGAsYHQ/s1478/tableoutput.png)

[![](https://1.bp.blogspot.com/-XhidJ4pg9wI/X-N2U4Xs5UI/AAAAAAAAcKg/tS53YxOJAdQeMEFjzEmBuOKx__tbdf--ACLcBGAsYHQ/w640-h156/createhivetable.png)](https://1.bp.blogspot.com/-XhidJ4pg9wI/X-N2U4Xs5UI/AAAAAAAAcKg/tS53YxOJAdQeMEFjzEmBuOKx__tbdf--ACLcBGAsYHQ/s2008/createhivetable.png)

## Part 2

#### REST to Database

Let's reverse this now.   Sometimes you want to take data, say from a REST service and store it to a JDBC datastore.

1. **InvokeHTTP (**read from a REST endpoint)
2. **PutDatabaseRecord (**put JSON to our JDBC store).

That's it to store data to a database.  We could add some of the ETL/ELT enrichments mentioned above 

or others that manipulate content.

  

[![](https://1.bp.blogspot.com/-fm-7dY3Kb8o/X-N1L9qiclI/AAAAAAAAcKI/1aLI3wvWn2QvzabI6D1zD3pk2QQi8BwoACLcBGAsYHQ/w658-h496/resttodb.png)](https://1.bp.blogspot.com/-fm-7dY3Kb8o/X-N1L9qiclI/AAAAAAAAcKI/1aLI3wvWn2QvzabI6D1zD3pk2QQi8BwoACLcBGAsYHQ/s1304/resttodb.png)

  

  

**REST Output**
  

[![](https://1.bp.blogspot.com/-VUK8bVzZKHk/X-N26y-OpGI/AAAAAAAAcLM/3thtlY6QZkw-AhYmJgbW6Jou5NaO7seEwCLcBGAsYHQ/w508-h1279/restdata.png)](https://1.bp.blogspot.com/-VUK8bVzZKHk/X-N26y-OpGI/AAAAAAAAcLM/3thtlY6QZkw-AhYmJgbW6Jou5NaO7seEwCLcBGAsYHQ/s1595/restdata.png)
  

  

**Database Connection Pool**  

[![](https://1.bp.blogspot.com/-uouPOUIOdtE/X-N26umoraI/AAAAAAAAcLA/V8Vyw7AZEcotuq1DMW_gB_7EHOpCI8W2gCLcBGAsYHQ/w640-h524/jdbcconnectionpool.png)](https://1.bp.blogspot.com/-uouPOUIOdtE/X-N26umoraI/AAAAAAAAcLA/V8Vyw7AZEcotuq1DMW_gB_7EHOpCI8W2gCLcBGAsYHQ/s914/jdbcconnectionpool.png)

  

**Get the REST Data**  

[![](https://1.bp.blogspot.com/-oQ82qq_JvPw/X-N26jQ7fGI/AAAAAAAAcLE/dRexPZXczncvjLkxTsPe9NNbZETIkm2cgCLcBGAsYHQ/w640-h412/invokeHttp.png)](https://1.bp.blogspot.com/-oQ82qq_JvPw/X-N26jQ7fGI/AAAAAAAAcLE/dRexPZXczncvjLkxTsPe9NNbZETIkm2cgCLcBGAsYHQ/s1038/invokeHttp.png)
  

**PutDatabaseRecord**

  

[![](https://1.bp.blogspot.com/-Ki54VEEekmI/X-N30QwIOjI/AAAAAAAAcLs/xYmyrAzjcEY_RYbVgMdCuKwHrG1gbIE4wCLcBGAsYHQ/w640-h360/putDatabaserecord.png)](https://1.bp.blogspot.com/-Ki54VEEekmI/X-N30QwIOjI/AAAAAAAAcLs/xYmyrAzjcEY_RYbVgMdCuKwHrG1gbIE4wCLcBGAsYHQ/s1031/putDatabaserecord.png)

  

  

From ApacheCon 2020, John Kuchmek does a great talk on **Incrementally Streaming RDBMS Data.**

  

<iframe allowfullscreen="" height="513" src="https://www.youtube.com/embed/nPewLy7Zjkg" width="617" youtube-src-id="nPewLy7Zjkg"></iframe>
  

[Incrementally Streaming Slides](https://www2.slideshare.net/bunkertor/incrementally-streaming-rdbms-data-to-your-data-lake-automagically?ref=)

  

## Resources

- [https://www.datainmotion.dev/2019/10/migrating-apache-flume-flows-to-apache\_15.html](https://dev.to/tspannhw/migrating-apache-flume-flows-to-apache-nifi-any-relational-database-to-from-anywhere-45d1)
- [https://github.com/tspannhw/EverythingApacheNiFi/blob/main/README.md](https://github.com/tspannhw/EverythingApacheNiFi/blob/main/README.md)
- [https://www.youtube.com/watch?v=XsL63ZQYmLE](https://www.youtube.com/watch?v=XsL63ZQYmLE)
- [https://community.cloudera.com/t5/Community-Articles/Change-Data-Capture-CDC-with-Apache-NiFi-Part-1-of-3/ta-p/246623](https://community.cloudera.com/t5/Community-Articles/Change-Data-Capture-CDC-with-Apache-NiFi-Part-1-of-3/ta-p/246623)
- [https://nifi.apache.org/docs/nifi-docs/components/org.apache.nifi/nifi-cdc-mysql-nar/1.5.0/org.apache.nifi.cdc.mysql.processors.CaptureChangeMySQL/](https://nifi.apache.org/docs/nifi-docs/components/org.apache.nifi/nifi-cdc-mysql-nar/1.5.0/org.apache.nifi.cdc.mysql.processors.CaptureChangeMySQL/)
- [https://dzone.com/articles/change-data-capture-using-apache-nifi](https://dzone.com/articles/change-data-capture-using-apache-nifi)
- [https://www.linkedin.com/pulse/building-near-real-time-big-data-lake-part-2-boris-tyukin/](https://www.linkedin.com/pulse/building-near-real-time-big-data-lake-part-2-boris-tyukin/)
- [https://www.qlik.com/us/data-management/nifi](https://www.qlik.com/us/data-management/nifi)
- [https://www.cloudera.com/tutorials/cdp-importing-rdbms-data-into-hive.html](https://www.cloudera.com/tutorials/cdp-importing-rdbms-data-into-hive.html)
- [https://www.cdata.com/kb/tech/oracledb-jdbc-apache-nifi.rst](https://www.cdata.com/kb/tech/oracledb-jdbc-apache-nifi.rst)
- [https://nathanlabadie.com/apache-nifi-ms-sql-and-kerberos-authentication/](https://nathanlabadie.com/apache-nifi-ms-sql-and-kerberos-authentication/)
- [https://dzone.com/articles/lets-build-a-simple-ingest-to-cloud-data-warehouse](https://dzone.com/articles/lets-build-a-simple-ingest-to-cloud-data-warehouse)
- [https://github.com/tspannhw/EverythingApacheNiFi#etl--elt--cdc--load--ingest](https://github.com/tspannhw/EverythingApacheNiFi#etl--elt--cdc--load--ingest)
- [https://www.linkedin.com/pulse/2020-streaming-edge-ai-events-tim-spann/](https://www.linkedin.com/pulse/2020-streaming-edge-ai-events-tim-spann/)
- [https://github.com/tspannhw/ApacheConAtHome2020](https://github.com/tspannhw/ApacheConAtHome2020)

  

  

That's it.   **Happy Holidays!**

  

[![](https://1.bp.blogspot.com/-a8x7qNX_itc/X-N4FT0JWNI/AAAAAAAAcL0/09XNWsN0tG4znSsPJa3lWuPKqOiUbRuSgCLcBGAsYHQ/w480-h640/VBsviFlXSQmX9h6f78vFeQ.jpeg)](https://1.bp.blogspot.com/-a8x7qNX_itc/X-N4FT0JWNI/AAAAAAAAcL0/09XNWsN0tG4znSsPJa3lWuPKqOiUbRuSgCLcBGAsYHQ/s1696/VBsviFlXSQmX9h6f78vFeQ.jpeg)