Amazon Athena
Serverless SQL directly on S3. No cluster, no loading step — point it at a bucket, get a schema from the Glue Data Catalog, and run standard SQL. You pay $5.00 per TB of data scanned, which is why almost everything worth knowing about Athena is really about scanning less.
SQL serverless chạy thẳng trên S3. Không cluster, không bước load — trỏ vào bucket, lấy schema từ Glue Data Catalog, rồi chạy SQL chuẩn. Tính tiền $5.00 mỗi TB data quét, nên gần như mọi thứ đáng nhớ về Athena thực chất là chuyện quét ít đi.
Reach for itNên dùng
- Sporadic / ad-hoc SQL over data already in S3 — nothing to run between queriesSQL thi thoảng / ad-hoc trên data đã nằm sẵn trên S3 — giữa các query không tốn gì
- Investigating AWS logs: VPC Flow Logs, ELB access logs, CloudTrail, S3 access logsĐiều tra log AWS: VPC Flow Logs, ELB access log, CloudTrail, S3 access log
- A data lake where the data must stay in S3 and be readable by several enginesData lake nơi data phải ở lại S3 và nhiều engine cùng đọc được
- One SQL statement across several different data stores → Federated QueryMột câu SQL chạy qua nhiều data store khác nhau → Federated Query
Not the right toolKhông hợp
- Sustained BI load, many concurrent dashboards → Redshift (provisioned pays off)Tải BI đều đặn, nhiều dashboard đồng thời → Redshift (provisioned bù lại được)
- Sub-second lookups by key → DynamoDBTra cứu theo key dưới một giây → DynamoDB
- Heavy transformation, not just querying → Glue or EMRBiến đổi nặng chứ không chỉ query → Glue hoặc EMR
- Frequent full scans of a huge raw dataset — the per-TB bill grows faster than a cluster wouldLiên tục quét toàn bộ một dataset thô khổng lồ — hoá đơn theo TB tăng nhanh hơn cả nuôi cluster
1 · Overview1 · Tổng quan
Athena reads S3 in place. The Glue Data Catalog supplies the table definitions — without a schema there is nothing to query.
Athena đọc thẳng trên S3. Glue Data Catalog cung cấp định nghĩa table — không có schema thì không query được gì.
- Serverless query service to analyze data stored in Amazon S3; uses standard SQL (built on Presto, the open-source distributed SQL engine).Dịch vụ query serverless phân tích data trên Amazon S3; dùng SQL chuẩn (xây trên Presto, engine SQL phân tán mã nguồn mở).
- Supports CSV, JSON, ORC, Avro, and Parquet.Hỗ trợ CSV, JSON, ORC, Avro và Parquet.
- Pricing: $5.00 per TB of data scanned. Failed queries are free; you are billed on bytes read, not rows returned.Giá: $5.00 mỗi TB data quét. Query lỗi không tính tiền; tính theo số byte đọc chứ không phải số dòng trả về.
- Commonly used with Amazon QuickSight for reporting and dashboards.Thường dùng chung với Amazon QuickSight để báo cáo và dashboard.
- Use cases: business intelligence / analytics / reporting; analyze and query VPC Flow Logs, ELB logs, CloudTrail trails.Use case: BI / analytics / báo cáo; phân tích và query VPC Flow Logs, ELB log, CloudTrail trail.
- Exam tip: "analyze data in S3 using serverless SQL" → Athena. Every time.Mẹo thi: "phân tích data trên S3 bằng SQL serverless" → Athena. Lần nào cũng vậy.
2 · Performance & cost — scan less2 · Hiệu năng & chi phí — quét ít lại
Speed and cost are the same problem here: both are decided by how many bytes Athena has to read off S3. The four levers stack — apply all of them and the same question reads a fraction of the data. Ở đây tốc độ và chi phí là cùng một bài toán: cả hai do số byte Athena phải đọc từ S3 quyết định. Bốn đòn bẩy cộng dồn — áp dụng hết thì cùng một câu hỏi chỉ đọc một phần nhỏ dữ liệu.
Columnar and partitioning are the big two: one drops columns you never asked for, the other drops files you never needed to open.
Format cột và partition là hai đòn bẩy lớn nhất: một cái bỏ đi những cột bạn không hỏi tới, cái kia bỏ đi những file lẽ ra không cần mở.
- Use columnar data for cost-savings (less scan). Apache Parquet or ORC is recommended — huge performance improvement. A columnar file stores each column together, so reading three columns out of fifty reads roughly 3/50 of the file. Row formats like CSV must read the whole row to reach one field. Use Glue to convert your data to Parquet or ORC.Dùng data dạng cột để tiết kiệm chi phí (quét ít hơn). Nên dùng Apache Parquet hoặc ORC — cải thiện hiệu năng rất lớn. File dạng cột lưu mỗi cột nằm cạnh nhau, nên đọc 3 cột trong 50 cột chỉ đọc khoảng 3/50 file. Format theo dòng như CSV phải đọc cả dòng mới lấy được một field. Dùng Glue để chuyển data sang Parquet hoặc ORC.
- Compress data for smaller retrievals — bzip2, gzip, lz4, snappy, zlib, zstd…Nén data để lấy về ít byte hơn — bzip2, gzip, lz4, snappy, zlib, zstd…
- Partition datasets in S3 for easy querying on virtual columns (see below).Chia partition cho dataset trên S3 để query theo cột ảo (xem bên dưới).
- Use larger files (> 128 MB) to minimize overhead. Thousands of tiny files cost more in per-object round-trips than the bytes are worth.Dùng file lớn (> 128 MB) để giảm overhead. Hàng nghìn file bé tí tốn thời gian gọi từng object hơn cả giá trị số byte đọc được.
What partitioning actually looks likePartition trông như thế nào
Path layout: s3://yourBucket/pathToTable/<COLUMN>=<VALUE>/<COLUMN>=<VALUE>/… — e.g. s3://athena-examples/flight/parquet/year=1991/month=1/day=1/
Bố cục path: s3://yourBucket/pathToTable/<COLUMN>=<VALUE>/<COLUMN>=<VALUE>/… — ví dụ s3://athena-examples/flight/parquet/year=1991/month=1/day=1/
3 · Federated Query3 · Federated Query
The connector is just a Lambda function you deploy per source type. Athena speaks SQL; the Lambda speaks whatever the source speaks.
Connector chỉ là một Lambda function bạn deploy cho mỗi loại nguồn. Athena nói SQL; Lambda nói thứ ngôn ngữ mà nguồn đó hiểu.
- Allows you to run SQL queries across data stored in relational, non-relational, object, and custom data sources — AWS or on-premises.Cho phép chạy query SQL trên data ở nguồn quan hệ, phi quan hệ, object và nguồn tuỳ biến — trên AWS hoặc on-premises.
- Uses Data Source Connectors that run on AWS Lambda (e.g. CloudWatch Logs, DynamoDB, RDS…). AWS publishes them; you can also write your own.Dùng Data Source Connector chạy trên AWS Lambda (ví dụ CloudWatch Logs, DynamoDB, RDS…). AWS phát hành sẵn; bạn cũng tự viết được.
- Store the results back in Amazon S3 — the query result location every Athena workgroup needs.Kết quả được ghi trở lại Amazon S3 — chính là query result location mà mọi workgroup Athena đều cần.
- The federated part is convenience, not speed: the Lambda pulls data out of the source at query time, so it's for joining across stores, not for replacing a warehouse.Phần "federated" là tiện lợi chứ không phải nhanh: Lambda kéo data ra khỏi nguồn ngay lúc query, nên nó hợp để join giữa các store, không phải để thay warehouse.
Exam reflexesPhản xạ khi thi
- "Analyze data in S3 using serverless SQL" → Athena. "Sustained warehouse workload" → Redshift."Phân tích data trên S3 bằng SQL serverless" → Athena. "Workload warehouse đều đặn" → Redshift.
- "Athena queries cost too much / are slow" → convert to Parquet, compress, partition, use bigger files. In that order of impact."Query Athena tốn quá / chậm quá" → chuyển sang Parquet, nén, partition, dùng file lớn hơn. Theo thứ tự tác động đó.
- "Query VPC Flow Logs / ELB logs / CloudTrail without building anything" → Athena."Query VPC Flow Logs / ELB log / CloudTrail mà không phải dựng gì" → Athena.
- "One SQL query joining S3 with DynamoDB / an on-prem database" → Federated Query + Lambda connector."Một câu SQL join S3 với DynamoDB / database on-prem" → Federated Query + Lambda connector.
- "Athena can't see the table" → no Glue Data Catalog entry. Run a crawler or create the table."Athena không thấy table" → chưa có mục trong Glue Data Catalog. Chạy crawler hoặc tạo table.