Update the Latest Data in a CSV File to the Database

Problem description & analysis

Below is CSV file emp.csv:

EMPID,FIRSTNAME,LASTNAME,EMAIL,UPDATEDATE

123,John,Smith,john.smith01@email.com,01/01/2020

234,Bruce,Waye,bruce.wayne@wayneenterprises.com,02/02/2020

123,John,Smith,john.smith02@email.com,02/15/2020

345,Clark,Kent,clark.kent@dailyplanet.com,02/16/2020

123,John,Smith,john.smith03@email.com,02/20/2020

In the above file, UPADATEDATE field is ordered and EMPID field contains duplicates.

We are trying to divide records in the CSV file into two parts:

1. Write the newest employee records, which are those with the latest UPDATEDATE values after the file is grouped by EMPID, to EMP table;

2. Write the rest of the records, which are the difference, to EMP_HIS table according to the original order.

Solution

Write the following script p1.dfx in esProc:

A

1

=file("emp.csv").import@ct(EMPID,FIRSTNAME,LASTNAME,EMAIL,UPDATEDATE📅"MM/dd/yyyy")

2

=A1.group(EMPID).(~.m(-1))

3

=A1\A2

4

=connect("demo")

5

=A4.update(A2,EMP;EMPID)

6

=A4.update(A3,EMP_HIS;EMPID,UPDATEDATE)

7

>A4.close()

Explanation:

A1  Import data from the CSV file, during which the first row is read as column headers and UPDATEDATE is parsed into date type (while the original date format is MM/dd/yyyy).

A2  Group A1’s table by EMPID and get the last member from each group (the record with the latest UPDATEDATE value).

A3  Get the rest of the records, which is the difference of A1 and A2.

A4  Connect to the database.

A5  Update the latest employee record to the database by EMPID or insert it to EMP table.

A6  Update the historical employee records by EMPID and UPDATEDATE to the database, or insert them to EMP_HIS table.

A7  Close database connection.

Find how to integrate the script code with a Java program in How to Call an SPL Script in Java.

 

Q & A Collection

https://stackoverflow.com/questions/61043157/java-insert-records-into-sql-table-by-reading-a-csv-file