Smart Stocks with FLaNK (NiFi, Kafka, Flink SQL)
Published: 2020-12-18 18:51:55

---
title: Smart Stocks with FLaNK (NiFi, Kafka, Flink SQL)
published: true
date: 2020-12-18 18:47:00 UTC
tags: 
canonical_url: 
---

# Smart Stocks with FLaNK (NiFi, Kafka, Flink SQL)

[![](https://1.bp.blogspot.com/-GhLVBvhdm5I/X9y_vN07VBI/AAAAAAAAcB4/ZULC9JrNfkgE5Xe3I0xfc9Zux_FCQUm9wCLcBGAsYHQ/w717-h211/xmas4.png)](https://1.bp.blogspot.com/-GhLVBvhdm5I/X9y_vN07VBI/AAAAAAAAcB4/ZULC9JrNfkgE5Xe3I0xfc9Zux_FCQUm9wCLcBGAsYHQ/s2016/xmas4.png)
  

  

  

I would like to track stocks from IBM and Cloudera frequently during the day using Apache NiFi to read the REST API.   After that I have some Streaming Analytics to perform with Apache Flink SQL and I also want permanent fast storage in Apache Kudu queried with Apache Impala.

  

Let's build that in the cloud in seconds.

  

**Source Code:  ** [https://github.com/tspannhw/SmartStocks](https://github.com/tspannhw/SmartStocks)

  

To Script Loading Schemas, Tables, Alerts see scripts/setup.sh:

**Source Code:  ** [https://github.com/tspannhw/ApacheConAtHome2020](https://github.com/tspannhw/ApacheConAtHome2020)

  

- Kafka Topic
- Kafka Schema
- Kudu Table
- Flink Prep
- Flink SQL Client Run
- Flink SQL Client Configuration

Once our automated admin has built our cloud environment and populated it with the goodness of our app, we can being out continuous sql.

  

[![](https://1.bp.blogspot.com/-uMeMJMmItoE/X9zB-gTW1DI/AAAAAAAAcCI/kIfCClNk4xEpRi3VdPCyeIo-5-LX49SfwCLcBGAsYHQ/w568-h757/catkeyboard.jpg)](https://1.bp.blogspot.com/-uMeMJMmItoE/X9zB-gTW1DI/AAAAAAAAcCI/kIfCClNk4xEpRi3VdPCyeIo-5-LX49SfwCLcBGAsYHQ/s2048/catkeyboard.jpg)

  

### If you know your data, build a schema, share to the registry

  

[![](https://1.bp.blogspot.com/-3wxT9jSUdvM/X9zvFM45YkI/AAAAAAAAcEg/kqk6sHiRhnojKEbhgWEXHy5hxqYnwLHbQCLcBGAsYHQ/w640-h272/stockschema.png)](https://1.bp.blogspot.com/-3wxT9jSUdvM/X9zvFM45YkI/AAAAAAAAcEg/kqk6sHiRhnojKEbhgWEXHy5hxqYnwLHbQCLcBGAsYHQ/s1542/stockschema.png)
  

  

  

[![](https://1.bp.blogspot.com/-B3bS6Dp8Qg8/X9zvFOk3fKI/AAAAAAAAcEc/VFcGsLz_0xIXm5ij6cZWYFomWvyfEq50ACLcBGAsYHQ/w640-h382/stocksSchemaRegistry.png)](https://1.bp.blogspot.com/-B3bS6Dp8Qg8/X9zvFOk3fKI/AAAAAAAAcEc/VFcGsLz_0xIXm5ij6cZWYFomWvyfEq50ACLcBGAsYHQ/s1868/stocksSchemaRegistry.png)
  

  

One unique thing we added was a **default value**  in our Avro schema and making it a **logicalType**  for timestamp-millis.  This is helpful for Flink SQL timestamp related queries.

  

> { "name" : "dt", "type" : ["long"], "default": 1, "logicalType": "timestamp-millis"}

You can see the entire schema here:

[https://raw.githubusercontent.com/tspannhw/SmartStocks/main/stocks.avsc](https://raw.githubusercontent.com/tspannhw/SmartStocks/main/stocks.avsc)

We will also want a topic for Stock Alerts that we will create later with Flink SQL, so let's define a schema for that as well.

 [https://raw.githubusercontent.com/tspannhw/SmartStocks/main/stockalerts.avsc](https://raw.githubusercontent.com/tspannhw/SmartStocks/main/stockalerts.avsc)

For our data today we will use AVRO data with AVRO schemas for use inside Kafka topics and whoever will consume it.

  

### How to Build a Smart Stock DataFlow in X Easy Steps

[![](https://1.bp.blogspot.com/-ZFKWj2Mk7hM/X9zs-hl7v4I/AAAAAAAAcDY/2XGKHDbo_SUWZbRJubDDX63pLhTfnCoKACLcBGAsYHQ/w640-h602/nifiGetRest.png)](https://1.bp.blogspot.com/-ZFKWj2Mk7hM/X9zs-hl7v4I/AAAAAAAAcDY/2XGKHDbo_SUWZbRJubDDX63pLhTfnCoKACLcBGAsYHQ/s1708/nifiGetRest.png)
  

  

  

[![](https://1.bp.blogspot.com/-26y_KSKhTbQ/X9zs-uH_prI/AAAAAAAAcDU/1k0VyI-bo08u61K68hZvuCD6sgOPuH5WACLcBGAsYHQ/w590-h640/nifiFlowToKafka.png)](https://1.bp.blogspot.com/-26y_KSKhTbQ/X9zs-uH_prI/AAAAAAAAcDU/1k0VyI-bo08u61K68hZvuCD6sgOPuH5WACLcBGAsYHQ/s1548/nifiFlowToKafka.png)

  

  

  

  

1. Retrieve data from source (example:    **InvokeHTTP** against SSL REST Feed - say TwelveData) with a schedule.
2. Set a Schema Name ( **UpdateAttribute** )
3. **ForkRecord** :  We use this to split out records from the header (/values) using **RecordPath** syntax.
4. **QueryRecord** :  Convert type and manipulate data with SQL.   We aren't doing anything in this one, but this is an option to change fields, add fields, etc...
5. **UpdateRecord: ** This first one I am setting some fields in the record from attributes and adding a current timestamp.   I also reformate by timestamp for conversion.
6. **UpdateRecord** :   I am making **dt** make numeric UNIX timestamp.
7. **UpdateRecord** :  I am making **datetime**  my formatted String date time.
8. **(LookupRecord)**:  I don't have this step yet as I don't have an internal record for this company in my Real-Time Data Mart.  [https://docs.cloudera.com/runtime/7.0.3/kudu-overview/topics/kudu-architecture-cdp.html](https://docs.cloudera.com/runtime/7.0.3/kudu-overview/topics/kudu-architecture-cdp.html).  I will probably add this step to augment or check my data.
9. ( **ValidateRecord** ):   For less reliable data sources, I may want to validate my data against our schema, otherwise we will get warnings or errors.
10. **PublishKafkaRecord\_2\_0:   ** Convert from JSON to AVRO, send to our Kafka topic with headers including reference to the correct schema **stocks**  and it's version **1.0.**

  

  

[![](https://1.bp.blogspot.com/-DzW_xzW-GZ0/X9zubGsDPVI/AAAAAAAAcDs/LEdlSdMSI98-MmWvXFv9gMjVifU5YTlOwCLcBGAsYHQ/w336-h320/getRestDataTwelveData.png)](https://1.bp.blogspot.com/-DzW_xzW-GZ0/X9zubGsDPVI/AAAAAAAAcDs/LEdlSdMSI98-MmWvXFv9gMjVifU5YTlOwCLcBGAsYHQ/s410/getRestDataTwelveData.png)

  

[![](https://1.bp.blogspot.com/-vbOLkxuinK8/X9zubT5QSbI/AAAAAAAAcDw/i-ODLvQHA704AmN9ctTNfTnnUzj3bpssgCLcBGAsYHQ/w640-h204/forkRecord.png)](https://1.bp.blogspot.com/-vbOLkxuinK8/X9zubT5QSbI/AAAAAAAAcDw/i-ODLvQHA704AmN9ctTNfTnnUzj3bpssgCLcBGAsYHQ/s1299/forkRecord.png)
  

  

[![](https://1.bp.blogspot.com/-TwkMVq_qQGk/X9zubDVqilI/AAAAAAAAcD0/SfXTv0DCYnoFlucqpqM2Bl3OiW_B_WXOACLcBGAsYHQ/w640-h176/queryRecord.png)](https://1.bp.blogspot.com/-TwkMVq_qQGk/X9zubDVqilI/AAAAAAAAcD0/SfXTv0DCYnoFlucqpqM2Bl3OiW_B_WXOACLcBGAsYHQ/s978/queryRecord.png)
  

[![](https://1.bp.blogspot.com/-Y4jB_U_ugFk/X9zublOsbqI/AAAAAAAAcD4/f_tfyySYh2YC3hwyFJMAlI_NoC95Ht_yACLcBGAsYHQ/w640-h206/updateRecord0.png)](https://1.bp.blogspot.com/-Y4jB_U_ugFk/X9zublOsbqI/AAAAAAAAcD4/f_tfyySYh2YC3hwyFJMAlI_NoC95Ht_yACLcBGAsYHQ/s1108/updateRecord0.png)
  

[![](https://1.bp.blogspot.com/-07MmBlaTl_U/X9zub3coonI/AAAAAAAAcD8/tIUrHsIJcBY_ai8tWR9nmosQk7d9v1aSwCLcBGAsYHQ/w640-h146/updateRecord1.png)](https://1.bp.blogspot.com/-07MmBlaTl_U/X9zub3coonI/AAAAAAAAcD8/tIUrHsIJcBY_ai8tWR9nmosQk7d9v1aSwCLcBGAsYHQ/s1007/updateRecord1.png)
  

[![](https://1.bp.blogspot.com/-Th05I_Asm84/X9zucWt64cI/AAAAAAAAcEA/AUrdHtU7TwEcR9cCAGucFJu9NqlsgUnMwCLcBGAsYHQ/w640-h134/updateRecord2.png)](https://1.bp.blogspot.com/-Th05I_Asm84/X9zucWt64cI/AAAAAAAAcEA/AUrdHtU7TwEcR9cCAGucFJu9NqlsgUnMwCLcBGAsYHQ/s1064/updateRecord2.png)
  

  

  

  

Now that we are streaming our data to Kafka topics, we can utilize it in Flink SQL Continuous SQL applications, NiFi applications, Spark 3 applications and more.   So in this case CFM NiFi is our Producer and we will have CFM NiFi and CSA Flink SQL as Kafka Consumers.

  

We can see what our data looks like in the new cleaned up format with all the fields we need.

  

  

[![](https://1.bp.blogspot.com/-1MsZllZSzs8/X9zvFDXojjI/AAAAAAAAcEk/fdg9vqwEUJwMib-qXf_A7E6iUbHJ-4REwCLcBGAsYHQ/w542-h826/stocksData.png)](https://1.bp.blogspot.com/-1MsZllZSzs8/X9zvFDXojjI/AAAAAAAAcEk/fdg9vqwEUJwMib-qXf_A7E6iUbHJ-4REwCLcBGAsYHQ/s1731/stocksData.png)

###   

### Viewing, Monitoring, Checking and Alerting On Our Streaming Data in Kafka

Cloudera Streams Messaging Manager solves all of these difficult problems from one easy to use pre integrated UI.   It is pre-wired into my Kafka Datahubs and secured with SDX.

  

[![](https://1.bp.blogspot.com/-_28bZBfCZDg/X9z2iLpfmRI/AAAAAAAAcF0/03_SStAU4XMO4KRWEXi4J5JeDRvZdKSBACLcBGAsYHQ/w640-h250/smmStocksDataExplorer.png)](https://1.bp.blogspot.com/-_28bZBfCZDg/X9z2iLpfmRI/AAAAAAAAcF0/03_SStAU4XMO4KRWEXi4J5JeDRvZdKSBACLcBGAsYHQ/s2834/smmStocksDataExplorer.png)
  

[![](https://1.bp.blogspot.com/-HZ8K9MqJ65c/X9z2iGQ4TrI/AAAAAAAAcFw/7l2y52XaMRIbQ27bls9J5LrWpVIcvixegCLcBGAsYHQ/w640-h214/smmStocksTopicOverview.png)](https://1.bp.blogspot.com/-HZ8K9MqJ65c/X9z2iGQ4TrI/AAAAAAAAcFw/7l2y52XaMRIbQ27bls9J5LrWpVIcvixegCLcBGAsYHQ/s2497/smmStocksTopicOverview.png)
  

[![](https://1.bp.blogspot.com/-VLXbn0HSfWU/X9z2iKVLQrI/AAAAAAAAcFs/QBTUtN0V8h4wRCn7SExvXsoxwBrzCn86ACLcBGAsYHQ/w640-h300/smmStocksview.png)](https://1.bp.blogspot.com/-VLXbn0HSfWU/X9z2iKVLQrI/AAAAAAAAcFs/QBTUtN0V8h4wRCn7SExvXsoxwBrzCn86ACLcBGAsYHQ/s2166/smmStocksview.png)
  

I can see my AVRO data with associated **stocks**  schema is in the topic, ready to be consumed.  I can then monitor who is consuming, how much and if there is a lag or latency.

  

### How to Store Our Streaming Data to Our Real-Time DataMart in the Cloud

[![](https://1.bp.blogspot.com/-8GBLd8gjZWI/X9zyhgmhZxI/AAAAAAAAcEw/JX0URWi-qqky8JWsI_FqSVvMWwBF4r_OQCLcBGAsYHQ/w640-h370/consumeToKudu.png)](https://1.bp.blogspot.com/-8GBLd8gjZWI/X9zyhgmhZxI/AAAAAAAAcEw/JX0URWi-qqky8JWsI_FqSVvMWwBF4r_OQCLcBGAsYHQ/s1742/consumeToKudu.png)

  

Consume **stocks**  AVRO data with **stocks**  schema then write to our Real-Time Data Mart in Cloudera Data Platform powered by Apache Impala and Apache Kudu.   If something failed or could not connect, let's retry three times.

  

  

We use a parameter for our 3+ Kafka brokers with port.   We could also have parameters for topic names and consumer name.   We read from **stocks**  table which uses **stocks**  schema that is referenced in Kafka header automatically ready by NiFi.  When we sent message to Kafka, nifi passed on our schema name via **schema.name** attribute in NiFi.   As we can see it was schema attached Avro, so we use that Reader and convert to simple JSON with that schema.

  

[![](https://1.bp.blogspot.com/-2wYNVsBpSVU/X9zy4cq-6_I/AAAAAAAAcFA/SjNQSnlbneMxijTel_dhHTGADjN2U89rACLcBGAsYHQ/w640-h278/consumeKafkaBrokers.png)](https://1.bp.blogspot.com/-2wYNVsBpSVU/X9zy4cq-6_I/AAAAAAAAcFA/SjNQSnlbneMxijTel_dhHTGADjN2U89rACLcBGAsYHQ/s930/consumeKafkaBrokers.png)  

[![](https://1.bp.blogspot.com/-pVyRx0I2wBU/X9zy4UsHCRI/AAAAAAAAcE8/vieRQmzeWGk_zQDSt6A9jAEdr9NWKf4YgCLcBGAsYHQ/w640-h590/consumeKafka.png)](https://1.bp.blogspot.com/-pVyRx0I2wBU/X9zy4UsHCRI/AAAAAAAAcE8/vieRQmzeWGk_zQDSt6A9jAEdr9NWKf4YgCLcBGAsYHQ/s484/consumeKafka.png)
  

Writing to our Cloud Native Real-Time Data Mart could not be simpler, we reference the table **stocks** we have created and have permissions to and use the JSON reader.   I like **UPSERT** since it handles INSERT AND UPDATE.

  

First we need to create our Kudu table in either Apache Hue from CDP or from the command line scripted.    **Example** :  impala-shell -i edge2ai-1.dim.local -d default -f  /opt/demo/sql/kudu.sql 

> CREATE TABLE **stocks**
> 
> (
> 
>   uuid STRING,
> 
>   `datetime` STRING,
> 
>   `symbol` STRING, 
> 
>   `open` STRING, 
> 
>   `close` STRING,
> 
>   `high` STRING,
> 
>   `volume` STRING,
> 
>   `ts` TIMESTAMP,
> 
>   `dt` TIMESTAMP,
> 
>   `low` STRING,
> 
> PRIMARY KEY (uuid,`datetime`) ) 
> 
> PARTITION BY HASH PARTITIONS 4 
> 
> STORED AS KUDU TBLPROPERTIES ('kudu.num\_tablet\_replicas' = '1');

  

  

[![](https://1.bp.blogspot.com/-JLZMeuATO_w/X9zy4Te0xCI/AAAAAAAAcFE/KA0fMIVAeocivoZ-A6bQ_2cOf_puS6pFgCLcBGAsYHQ/w640-h522/putKudu.png)](https://1.bp.blogspot.com/-JLZMeuATO_w/X9zy4Te0xCI/AAAAAAAAcFE/KA0fMIVAeocivoZ-A6bQ_2cOf_puS6pFgCLcBGAsYHQ/s1004/putKudu.png)

  

  

Using Apache Hue integrated in CDP, I can examine my Real-Time Data Mart table and then query my table.  

  

[![](https://1.bp.blogspot.com/-dFgWsuSFSmU/X9z22lEOJOI/AAAAAAAAcGA/h8rKzbvLgMMp2Vq5aMn2tN8Muajse2LUACLcBGAsYHQ/w640-h274/kuduStocksTable.png)](https://1.bp.blogspot.com/-dFgWsuSFSmU/X9z22lEOJOI/AAAAAAAAcGA/h8rKzbvLgMMp2Vq5aMn2tN8Muajse2LUACLcBGAsYHQ/s2710/kuduStocksTable.png)

  

My data is now ready for reports, dashboards, applications, notebooks, web applications, mobile apps and machine learning.

  

[![](https://1.bp.blogspot.com/-il-ayvs6-sg/X9z22q3NMvI/AAAAAAAAcGE/lkskQTFE1Jw3LeoyIvVdcicRyUhw3_U3ACLcBGAsYHQ/w640-h394/hueSelectStocks.png)](https://1.bp.blogspot.com/-il-ayvs6-sg/X9z22q3NMvI/AAAAAAAAcGE/lkskQTFE1Jw3LeoyIvVdcicRyUhw3_U3ACLcBGAsYHQ/s2048/hueSelectStocks.png)

  

I can now spin up a Cloudera Visual Application on this table in a few seconds.

  

[![](https://1.bp.blogspot.com/-2BwGHlUHGTY/X9z2JssluNI/AAAAAAAAcFg/3IMYgrQQjlkueYW-X5p_JFwpYcoshlcSgCLcBGAsYHQ/w640-h492/stocksvisualapp.png)](https://1.bp.blogspot.com/-2BwGHlUHGTY/X9z2JssluNI/AAAAAAAAcFg/3IMYgrQQjlkueYW-X5p_JFwpYcoshlcSgCLcBGAsYHQ/s1385/stocksvisualapp.png)
  

  

  

Now we can build our streaming analytics application in Flink.

  

### How to Build a Smart Stock Streaming Analytics in X Easy Steps

I can connect to Flink SQL from the command line Flink SQL Client to start exploring my Kafka and Kudu data, create temporary tables and launch some applications (insert statements). The environment lets me see all the different catalogs available including registry (Cloudera Cloud Schema Registry), hive (Cloud Native Database table) and kudu (Cloudera Real-Time Cloud Data Mart) tables.

  

[![](https://1.bp.blogspot.com/-BY-jpkFxD_M/X9zl8cts3PI/AAAAAAAAcCg/TSMNzxcRygkRqlVs1P00YM01EHB4t9YzACLcBGAsYHQ/w640-h598/flinkSQLClient.png)](https://1.bp.blogspot.com/-BY-jpkFxD_M/X9zl8cts3PI/AAAAAAAAcCg/TSMNzxcRygkRqlVs1P00YM01EHB4t9YzACLcBGAsYHQ/s1461/flinkSQLClient.png)
  

[![](https://1.bp.blogspot.com/-Aqft8sZPMgE/X9zl8sKPjdI/AAAAAAAAcCk/LhjnM5RgDdE-08ELl7bavbrBEes3t6FbgCLcBGAsYHQ/w640-h296/flinksqlcreatestockalerts.png)](https://1.bp.blogspot.com/-Aqft8sZPMgE/X9zl8sKPjdI/AAAAAAAAcCk/LhjnM5RgDdE-08ELl7bavbrBEes3t6FbgCLcBGAsYHQ/s2537/flinksqlcreatestockalerts.png)

  

[![](https://1.bp.blogspot.com/-1X5ZqtpSlYY/X9zl8TuwU2I/AAAAAAAAcCc/I5USKHry-UcuRCleKLMwmNRQRP-4FJcfACLcBGAsYHQ/w640-h428/flinkCreateStockEvents.png)](https://1.bp.blogspot.com/-1X5ZqtpSlYY/X9zl8TuwU2I/AAAAAAAAcCc/I5USKHry-UcuRCleKLMwmNRQRP-4FJcfACLcBGAsYHQ/s1754/flinkCreateStockEvents.png)
  
  

[![](https://1.bp.blogspot.com/-Q8hfuB3ZkW8/X9zl8eKJhFI/AAAAAAAAcCY/JH-7g8OvIX4eu6CHouJ0MWi2_n0tXVXQACLcBGAsYHQ/w640-h618/flinkSQLRow.png)](https://1.bp.blogspot.com/-Q8hfuB3ZkW8/X9zl8eKJhFI/AAAAAAAAcCY/JH-7g8OvIX4eu6CHouJ0MWi2_n0tXVXQACLcBGAsYHQ/s1890/flinkSQLRow.png)
  

[![](https://1.bp.blogspot.com/-qGlBPXdSei8/X9zl9eVtliI/AAAAAAAAcCo/qAHAfYnUImsGRJluBPlQYyUsp2tRPE5ggCLcBGAsYHQ/w640-h410/flinksqltumblestocks.png)](https://1.bp.blogspot.com/-qGlBPXdSei8/X9zl9eVtliI/AAAAAAAAcCo/qAHAfYnUImsGRJluBPlQYyUsp2tRPE5ggCLcBGAsYHQ/s2048/flinksqltumblestocks.png)
  

[![](https://1.bp.blogspot.com/-Al6fjjuULJ4/X9zl9r7cIVI/AAAAAAAAcCw/aDuJMy_TBjkjDoPSOM8Y7sU7oNvxqWuWQCLcBGAsYHQ/w640-h346/flinksqlwindowquery.png)](https://1.bp.blogspot.com/-Al6fjjuULJ4/X9zl9r7cIVI/AAAAAAAAcCw/aDuJMy_TBjkjDoPSOM8Y7sU7oNvxqWuWQCLcBGAsYHQ/s2048/flinksqlwindowquery.png)
  

[![](https://1.bp.blogspot.com/-EGz-p8x16x0/X9zl9p6BOcI/AAAAAAAAcC0/g4phTangpYs8fntLAitT0z8hlz88OHS3QCLcBGAsYHQ/w640-h406/queryStocksFlinkSQL.png)](https://1.bp.blogspot.com/-EGz-p8x16x0/X9zl9p6BOcI/AAAAAAAAcC0/g4phTangpYs8fntLAitT0z8hlz88OHS3QCLcBGAsYHQ/s2048/queryStocksFlinkSQL.png)
  

  

  

**Run Flink SQL Client**

  

It's a two step process, first setup a yarn session.   You may need to add your **Kerberos**  credentials.

  

> flink-yarn-session -tm 2048 -s 2 -d

  

Then launch the command line SQL Client.

  

> flink-sql-client embedded -e sql-env.yaml

 

See:  [https://docs.cloudera.com/csa/1.2.0/sql-client/topics/csa-sql-client-config.html](https://docs.cloudera.com/csa/1.2.0/sql-client/topics/csa-sql-client-config.html)

[https://docs.cloudera.com/csa/1.2.0/sql-client/topics/csa-sql-security.html](https://docs.cloudera.com/csa/1.2.0/sql-client/topics/csa-sql-security.html)

  

**Run Flink SQL**

  

**Cross Catalog Query to Stocks Kafka Topic**

  

select \* from registry.default\_database.stocks;

  

**Cross Catalog Query to Stocks Kudu/Impala Table**

  

select \* from kudu.default\_database.impala::default.stocks;

  

**Default Catalog**

  

use catalog default\_catalog;

  

CREATE TABLE **stockEvents** ( symbol STRING, uuid STRING, ts BIGINT, dt BIGINT, datetime STRING, open STRING, close STRING, high STRING, volume STRING, low STRING, 

event\_time AS CAST(from\_unixtime(floor(ts/1000)) AS TIMESTAMP(3)), 

WATERMARK FOR event\_time AS event\_time - INTERVAL '5' SECOND ) 

WITH ( 'connector.type' = 'kafka', 'connector.version' = 'universal', 

'connector.topic' = 'stocks', 

'connector.startup-mode' = 'earliest-offset', 

'connector.properties.bootstrap.servers' = 'edge2ai-1.dim.local:9092', 

'format.type' = 'registry', 

'format.registry.properties.schema.registry.url' = 'http://edge2ai-1.dim.local:7788/api/v1' );

  

show tables;

  

Flink SQL> describe stockEvents; 

  

root |-- symbol: STRING |-- uuid: STRING |-- ts: BIGINT |-- dt: BIGINT |-- datetime: STRING |-- open: STRING |-- close: STRING |-- high: STRING |-- volume: STRING |-- low: STRING |-- event\_time: TIMESTAMP(3) AS CAST(FROM\_UNIXTIME(FLOOR(ts / 1000)) AS TIMESTAMP(3)) |-- WATERMARK FOR event\_time AS event\_time - INTERVAL '5' SECOND

  

We added a watermark and event time pulled from our timestamp.

  

**Simple Select All Query**

  

select \* from default\_catalog.default\_database.stockEvents;

  

We can do some interesting queries against this table we created.

  

**Tumbling Window**

  

SELECT symbol, 

**TUMBLE\_START** (event\_time, INTERVAL '1' MINUTE) as tumbleStart, 

TUMBLE\_END(event\_time, INTERVAL '1' MINUTE) as tumbleEnd, 

AVG(CAST(high as DOUBLE)) as avgHigh 

FROM stockEvents 

WHERE symbol is not null 

GROUP BY TUMBLE(event\_time, INTERVAL '1' MINUTE), symbol;

  

**Top 3**

  

SELECT \* 

FROM 

( SELECT \* , ROW\_NUMBER() OVER 

( PARTITION BY window\_start ORDER BY num\_stocks desc ) AS rownum 

FROM ( 

SELECT TUMBLE\_START(event\_time, INTERVAL '10' MINUTE) AS window\_start, 

symbol, 

COUNT(\*) AS num\_stocks 

FROM stockEvents 

GROUP BY symbol, 

TUMBLE(event\_time, INTERVAL '10' MINUTE) ) ) 

WHERE rownum <=3;

  

**Stock Alerts**

  

INSERT INTO **stockalerts**  

/\*+ OPTIONS('sink.partitioner'='round-robin') \*/ 

SELECT CAST(symbol as STRING) symbol, 

CAST(uuid as STRING) uuid, ts, dt, open, close, high, volume, low, 

datetime, 'new-high' message, 'nh' alertcode, CAST(CURRENT\_TIMESTAMP AS BIGINT) alerttime FROM stocks st 

WHERE symbol is not null 

AND symbol <> 'null' 

AND trim(symbol) <> '' 

AND CAST(close as DOUBLE) > 11;

  

  

## Monitoring Flink Jobs

Using the CSA Flink Global Dashboard, I can see all my Flink jobs runninging including SQL Client jobs, disconnected Flink SQL inserts and deployed Flink applications.

  

[![](https://1.bp.blogspot.com/-kPskb_5fHTk/X9z3YW6OOxI/AAAAAAAAcGU/-9t-elHn78QhqJTCZgrBGXcY2xS3rSK-ACLcBGAsYHQ/w640-h250/flinkdashboardjobs.png)](https://1.bp.blogspot.com/-kPskb_5fHTk/X9z3YW6OOxI/AAAAAAAAcGU/-9t-elHn78QhqJTCZgrBGXcY2xS3rSK-ACLcBGAsYHQ/s2474/flinkdashboardjobs.png)
  

[![](https://1.bp.blogspot.com/-cm10658qfHc/X9z3YTVpyjI/AAAAAAAAcGY/JlZVC8n8Nwk_aI51uPxFZWEmaSrrngdbACLcBGAsYHQ/w640-h296/flinksql1mintumblre.png)](https://1.bp.blogspot.com/-cm10658qfHc/X9z3YTVpyjI/AAAAAAAAcGY/JlZVC8n8Nwk_aI51uPxFZWEmaSrrngdbACLcBGAsYHQ/s2209/flinksql1mintumblre.png)
  

[![](https://1.bp.blogspot.com/-L-apg5r0fXU/X9z3YQY-lvI/AAAAAAAAcGc/GhMg7iMbgPga4bMdCA4dbpNMnrCgs_jdQCLcBGAsYHQ/w640-h332/flinksqlgroupby.png)](https://1.bp.blogspot.com/-L-apg5r0fXU/X9z3YQY-lvI/AAAAAAAAcGc/GhMg7iMbgPga4bMdCA4dbpNMnrCgs_jdQCLcBGAsYHQ/s2048/flinksqlgroupby.png)
  

[![](https://1.bp.blogspot.com/-aEJ9mxjGvlA/X9z3YiUXowI/AAAAAAAAcGg/4r7TVIAOUfQH5jmSZ5M0I_enlT8wjqF9gCLcBGAsYHQ/w640-h320/flinksqltumble.png)](https://1.bp.blogspot.com/-aEJ9mxjGvlA/X9z3YiUXowI/AAAAAAAAcGg/4r7TVIAOUfQH5jmSZ5M0I_enlT8wjqF9gCLcBGAsYHQ/s2048/flinksqltumble.png)
  

[![](https://1.bp.blogspot.com/-TW5ELy5Bp9k/X9z3YponMYI/AAAAAAAAcGo/oPSmqAYxMf8Bq3K_--AteO69HqHcOTnGwCLcBGAsYHQ/w640-h280/insertflinksqljob.png)](https://1.bp.blogspot.com/-TW5ELy5Bp9k/X9z3YponMYI/AAAAAAAAcGo/oPSmqAYxMf8Bq3K_--AteO69HqHcOTnGwCLcBGAsYHQ/s2530/insertflinksqljob.png)
  

[![](https://1.bp.blogspot.com/-KOacriPRqKo/X9z3YtG4VSI/AAAAAAAAcGk/ug9B99NvHZUtS2ZiTRgZGNOVgp1tbGgmQCLcBGAsYHQ/w640-h202/runningflinkjobs.png)](https://1.bp.blogspot.com/-KOacriPRqKo/X9z3YtG4VSI/AAAAAAAAcGk/ug9B99NvHZUtS2ZiTRgZGNOVgp1tbGgmQCLcBGAsYHQ/s2169/runningflinkjobs.png)
  

  

  

[![](https://1.bp.blogspot.com/-9zxM1l3YWNY/X9z3ZIL_SzI/AAAAAAAAcG0/km7K6q4-6dYD_OZJPxXpJwWhSLWiNqY7ACLcBGAsYHQ/w640-h276/stockAlerts.png)](https://1.bp.blogspot.com/-9zxM1l3YWNY/X9z3ZIL_SzI/AAAAAAAAcG0/km7K6q4-6dYD_OZJPxXpJwWhSLWiNqY7ACLcBGAsYHQ/s2511/stockAlerts.png)

  

  

We can also see the data populated in the **stockalerts**  topic.  We can run a Flink SQL, Spark 3, NiFi or other applications against this data to handle alerts.   That may be the next application, I may send those alerts to iphone messages, Slack messages, a database table and a websockets app.
  

[![](https://1.bp.blogspot.com/-dYGs-_Az37Q/X9z3ZhxnGaI/AAAAAAAAcGw/8uLowO4q3MQlrGgqqm9pO4KtPJ6EVib3ACLcBGAsYHQ/w640-h276/stockalertsflinksql.png)](https://1.bp.blogspot.com/-dYGs-_Az37Q/X9z3ZhxnGaI/AAAAAAAAcGw/8uLowO4q3MQlrGgqqm9pO4KtPJ6EVib3ACLcBGAsYHQ/s2536/stockalertsflinksql.png)

  

  

## Data Lineage and Governance

We all know that NiFi has deep data lineage that can be pushed or pulled via REST, Reporting Tasks or CLI to use in audits, metrics and tracking.   If I want to all the governance data for my entire streaming pipeline I will use Apache Atlas that is prewired as part of SDX in my Cloud Data Platform.

  

[![](https://1.bp.blogspot.com/-yZu7rt1An2U/X9z1-XwGC9I/AAAAAAAAcFc/js8Qci-fUaw_TAOLak2kvifc80_RxOlcACLcBGAsYHQ/w640-h358/atlasStocks.png)](https://1.bp.blogspot.com/-yZu7rt1An2U/X9z1-XwGC9I/AAAAAAAAcFc/js8Qci-fUaw_TAOLak2kvifc80_RxOlcACLcBGAsYHQ/s2048/atlasStocks.png)
  

  

## References

- [https://github.com/cloudera/flink-tutorials/tree/master/flink-sql-tutorial](https://github.com/cloudera/flink-tutorials/tree/master/flink-sql-tutorial)
- [https://github.com/tspannhw/FlinkSQLWithCatalogsDemo](https://github.com/tspannhw/FlinkSQLWithCatalogsDemo)
- [https://github.com/tspannhw/ClouderaFlinkSQLForPartners/blob/main/README.md](https://github.com/tspannhw/ClouderaFlinkSQLForPartners/blob/main/README.md)
- [https://github.com/tspannhw/ApacheConAtHome2020/tree/main/scripts](https://github.com/tspannhw/ApacheConAtHome2020/tree/main/scripts)
- [https://github.com/tspannhw/SmartWeather](https://github.com/tspannhw/SmartWeather)
- [https://ci.apache.org/projects/flink/flink-docs-stable/dev/table/sqlClient.html](https://ci.apache.org/projects/flink/flink-docs-stable/dev/table/sqlClient.html)
- [https://www.datainmotion.dev/2020/11/flank-smart-weather-applications-with.html](https://dev.to/tspannhw/flank-smart-weather-applications-with-flink-sql-2kfj)
- [https://www.datainmotion.dev/2020/11/flank-smart-weather-websocket.html](https://dev.to/tspannhw/flank-smart-weather-websocket-application-kafka-consumer-5d1b)

[![](https://1.bp.blogspot.com/-6-IUyw2pLWY/X9zBvT53HmI/AAAAAAAAcCE/7MNtJKaFqJkGhAHp3y5hYPdv2YnJbFungCLcBGAsYHQ/w681-h908/catchristmas.jpg)](https://1.bp.blogspot.com/-6-IUyw2pLWY/X9zBvT53HmI/AAAAAAAAcCE/7MNtJKaFqJkGhAHp3y5hYPdv2YnJbFungCLcBGAsYHQ/s2048/catchristmas.jpg)