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
Account,LastName,FirstName,Balance,CreditLimit,AccountCreated,Rating 101,Reeves,Keanu,9315.45,10000.00,1/17/1998,A 312,Butler,Gerard,90.00,1000.00,8/6/2003,B 868,Hewitt,Jennifer Love,0,17000.00,5/25/1985,B 761,Pinkett-Smith,Jada,49654.87,100000.00,12/5/2006,A 317,Murray,Bill,789.65,5000.00,2/5/2007,C
Java Code Listing
package com.northconcepts.datapipeline.examples.cookbook; import java.io.File; 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")) .setFieldNamesInFirstRow(true); MemoryWriter memoryWriter = new MemoryWriter(); Job.run(reader, 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) { log.info(memoryWriter.getRecordList().get(index)); } else { log.info("no record found"); } } }
Code walkthrough
- A CSVReader is created corresponding to the input CSV file
credit-balance-01.csv
. - A new MemoryWriter is created.
- Data is transferred from the CSV file to a RecordList in memory via the JobTemplate.DEFAULT.transfer method.
- A new FilterExpresssion is created using the search criteria
Rating == "B" and CreditLimit > 1000
. - The RecordList.findFirst method is invoked to find the first record which matches the search criteria specified.
- 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 { 0:[Account]:STRING=[868]:String 1:[LastName]:STRING=[Hewitt]:String 2:[FirstName]:STRING=[Jennifer Love]:String 3:[Balance]:STRING=[0]:String 4:[CreditLimit]:STRING=[17000.00]:String 5:[AccountCreated]:STRING=[5/25/1985]:String 6:[Rating]:STRING=[B]:String }