I have a bunch of files on S3 that contain just MD5s, one per line. I created an AWS Athena table to run a de-duplication query against the MD5s. In total there are hundreds of millions of MD5s in those files and in the table.
Athena Table Creation Query:
CREATE EXTERNAL TABLE IF NOT EXISTS database.md5s (
`md5` string
)
ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe'
WITH SERDEPROPERTIES (
'serialization.format' = ',',
'field.delim' = ','
) LOCATION 's3://bucket/folder/';
Here are all the "dedup" queries I've tried (These should all be the same):
SELECT DISTINCT md5
FROM md5s;`
SELECT md5
FROM md5s
GROUP BY md5;
SELECT md5
FROM md5s
GROUP BY DISTINCT md5;
SELECT DISTINCT md5
FROM md5s
GROUP BY DISTINCT md5;
All results output .csvs from Athena still have repeated MD5s. What gives?
Is Athena Doing Partial Deduplication? - Even more peculiar, if I perform a COUNT(DISTINCT md5)
in Athena, the count I get is different than the number of rows returned on export.
COUNT(DISTINCT md5)
in Athena: 97,533,226 Is Athena CREATING Duplicates on Export? - The plot thickens. If I query my Athena Table for one of the MD5s that is duplicated in the Athena result export, I only get one result/row from the table. I tested this with a LIKE
query to make sure whitespace wasn't causing the issue. This means Athena is ADDING duplicates to the export. There are never more than two of the same MD5 in the results.
select
md5,
to_utf8(md5)
from md5s
where md5 like '%0061c3d72c2957f454eef9d4b05775d7%';
Are Athena's Counts & Results File Both Wrong? - I deduped these same records using MySQL, and ended up with 97,531,010 unique MD5s. Athenas counts and results details are below.
COUNT(DISTINCT md5)
in Athena: 97,533,226 I think this is an Athena bug - I've filed a ticket with AWS's dev team to get this fixed, and will update this post when it is.
Here is the related AWS Forum Post where other users are seeing the same issues. https://forums.aws.amazon.com/thread.jspa?messageID=764702
SELECT ALL columns FROM table; The syntax diagram is: SELECT [ALL | DISTINCT] columns FROM table; If a table has a properly defined primary key, SELECT DISTINCT * FROM table; and SELECT * FROM table; return identical results because all rows are unique.
One way to find duplicate records from the table is the GROUP BY statement. The GROUP BY statement in SQL is used to arrange identical data into groups with the help of some functions. i.e if a particular column has the same values in different rows then it will arrange these rows in a group.
Athena is a great choice for getting started with analytics if you have nothing set up yet. Redshift Spectrum is great for Redshift customers. Presto is for everything else, including large data sets, more regular analytics, and higher user concurrency.
I have confirmed with the AWS team, that this was a known bug with AWS Athena at the time the question was asked. I'm not sure if this has been resolved.
When in doubt please use CTAS to remove any duplicates :
CREATE TABLE new_table
WITH (
format = 'Parquet',
parquet_compression = 'SNAPPY')
AS SELECT DISTINCT *
FROM old_table;
Reference: https://docs.aws.amazon.com/athena/latest/ug/ctas-examples.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With