esProc Improves Text-processing – Merge Sorted Data

Uncategorized 794 0

Many databases support the MERGE statement and thus can merge two tables conveniently. Database syntax cannot be used directly, however, if the data to be merged are the text data. In this case esProc is able to perform what MERGE statements can do based on the text files.   

The two files – table1.txt and table2.txt – have the same structure but contain different data. According to the logical primary key defined on column A and column B, table1 will be merged with table2. The operation goes like this: update table1 when the primary key values of the two files are same but values of other columns are different; insert data into table1 when the primary key values are different. 

Assume that the original data have been sorted by the logical primary key and make the result set ordered based on the primary key, then you can perform the operation repeatedly and merge new data to the target file. 

Below is the original data:

esProc_text_merge_sort_1

Merge Table 1 with Table 2 and the result is as follows:

esProc_text_merge_sort_2

esProc code is simple:

        

A

 

1

=file(“D:\\table1.txt”).cursor@t() =file(“D:\\table2.txt”).cursor@t()

2

=[B1,A1].merge@xu(A,B)  

3

=file(“D:\\result.txt”).export@t(A2)  

A1,B1:Read table1.txt and table2.txt as cursors.

A2:Merge A1 with B1 by the logical primary key. merge function unions data together, ensuring the result set is still ordered. @x represents cursor-handling and @u represents union operation.

A3:Write the result to a new a new file.

Cursor is used in the code to handle data files of any size, even those having big data.

FAVOR (0)
Leave a Reply
Cancel
Icon

Hi,You need to fill in the Username and Email!

  • Username (*)
  • Email (*)
  • Website