Created on 03-16-2018 12:16 AM - edited 08-17-2019 08:21 AM
NiFi JSON to DDL Custom Processor
Java Class
JUnit
This is further enhanced version of the idea started here: https://community.hortonworks.com/articles/154957/converting-json-to-sql-ddl.html?es_p=6294995
There was some discussion on linkedin about the previous article being a good processor, so I decided to do that. This is pretty basic, but it handles most types okay. Date and number processing is a bit hacky, but guesses some types.
To install, copy the NAR file that you build or download from Github to your NiFi/lib directories and restart those servers.
Add the New Processor to Your Flow
Configure the Processor with a table type (that is ignored in this version)
Configure the Processor with a table name (this is important)
JsonToDDLProcessor Generated Docs
I configured my table name to be the filename without an extension for JSON
Output in NiFi
Example Flow
Enhancements In Consideration:
Call to the community, if this is interesting, please join. If you don't want to code, please suggest enhancements, open tickets on bugs, spread the word. Thanks.
Source Code:
https://github.com/tspannhw/nifi-convertjsontoddl-processor
mvn archetype:generate
Install the Pre-Built Nar
https://github.com/tspannhw/nifi-convertjsontoddl-processor/releases/tag/v1.0
Test JSON Files
Table Create DDL
generatedddl
CREATE TABLE simple ( EMPID INT, GENDER CHAR(1), DEPTID INT, FIRSTNAME VARCHAR(17), LASTNAME VARCHAR(15), TOTALSPENT INT )
generatedddl
CREATE TABLE complex ( EMPID INT, GENDER CHAR(1), DEPTID INT, FIRSTNAME VARCHAR(17), LASTNAME VARCHAR(15), TOTALSPENT INT, ALONGFIELDNAME VARCHAR(33), MYFIELDISALARGESTRINGGUESSWHATTYPE VARCHAR(141), day9 INT, day0 INT, day1 INT, day2 INT, day3 INT, day4 INT, day5 INT, day6 INT, day7 INT, day8 INT, day9 INT, day0 INT, day1 INT, day INT, day INT, day INT, day INT, day INT, day INT, day INT, day INT, day INT, day0 INT, day1 INT, day2 INT, day3 INT, day4 INT, day5 INT, day6 INT, day7 INT, day8 INT, swver VARCHAR(41), hwver VARCHAR(15), mac VARCHAR(29), type VARCHAR(31), hwId VARCHAR(44), fwId VARCHAR(44), oemId VARCHAR(44), devname VARCHAR(51), model VARCHAR(21), deviceId VARCHAR(52), alias VARCHAR(59), iconhash CHAR(1), relaystate INT, ontime INT, activemode VARCHAR(20), feature VARCHAR(19), updating INT, rssi INT, ledoff INT, latitude INT, longitude INT, index INT, zonestr VARCHAR(59), tzstr VARCHAR(34), dstoffset INT, month INT, month INT, month INT, current INT, voltage INT, power INT, total INT, time DATETIME, ledon BOOLEAN, systemtime DATETIME )
generatedddl
CREATE TABLE inception ( uuid VARCHAR(41), toppct VARCHAR(25), top VARCHAR(29), toppct VARCHAR(25), top VARCHAR(32), toppct VARCHAR(25), top VARCHAR(47), toppct VARCHAR(25), top VARCHAR(28), toppct VARCHAR(25), top VARCHAR(25), imagefilename VARCHAR(51), runtime CHAR(1) )
generatedddl
CREATE TABLE weather ( version VARCHAR(15), xsinoNamespaceSchemaLocation VARCHAR(63), credit VARCHAR(43), creditURL VARCHAR(31), url VARCHAR(50), title VARCHAR(43), link VARCHAR(30), suggestedpickup VARCHAR(37), suggestedpickup_period VARCHAR(14), location VARCHAR(58), stationid VARCHAR(16), latitude VARCHAR(19), longitude VARCHAR(20), observationtime VARCHAR(52), observationtime_rfc822 DATETIME, weather VARCHAR(20), windstring VARCHAR(74), winddir VARCHAR(16), winddegrees VARCHAR(15), windmph VARCHAR(16), windgust_mph VARCHAR(16), windkt CHAR(1), windgust_kt VARCHAR(14), pressurein VARCHAR(17), visibilitymi VARCHAR(17), iconurl_base VARCHAR(57), twoday_history_url VARCHAR(59), iconurl_name VARCHAR(19), oburl VARCHAR(56), disclaimerurl VARCHAR(46), copyrighturl VARCHAR(46), privacypolicy_url VARCHAR(42) )
Example Flow
Resources:
Created on 03-16-2018 12:19 AM
Other Options:
Created on 05-21-2022 07:38 AM
Good work!
I have similar plans to add ML for schema inference etc.
Any update after this?
Created on 01-12-2024 12:33 AM
I try using this custom processor but when I try it in tableName using ${db.table.name} like this
the result like this,
can you help me? its also when using ${filename} the result get the same ${filename}