bing
Flat 10% & upto 50% off + Free additional Courses. Hurry up!

Validating Data and Handling Errors

 

Capturing errors while calculating the age of a film:

  1. Get the file with the films. You can take the transformation that denormalized the data and generate the file with a Text file output step, or you can take a sample file from the Packt website.
  2. Create a new transformation and read the file with a Text file input step.
  3. Do a preview of the data. You will see the following:

 

1

 

 

  1. After the Text file input step, add a Get System Info step.
  2. Edit the step, add a new field named today, and choose Today 00:00:00 as its value.
  3. Add a JavaScript step.
  4. Edit the step and type the following piece of code:
var diff;

film_date = str2date('01/01/' + Year, 'dd/MM/yyyy');

diff = dateDiff(film_date,today,”y”);
  1. Click on Get variables to add diff as a new field.
  2. Add a Number range step, edit it, and fill its window as follows:

 

1

 

 

  1. With a Sort rows step, sort the data by diff.
  2. Finally, add a Group by step and double-click to edit it.
  3. As group field put age_of_film. In the Aggregates grid create a field named number_of_films to hold the number of films with that age. Put film as the Subject and select Number of values (N) as the Type.
  4. Add a Dummy step at the end and do a preview. You will be surprised by an error like this:

 

1

 

 

  1. Look at the logging window. It looks like this:

 

1

 

 

  1. Now drag Write to log step to the canvas from the Utility category.
  2. Create a hop from the JavaScript step to this new step.
  3. Select the JavaScript step, right-click it to bring up a contextual menu, and select Define error handling….
  4. The error handling settings window appears. Fill it like shown:

 

1

 

 

  1. Click on OK.
  2. Save the transformation and do a new preview on the Dummy step. You will see this:

 

1

 

 

  1. The logging window will show you this:
... - Bad rows.0 -

... - Bad rows.0 - ------------> Linenr 1-------------------------

... - Bad rows.0 – null
  1. Now do a preview on the Write to log step. This is what you see:

 

1

 

 

Aborting when there are too many errors:

We can handle the errors by detecting and sending bad rows to an extra stream. But when the errors are too many or when the errors are severe, the best option is to cancel the whole transformation.

Let’s see how to force the abortion of a transformation in such a situation.

  1. Open the transformation from the previous tutorial and save it under a different name.
  2. From the Flow category, drag an Abort step to the canvas.
  3. Create a hop from the Write to log step to the Abort step.
  4. Double-click the Abort step. Enter 5 as Abort threshold. As Abort message, type Too many errors calculating age of film!.
  5. Click OK.
  6. Select the Dummy step and do a preview. As a result, a warning window shows up informing you that there were no rows to display. In the Step Metrics tab, the Abort after 5 errors line becomes red to show you that there was an error:

 

1

 

 

  1. The log looks like this:
... - Bad rows.0 -

... - Bad rows.0 - ====================

Avoiding unexpected errors by validating data

To avoid unexpected errors that happen or just to meet your requirements is a common practice to validate your data before processing it.

 

Avoiding unexpected errors by validating data:
To avoid unexpected errors that happen or just to meet your requirements is a common practice to validate your data before processing it. Let’s do some validations.

 

Validating genres with a Regex Evaluation step:

  1. Create a new transformation.
  2. Read the modified films file just as you did in the previous tutorial.
  3. In the Content tab, check the Rownum in output? option and fill the Rownum fieldname with the text rownum.
  4. Do a preview. You should see this:

 

1

 

 

  1. After the Text file input step, add a Regex Evaluation step. You will find it under the Scripting category of steps.
  2. Under the Step settings box, select Genres as the Field to evaluate, and type genres_ok as the Result Fieldname.
  3. In the Regular expression textbox type [A-Za-z\s\-]*(\|[A-Za-z\s\-]*)* .
  4. Add the Filter rows step, an Add constants step, and two Text file output steps and link them as shown next:

 

1

 

 

  1. Edit the Add constants step.
  2. Add a String constant named err_code with value GEN_INV and a String constant named err_desc with value Invalid list of genres.
  3. Configure the Text file output step after the Add constant step to create the ${LABSOUTPUT}/films_err.txt file, with the fields rownum, err_code, and err_desc.
  4. Configure the other Text file output step to create the ${LABSOUTPUT}/films_ ok.txt file, with the fields film, Year, Genres, Director, and Actors.
  5. Double-click the Filter rows step and add the condition genres_ok = Y, Y being a Boolean value. Send true data to the stream that generates the films_ok.txt file. Send false data to the other stream.
  6. Run the transformation.
  7. Check the generated files. The films_err.txt file looks like the following:
rownum;err_code;err_desc

12;GEN_INV;Invalid list of genres

18;GEN_INV;Invalid list of genres

20;GEN_INV;Invalid list of genres

21;GEN_INV;Invalid list of genres

22;GEN_INV;Invalid list of genres

33;GEN_INV;Invalid list of genres

34;GEN_INV;Invalid list of genres.

 

Validating data:

As said, you would validate data mainly for two reasons:

1. To prevent the transformation from aborting because of unexpected errors

2. To check that your data meets some pre-existing requirements.

 

Checking films file with the Data Validator:

Let’s validate not only the Genres field, but also the Year field.

  1. Open the last transformation and save it under a new name.
  2. Delete all steps except the Text file input and Text file output steps.
  3. In the Fields tab of the Text file input step, change the Type of the Year from Integer to String.
  4. From the Validation category add a Data Validator step. Also add a Select values step. Link all steps as follows:

 

 

1

 

 

  1. Double-click the Data Validator step.
  2. Check the Report all errors, not only the first option found on at the top of the window. This will enable the Output one row, concatenate errors with separator option. Check this option too, and fill the textbox to the right with a slash /. Click on New validation and type genres as the name of the validation rule.
  3. Click on OK.
  4. Click on genres. The right half of the window is filled with checkboxes and textboxes where you will define the rule.

9. Fill the header of the rule definition as follows:

 

1

 

 

  1. In the Regular expression expected to match textbox, type [A-Za-z\s\-]*(\|[A-Za-z\s\-]*)*
  2. Click on New validation and type year as the name of the validation rule.
  3. Click on OK.
  4. Click on year and fill the header of the rule definition as follows:

 

1

 

 

  1. In the data block, select the Only numeric data expected checkbox option.
  2. Click on OK.
  3. Right-click the Data Validator step and select Define error handling….
  4. Fill the error handling settings window as follows: As Target step, select the step that generates the file with invalid rows. Check the Enable the error handling? checkbox. Type err_desc as Error description field name, err_field as Error fields, and err_code as Error codes. Click on OK.
  5. Use the Select values step to change the metadata for the Year from String to Integer.
  6. Save the transformation and run it.
  7. . Check the generated files. The films_err.txt file now has more detail, as you validated two fields.rownum;err_code;err_desc9;YEAR_NULL;Year invalid or absent 12;GEN_INV;Invalid list of genres18;GEN_INV;Invalid list of genres
    20;GEN_INV;Invalid list of genres
    
    21;GEN_INV;Invalid list of genres
    
    22;GEN_INV;Invalid list of genres
    
    33;GEN_INV;Invalid list of genres
    
    34;GEN_INV;Invalid list of genres
    
    47;YEAR_NULL/GEN_INV;Year invalid or absent/Invalid list of genres
    
    48;YEAR_NULL/GEN_INV;Year invalid or absent/Invalid list of genres
    
    49;YEAR_NULL;Year invalid or absent
  8. The films_ok.txt file should have less rows instead, as the films with year invalid or absent are no longer sent to this file.

"0 Responses on Validating Data and Handling Errors"

Training in Cities

Bangalore, Hyderabad, Chennai, Delhi, Kolkata, UK, London, Chicago, San Francisco, Dallas, Washington, New York, Orlando, Boston

100% Secure Payments. All major credit & debit cards accepted Or Pay by Paypal.

top

Sales Offer

  • To avail this offer, enroll before 07th December 2016.
  • This offer cannot be combined with any other offer.
  • This offer is valid on selected courses only.
  • Please use coupon codes mentioned below to avail the offer
offer-june

Sign Up or Login to view the Free Validating Data and Handling Errors.