Tuesday, 5 June 2018

Apache Nifi - VIII

In the last post, we saw how we can fetch data from MongoDB using Apache Nifi. We extend the discussion further by introducing another processor called RunMongoAggregation. GetMongo processor offers us a small window to query the MongoDB database but RunMongoAggregation processor ups the game by offering exciting possibilities. In the world of MongoDB, Aggregation Framework is a data processing tool for performing analysis in real-time and generating pre-aggregated reports. The fact that we have a separate processor, RunMongoAggregation, dedicated to run Aggregation against MongoDB only goes to show the significance of this processing framework even in Apache Nifi. More details about this framework are here. Perhaps we will explore the capabilities of Aggregation Framework when we deal with MongoDB context in some later articles. But, for now, let us focus on how we can leverage the Aggregation Framework in MongoDB using Nifi to process data

The environments and sample data details are in the previous post. We begin by showing a very simple aggregation pipeline in a flow consisting of just two steps as shown below:





















The properties of RunMongoAggregation are shown below:


As you can see, we have entered the pipeline in the Query field. Note that we have coded the same conditions that we used in the earlier post in form of Aggregation pipeline. So, we should get the same results as in the previous post. The properties of PutFile remain the same but are shown below for the sake of completeness:



















Let us run this flow:

























Let us examine the data provenance on RunMongoAggregation processor:












We can see that the three records. Further confirmation is seen by checking the file at the target directory:












This is in line with the results in the last post

In the next example, we attempt a self-join using $lookup stage and fetch the manager first name and manager last name. The flow remains the same but we merely switch the value in the Query field as shown below:


The properties for PutFile remain the same as shown earlier. Let us run this flow:
























Let us now examine the results in the file at target directory:







Note that are are 5 records as per the aggregation pipeline. Also, note that the manager first name and manager last name appears along with the employee record. In particular not that Steven King has no manager and that all the other four employees have Steven as their manager

Monday, 4 June 2018

Apache Nifi - VII

In this post, we will see how Apache Nifi integrates with MongoDB. MongoDB is a document database that supports querying and indexing along with scalability and flexibility. More details about MongoDB are here. We will use the latest versions of Nifi and MongoDB for all the work in this post: Apache Nifi 1.6 and MongoDB 3.6.5. MongoDB Community Edition is installed and employees data has already been loaded into the database using mongoimport command. This utility is reminiscent of SQL*Loader in Oracle database. The employees data has been repeatedly used in earlier blogs. Since this is only sample data, any other data may also be used

Using Compass, we can view the employee collection under users database and is shown below for reference:













It can been seen from Compass that there are 107 documents in the employees collection. We will use Nifi to interact with MongoDB and as a first activity fetch data from MongoDB. Note that like mongoimport, we have a corresponding mongoexport that can be used to export data out of  MongoDB. But, we are interested in fetching data from MongoDB using Nifi as the fetched data can be later processed using the rich features of Nifi.

The first flow that we will see is shown below:
























The flow consists of just two processors: GetMongo and PutFile. The properties of GetMongo are shown below:



















We set the properties for Mongo URI, Database, Collection and Results per FlowFile. Notice that the Mongo URI (Uniform Resource Identifier) points to the running instance of MongoDB that listening for client connections on TCP port 27017. The Results per FlowFile (shown below) offers the flexibility to limit the data extracted per FlowFile from MongoDB.



















On the PutFile processor, we just set the destination directory as shown below:



















Running this simple flow, we can see the Data Provenance on the GetMongo processor as shown below:
















Clicking on View above shows the data that has been extracted from MongoDB as shown below:





















We can also see the file under the destination directory:

























In the second attempt the flow used is the same but, we add bells and whistles to GetMongo processor as shown below:





































Query filters the records to only those having DEPARTMENT_ID as 90. This filter is passed to an attribute called query_detail to the FlowFile. Only four columns are projected and the _id field explicitly skipped. The salary is sorted in descending order and only the top 3 records are picked. The properties of PutFile remain the same. The query_detail attribute can be seen in the Attributes tab under Data Provenance as shown below:
















On the Content tab, we can view the result that is shown below:


























We can also see the file in destination folder as shown below:


























Note that there are only three records as we had configured. We can finally confirm this by running the same properties that we set in Nifi in Compass and the results compare well between the two as shown below:











With this last step, we conclude this post

Monday, 7 May 2018

Apache Nifi - VI

The blog could be also called "SQL in Nifi". SQL is prevalent is most Big Data tools and Apache Nifi is no different. The SQL in Apache Nifi is based on another project called Calcite, the details of which can be found here. All the SQL grammar and the supported SQL keywords are here. In this post, we will build a very simple flow that uses SQL to separate the records. For all the work in this post, we will be using the latest version of Apache Nifi, 1.6.0

The flow is shown below:
















Note that there is a QueryRecord processor that forms the crux of this flow. The SQL statements are written as user defined properties in this processor. One can define a property and in the Value field, enter the SQL statement. The property names will serve as a routing route. This will become obvious in a short while on seeing the properties of this processor. The properties of GetFile are shown below:



















On the QueryRecord processor, there are two properties that have to be set: RecordReader and RecordWriter. The RecordReader set as a Controller Service will be used to read the incoming flowfile. The RecordWriter set as a Controller Service will be used to write the outgoing flowfile. The properties of QueryRecord processor is shown below:



















Since we will be using the familiar employees.csv file used often in earlier posts, the RecordReader will be CSVReader. The RecordWriter will also be CSVRecordSetWriter that will write the flowfile in CSV format. Two properties are added that specify the SQL that will be used to query the flowfile in real time as it traverses through this processor. The SQLs will separate the records in the flowfile based on the salary field. The flowfile is queried in real time as though it is a table containing records of the input. The connections out of this processor are four: two based on the SQLs, one is the original flowfile and, lastly, the failure connection. The failure connection is routed back to itself as shown in the flow above. The properties of CSVReader controller service is shown below:



















The properties of CSVRecordSetWriter controller service is shown below:



















The properties of PutFile tied to connection SAL_GTE_15000 is shown below:



















The properties of PutFile tied to connection original is shown below:




















The properties of PutFile tied to connection SAL_LT_15000 is shown below:



















After all the properties are correctly set, enable the Controller Services, drop the employees.csv in source folder and start the flow. The results are shown below:















Checking the Data Provenance on the QueryRecord processor,we see three entries as shown below:








FORK corresponds to the original connection while the two ROUTEs are for the two SQLs in the properties. The output corresponding to the first Route is shown below:


















Note that the salary field has salary less than 15000 value. The output corresponding to the second Route is shown below:








The output has only three records but the salary values are all above 15000. Lastly, the original file is seen in output from FORK type:
















The output from the FORK type has records of all salaries. The QueryRecord acts in this flow more like a RouteOnContent processor. But then, we have not fully unleashed the power of SQL in Nifi in this example