We currently have some data on an HDFS cluster on which we generate reports using Hive. The infrastructure is in the process of being decommissioned and we are left with the task of coming up with an alternative of generating the report on the data (which we imported as tab separated files into our new environment)
Assuming we have a table with the following fields.
- Query
- IPAddress
- LocationCode
Our original SQL query we used to run on Hive was (well not exactly.. but something similar)
select
COUNT(DISTINCT Query, IPAddress) as c1,
LocationCode as c2,
Query as c3
from table
group by Query, LocationCode
I was wondering if someone could provide me with an the most efficient script using standard unix/linux tools such as sort, uniq and awk which can act as a replacement for the above query.
Assume the input to the script would be a directory of text files. the dir would contain about 2000 files. Each file would contain arbitrary number of tab separated records of the form :
Query <TAB> LocationCode <TAB> IPAddress <NEWLINE>
Once you have a sorted file containing all the unique
you could:
To get this
sorted_uniq_filethe naive way can be:But this can be very long and memory consuming.
A faster option (and less memory consuming) could be to eliminate duplicate as soon as possible, sorting first and merging later. This needs a temporary space for the sorted file, let use a directory named
sorted:If the solution above hit some shell or sort limit (expansion of
dir/*, or ofsorted/*, or number of parameters ofsort):The solution above can be optimized to merge more that 2 files at the same time.