Developing a custom parser module
About the abstract parse method
After you implement the abstract parse method, the parser receives the full name (of the file to parse) as the first parameter and returns a DataSetList object containing data that has been extracted.
The parser does not find and select files to parse. The ETL framework does this task in advance as per the configuration present during the creation of the ETL.
For instance, the ETL is configured to access a Secure File Transfer Protocol (SFTP) folder and select files that match a certain pattern. The ETL framework copies the selected files via SFTP to the local ETL engine disk and then, it sequentially calls the parse method of the defined custom parser for each file. This means that the ETL will:
- Call the parse method for the first file.
- Populate the output dataset with the result.
- Call the parse method for the second file.
- Append the result to the dataset, and so on.
After parsing each file and depending on configuration, the ETL framework will rename or move the parsed file.
Consider the following example:
Full example code
You can download the full code of the example presented:
.Editing the parser code
Every new custom parser module that you create in the ETL Development Kit uses a code template that contains some auto-generated code pieces.
The following examples sequentially illustrate the initial code you need to write and the other operations you can perform on parsers.
Write the initial parser code:
Instruct the parser to prepare the output datasets.
Open the file and read lines of text.
Parse a line of text, extract the CPU Utilization samples, and put the data in the dataset.
The code for parsing lines and filling the dataset looks similar to the one elaborated in the following example:
Badly formed file
We recommend that you add pieces of control code to the main parser code to make sure the successful run of a parser. The absence of control code results in a badly formed file – a file that contains incorrectly formed lines, which are eventually rejected by the parser. Such files are very common and are often encountered by the parser.
Rejection Percentage
As a best practice, we recommended that you calculate a rejection percentage (denoted by rp), on parsed content. rp is the percentage of rejected lines over the number of lines expected to be good and well-formed.
To derive a rejection percentage value, do the following:
- Count the total number of lines in the file (tot).
- Count the number of lines that match the regular expression used to select good lines (match).
- In most cases, match = tot/2.
- Therefore —
rp = (match - (tot/2) / (tot/2) * 100
After the rejection percentage rp is calculated, it can be logged to help the administrator in detecting bad files, or an error can be generated if the rp is too high. Using the rp command is only a recommendation. Adding the rp calculation code and logging functionality, the parser is complete (see the code example in ).