Search for a Record

This example shows you how to search records that match a search criteria from an input source using the MemoryWriter, RecordList and FilterExpresssion classes. This example searches records from a CSV file but any other input source like MS Excel, XML, Database, etc may be used.

Input CSV file

868,Hewitt,Jennifer Love,0,17000.00,5/25/1985,B

Java Code Listing

package com.northconcepts.datapipeline.examples.cookbook;


import org.apache.log4j.Logger;

import com.northconcepts.datapipeline.core.DataEndpoint;
import com.northconcepts.datapipeline.core.DataReader;
import com.northconcepts.datapipeline.csv.CSVReader;
import com.northconcepts.datapipeline.filter.FilterExpression;
import com.northconcepts.datapipeline.job.Job;
import com.northconcepts.datapipeline.memory.MemoryWriter;

public class SearchForARecord {

    public static final Logger log = DataEndpoint.log;

    public static void main(String[] args) throws Throwable {
        DataReader reader = new CSVReader(new File("example/data/input/credit-balance-01.csv"))

        MemoryWriter memoryWriter = new MemoryWriter();, memoryWriter);

        int index = memoryWriter.getRecordList().findFirst(
                new FilterExpression("Rating == 'B' && parseDouble(CreditLimit) > 1000"), 0);

        // this would work too: "Rating = 'B' and parseDouble(CreditLimit) > 1000"

        if (index >= 0) {
        } else {
  "no record found");



Code walkthrough

  1. A CSVReader is created corresponding to the input CSV file credit-balance-01.csv.
  2. A new MemoryWriter is created.
  3. Data is transferred from the CSV file to a RecordList in memory via the JobTemplate.DEFAULT.transfer method.
  4. A new FilterExpresssion is created using the search criteria Rating == "B" and CreditLimit > 1000.
  5. The RecordList.findFirst method is invoked to find the first record which matches the search criteria specified.
  6. If a matching record is found, it is retrieved using its index, otherwise no record found message is displayed.

FilterExpession and Filter

Filter is an abstract class which represents a logical expression corresponding to the records being searched. FilterExpresssion is a concrete sub-class of Filter. It is created using a String which specifies the logical expression for the search criteria. It overrides the allow method to actually determines whether the particular record matches the specified expression.

Searching for records

In order to search for records from an input data source, they first need to be transferred to a RecordList in memory. This is done by creating a new MemoryWriter and transferring the data from the input reader to the MemoryWriter. This creates a new RecordList which can be retrieved via memoryWriter.getRecordList. RecordList has several methods like findFirst, findLast and findAll which can then be used to search for records. In this example, the RecordList.findFirst method is invoked with a FilterExpresssion and an integer which specifies the index in the RecordList from where to begin searching. If a record exists in the RecordList that matches this expression, a positive integer corresponding to the index of the first occurrence of the matching record is returned, otherwise -1 is returned. The matching record is then retrieved from the RecordList using its index and displayed to the console via the Datapipeline logger.

Console Output

11:45:23,660 DEBUG [main] datapipeline:60 - job::Start
11:45:23,669 DEBUG [main] datapipeline:72 - job::Success
11:45:23,738  INFO [main] datapipeline:42 - Record {
    2:[FirstName]:STRING=[Jennifer Love]:String
Mobile Analytics