Skip to main content

Oracle Golden Gate to Apache Kafka to Apache NiFi to JDBC Data Sink

Oracle -> GoldenGate -> Apache Kafka -> Apache NiFi / Hortonworks Schema Registry -> JDBC Database
Sometimes you need to process any number of table changes sent from tools via Apache Kafka. As long as they have proper header data and records in JSON, it's really easy in Apache NiFi.
  1. Process Each Partition Separately
  2. Process Records in Order as each message is an Insert, Update or Delete to an existing table in our receiving JDBC store.
  3. Re-process if data lost
For The Main Processor for Routing, It must only run on the Primary Node.
Enforcing Order
We use the Kafka.Offset to order the records, which makes sense in Apache Kafka topics.
After Insert, Update, Delete queries are built, let's confirm and enforce that strict ordering.
To further confirm processing in order, we make each connection in the flow FirstInFirstOutPrioritizer.

To Route, We Route Each Partition to A Different Processor Group (One Local, The Other Remote)
Let's Store Some Data in HDFS for each Table
Connect To Kafka and Grab From our Topic
Let's Connect to our JDBC Store
Let's do an Update (Table Name is Dynamic)
The Jolt Processor has an awesome tester for trying out Jolt

Make sure we connect our remote partitions

Routing From Routing Server (Primary Node)
For Processing Partition 0 (Run on the Routing Server)
We infer the schema with our InferAvroSchema, so we don't need to know the embedded table layouts before a record arrives. In production it makes sense to know all these in advance and do integration tests and versioning of schemas. This is where Hortonworks Scheme Registry is awesome. We name the avro record after the table dynamically. We can get and store permanent schema in the Hortonworks Schema Registry.
Process The Next Partition 1 .. (We can have one server or cluster per partition)
Process the Partition 1 Kafka Records from the Topic

This Flow Will Convert Our Embedded JSON Table Record into New SQL
Input: {"ID":2001,"GD":"F","DPTID":2,"FIRSTNAME":"Tim","LAST":"Spann"}
sql.args.5.value Spann
sql.table THETABLE
With all the field being parameters for a SQL Injection safe parameter based insert, update or delete based on control sent.
Golden Gate Messages
{"table": "SCHEMA1.TABLE7","op_type": "I","op_ts": "2017-11-01 04:31:56.000000","current_ts": "2017-11-01T04:32:04.754000","pos": "00000000310000020884","after": {"ID":1,"CODE": "B","NAME":"STUFF","DESCR" :"Department","ACTIVE":1}}
Using a simple EvaluateJsonPath we pull out these control fields, example: $.before.
The Table Name for ConvertJSONtoSQL${table:substringAfter('.')}. This is to remove all leading schema / tablespace name. From the drop down for each of the three we pick either UPDATEINSERT or DELETE based on the op_type.
We follow this with a PutSQL which will execute on our destination JDBC database sink.
After that I collect all the attributes convert them to a JSON flowfile and save that to HDFS for logging and reporting. This step could be skipped or could be in another format or sent elsewhere.
Control Fields
pos: position
table: table to update in the data warehouse
current_ts: time stamp
op_ts: time stamp
op_type: operation type (I – insert, U- update, D – delete)
Important Apache NiFi System Fields
We can Route and process these for special handling.
To Create HDFS Directories for Changes
  1. su hdfs <br>hdfs dfs -mkdir -p /new/T1 <br>hdfs dfs -mkdir -p /new/T2 <br>hdfs dfs -mkdir -p /poc/T3
  2. hdfs dfs -chmod -R 777 /new <br>hdfs dfs -ls -R /new
To Create a Test Apache Kafka Topic
  1. ./bin/ --create \
  2. --zookeeper localhost:2181 \
  3. --replication-factor 1 \
  4. --partitions 2 \
  5. --topic goldengate

Creating a MYSQL Database As Recipient JDBC Server
  1. wget
  2. mysql
  3. create database mydw;
  4. CREATE USER 'nifi'@'%' IDENTIFIED BY 'MyPassWordIsSoAwesome!!!!';
  6. commit;
  7. SHOW GRANTS FOR 'nifi'@'%';
  10. #Create some tables in the database for your records.
  13. create table ALOG (
  14. AID VARCHAR(1),
  18. );
Jolt Filter
Attribute: afterJolt
${op_type:equalsIgnoreCase("D"):ifElse("none", "after")}
Attribute: beforeJolt
${op_type:equalsIgnoreCase("D"):ifElse("before", "none")}
Jolt Script to Transform JSON
  1. [ {
  2. "operation": "shift",
  3. "spec": {
  4. "${beforeJolt}": {
  5. "*": "&"
  6. },
  7. "${afterJolt}": {
  8. "*": "&"
  9. }
  10. }
  11. }, {
  12. "operation": "shift",
  13. "spec": {
  14. "*": "&"
  15. }
  16. } ]

Primary Node Flow Template

Partition X Node Flow Template


Popular posts from this blog

Ingesting Drone Data From DJII Ryze Tello Drones Part 1 - Setup and Practice

Ingesting Drone Data From DJII Ryze Tello Drones Part 1 - Setup and Practice In Part 1, we will setup our drone, our communication environment, capture the data and do initial analysis. We will eventually grab live video stream for object detection, real-time flight control and real-time data ingest of photos, videos and sensor readings. We will have Apache NiFi react to live situations facing the drone and have it issue flight commands via UDP. In this initial section, we will control the drone with Python which can be triggered by NiFi. Apache NiFi will ingest log data that is stored as CSV files on a NiFi node connected to the drone's WiFi. This will eventually move to a dedicated embedded device running MiniFi. This is a small personal drone with less than 13 minutes of flight time per battery. This is not a commercial drone, but gives you an idea of the what you can do with drones. Drone Live Communications for Sensor Readings and Drone Control You must connect t

NiFi on Cloudera Data Platform Upgrade - April 2021

CFM 2.1.1 on CDP 7.1.6 There is a new Cloudera release of Apache NiFi now with SAML support. Apache NiFi Apache NiFi Registry See:   For changes: Get your download on: To start researching for the future, take a look at some of the technical preview features around Easy Rules engine and handlers. Make sure you use the latest possible JDK 8 as there are some bugs out there.   Use a recent v

Using Apache NiFi in OpenShift and Anywhere Else to Act as Your Global Integration Gateway

Using Apache NiFi in OpenShift and Anywhere Else to Act as Your Global Integration Gateway What does it look like? Where Can I Run This Magic Engine: Private Cloud, Public Cloud, Hybrid Cloud, VM, Bare Metal, Single Node, Laptop, Raspberry Pi or anywhere you have a 1GB of RAM and some CPU is a good place to run a powerful graphical integration and dataflow engine.   You can also run MiNiFi C++ or Java agents if you want it even smaller. Sounds Too Powerful and Expensive: Apache NiFi is Open Source and can be run freely anywhere. For What Use Cases: Microservices, Images, Deep Learning and Machine Learning Models, Structured Data, Unstructured Data, NLP, Sentiment Analysis, Semistructured Data, Hive, Hadoop, MongoDB, ElasticSearch, SOLR, ETL/ELT, MySQL CDC, MySQL Insert/Update/Delete/Query, Hosting Unlimited REST Services, Interactive with Websockets, Ingesting Any REST API, Natively Converting JSON/XML/CSV/TSV/Logs/Avro/Parquet, Excel, PDF, Word Documents, Syslog, Kafka, JMS, MQTT, TCP