View a markdown version of this page

使用分析工具查詢 S3 Storage Lens 資料 - Amazon Simple Storage Service

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

使用分析工具查詢 S3 Storage Lens 資料

在使用 Amazon Athena 或 Amazon EMR 等分析服務查詢匯出至 S3 Tables 的 S3 Storage Lens 資料之前,您必須在 AWS受管 `aws-s3` 資料表儲存貯體上啟用分析整合,並設定 AWS Lake Formation 許可。 S3 AWS

重要

在「aws-s3」資料表儲存貯體上啟用分析整合是經常遺漏的必要步驟。如果沒有此組態,您將無法使用 AWS 分析服務查詢 S3 Storage Lens 資料表。

先決條件

開始之前,請確定您已:

  • 啟用 S3 Tables 匯出的 S3 Storage Lens 組態。如需詳細資訊,請參閱 將 S3 Storage Lens 指標匯出至 S3 資料表

  • 存取 Amazon Athena 或其他分析服務。

  • 啟用第一個資料可用的匯出後,等待 24-48 小時。

整合概觀

如需整合 S3 Tables 與 AWS 分析服務的詳細資訊,包括先決條件、IAM 角色組態和step-by-step程序,請參閱整合 Amazon S3 Tables 與 AWS 分析服務。

啟用 S3 Tables 匯出和設定分析整合之後,您可以使用 Amazon Athena、Amazon Redshift 和 Amazon EMR 等 AWS 分析服務查詢 S3 Storage Lens 資料。這可讓您執行自訂分析、建立儀表板,以及使用標準 SQL 從儲存資料衍生洞見。

使用 Amazon Athena 查詢

Amazon Athena 是一種無伺服器互動式查詢服務,可讓您輕鬆地使用標準 SQL 分析資料。使用下列步驟在 Athena 中查詢 S3 Storage Lens 資料。

注意

在所有查詢範例中,將 取代lens_my-config_exp為您實際的 Storage Lens 組態命名空間。如需命名命名的詳細資訊,請參閱 S3 Storage Lens 匯出至 S3 Tables 的資料表命名

範例:查詢熱門儲存取用者

下列查詢會依儲存體使用量識別前 10 個儲存貯體:

SELECT bucket_name, storage_class, SUM(storage_bytes) / POWER(1024, 3) AS storage_gb, SUM(object_count) AS objects FROM "s3tablescatalog/aws-s3"."lens_my-config_exp"."default_storage_metrics" WHERE report_time = ( SELECT MAX(report_time) FROM "s3tablescatalog/aws-s3"."lens_my-config_exp"."default_storage_metrics" ) AND record_type = 'BUCKET' AND bucket_name != '' GROUP BY bucket_name, storage_class ORDER BY storage_gb DESC LIMIT 10

範例:分析一段時間內的儲存成長

下列查詢會分析過去 30 天的儲存成長:

SELECT CAST(report_time AS date) AS report_date, SUM(storage_bytes) / POWER(1024, 3) AS total_storage_gb FROM "s3tablescatalog/aws-s3"."lens_my-config_exp"."default_storage_metrics" WHERE report_time >= current_date - interval '30' day AND record_type = 'ACCOUNT' GROUP BY CAST(report_time AS date) ORDER BY report_date DESC;

範例:識別未完成的分段上傳

下列查詢會尋找具有超過 7 天未完成分段上傳的儲存貯體:

SELECT bucket_name, SUM(incomplete_mpu_storage_older_than_7_days_bytes) / POWER(1024, 3) AS wasted_storage_gb, SUM(incomplete_mpu_object_older_than_7_days_count) AS wasted_objects FROM "s3tablescatalog/aws-s3"."lens_my-config_exp"."default_storage_metrics" WHERE report_time = ( SELECT MAX(report_time) FROM "s3tablescatalog/aws-s3"."lens_my-config_exp"."default_storage_metrics" ) AND record_type = 'BUCKET' AND incomplete_mpu_storage_older_than_7_days_bytes > 0 GROUP BY bucket_name ORDER BY wasted_storage_gb DESC;

範例:尋找冷資料候選項目

下列查詢會識別過去 100 天內沒有活動的字首,這些字首存放在熱儲存層中:

WITH recent_activity AS ( SELECT DISTINCT bucket_name, record_value AS prefix_path FROM "s3tablescatalog/aws-s3"."lens_my-config_exp"."expanded_prefixes_activity_metrics" WHERE report_time >= current_date - interval '100' day AND record_type = 'PREFIX' AND all_request_count > 0 ) SELECT s.bucket_name, s.record_value AS prefix_path, s.storage_class, SUM(s.storage_bytes) / POWER(1024, 3) AS storage_gb FROM "s3tablescatalog/aws-s3"."lens_my-config_exp"."expanded_prefixes_storage_metrics" s LEFT JOIN recent_activity r ON s.bucket_name = r.bucket_name AND s.record_value = r.prefix_path WHERE s.report_time = ( SELECT MAX(report_time) FROM "s3tablescatalog/aws-s3"."lens_my-config_exp"."expanded_prefixes_storage_metrics" ) AND s.record_type = 'PREFIX' AND s.storage_class IN ('STANDARD', 'REDUCED_REDUNDANCY') AND s.storage_bytes > 1073741824 -- > 1GB AND r.prefix_path IS NULL -- No recent activity GROUP BY s.bucket_name, s.record_value, s.storage_class ORDER BY storage_gb DESC LIMIT 20;

範例:分析請求模式

下列查詢會分析請求模式,以了解存取頻率:

SELECT bucket_name, SUM(all_request_count) AS total_requests, SUM(get_request_count) AS get_requests, SUM(put_request_count) AS put_requests, ROUND(100.0 * SUM(get_request_count) / NULLIF(SUM(all_request_count), 0), 2) AS get_percentage, SUM(downloaded_bytes) / POWER(1024, 3) AS downloaded_gb FROM "s3tablescatalog/aws-s3"."lens_my-config_exp"."default_activity_metrics" WHERE report_time >= current_date - interval '7' day AND record_type = 'BUCKET' AND bucket_name != '' GROUP BY bucket_name HAVING SUM(all_request_count) > 0 ORDER BY total_requests DESC LIMIT 10;

在 Amazon EMR 上使用 Apache Spark 查詢

Amazon EMR 提供受管 Hadoop 架構,可讓您使用 Apache Spark 輕鬆處理大量資料。您可以使用 Iceberg 連接器直接讀取 S3 Storage Lens 資料表。

使用 Spark 讀取 S3 資料表

使用下列 Python 程式碼,透過 Spark 讀取 S3 Storage Lens 資料:

from pyspark.sql import SparkSession spark = SparkSession.builder \ .appName("S3StorageLensAnalysis") \ .config("spark.sql.catalog.s3tablescatalog", "org.apache.iceberg.spark.SparkCatalog") \ .config("spark.sql.catalog.s3tablescatalog.catalog-impl", "org.apache.iceberg.aws.glue.GlueCatalog") \ .getOrCreate() # Read S3 Storage Lens data df = spark.read \ .format("iceberg") \ .load("s3tablescatalog/aws-s3.lens_my-config_exp.default_storage_metrics") # Analyze data df.filter("record_type = 'BUCKET'") \ .groupBy("bucket_name", "storage_class") \ .sum("storage_bytes") \ .orderBy("sum(storage_bytes)", ascending=False) \ .show(10)

查詢最佳化最佳實務

遵循這些最佳實務來最佳化查詢效能並降低成本:

  • 依 report_time 篩選 – 一律包含日期篩選條件,以減少掃描的資料量。這對保留期較長的資料表尤其重要。

    WHERE report_time >= current_date - interval '7' day
  • 使用 record_type 篩選條件 – 指定適當的彙總層級 (ACCOUNT、BUCKET、 PREFIX),以僅查詢您需要的資料。

    WHERE record_type = 'BUCKET'
  • 包含 LIMIT 子句 – 針對探索性查詢使用 LIMIT,以控制結果大小並降低查詢成本。

    LIMIT 100
  • 篩選空白記錄 – 使用條件來排除空白或零值記錄。

    WHERE storage_bytes > 0
  • 使用最新資料 – 分析目前狀態時,請篩選最新的 report_time,以避免掃描歷史資料。

    WHERE report_time = (SELECT MAX(report_time) FROM table_name)

最佳化查詢模式範例

下列查詢示範最佳化的最佳實務:

SELECT bucket_name, SUM(storage_bytes) / POWER(1024, 3) AS storage_gb FROM "s3tablescatalog/aws-s3"."lens_my-config_exp"."default_storage_metrics" WHERE report_time >= current_date - interval '7' day -- Date filter AND record_type = 'BUCKET' -- Record type filter AND storage_bytes > 0 -- Non-empty filter AND bucket_name != '' -- Non-empty filter GROUP BY bucket_name ORDER BY storage_gb DESC LIMIT 100; -- Result limit

疑難排解

查詢不會傳回任何結果

問題:您的查詢已成功完成,但不會傳回任何結果。

解決方案

  • 檢查最新的 report_time 以確認資料是否可用:

    SELECT MAX(report_time) AS latest_data FROM "s3tablescatalog/aws-s3"."lens_my-config_exp"."default_storage_metrics";
  • 請確定您使用的是正確的命名空間名稱。使用 SHOW TABLES IN `lens_my-config_exp`; 列出可用的資料表。

  • 在啟用 S3 Tables 匯出後等待 24-48 小時,第一個資料才可使用。

存取遭拒錯誤

問題:您在執行查詢時收到存取遭拒錯誤。

解決方案:確認已正確設定 AWS Lake Formation 許可。如需詳細資訊,請參閱將 Amazon S3 Tables 與 AWS 分析服務整合。

後續步驟