Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.
Esempi di query di tabelle di metadati
Gli esempi seguenti mostrano come sia possibile ottenere informazioni di diverso tipo dalle tabelle dei metadati S3 utilizzando query SQL standard.
Quando utilizzi questi esempi, ricorda che:
-
Gli esempi sono scritti per funzionare con Amazon Athena. Potrebbe essere necessario modificare gli esempi per lavorare con un motore di query diverso.
-
Assicurati di capire come ottimizzare le query.
-
b_
Sostituiscilo con il nome del tuo namespace.general-purpose-bucket-name
-
Per un elenco completo delle colonne supportate, consulta e. Schema delle tabelle del journal S3 Metadata Schema delle tabelle di inventario in tempo reale di S3 Metadata
Indice
Unione di metadati personalizzati con le tabelle di metadati S3
Visualizzazione dei dati delle tabelle di metadati con Amazon QuickSight
Domande di esempio relative alla tabella Journal
È possibile utilizzare le seguenti query di esempio per interrogare le tabelle del diario.
Ricerca di oggetti per estensione di file
La seguente query restituisce oggetti con un'estensione di file specifica (.jpg
in questo caso):
SELECT key FROM "s3tablescatalog/aws-s3"."
b_
"."journal" WHERE key LIKE '%.jpg' AND record_type = 'CREATE'general-purpose-bucket-name
Elenco delle eliminazioni di oggetti
La seguente query restituisce gli eventi di eliminazione degli oggetti, incluso l' Account AWS ID o il principale del AWS servizio che ha effettuato la richiesta:
SELECT DISTINCT bucket, key, sequence_number, record_type, record_timestamp, requester, source_ip_address, version_id FROM "s3tablescatalog/aws-s3"."
b_
"."journal" WHERE record_type = 'DELETE';general-purpose-bucket-name
Elenco delle chiavi di AWS KMS crittografia utilizzate dai tuoi oggetti
La seguente query restituisce ARNs le chiavi AWS Key Management Service (AWS KMS) che crittografano gli oggetti:
SELECT DISTINCT kms_key_arn FROM "s3tablescatalog/aws-s3"."
b_
"."journal";general-purpose-bucket-name
Elenco di oggetti che non utilizzano le chiavi KMS
La seguente query restituisce oggetti che non sono crittografati con AWS KMS chiavi:
SELECT DISTINCT kms_key_arn FROM "s3tablescatalog/aws-s3"."
b_
"."journal" WHERE encryption_status NOT IN ('SSE-KMS', 'DSSE-KMS') AND record_type = 'CREATE';general-purpose-bucket-name
Elenco delle chiavi di AWS KMS crittografia utilizzate per PUT
le operazioni negli ultimi 7 giorni
La seguente query restituisce ARNs le chiavi AWS Key Management Service (AWS KMS) che crittografano gli oggetti:
SELECT DISTINCT kms_key_arn FROM "s3tablescatalog/aws-s3"."
b_
"."journal" WHERE record_timestamp > (current_date - interval '7' day) AND kms_key_arn is NOT NULL;general-purpose-bucket-name
Elenco degli oggetti eliminati nelle ultime 24 ore da S3 Lifecycle
La seguente query restituita elenca gli oggetti scaduti nell'ultimo giorno da S3 Lifecycle:
SELECT bucket, key, version_id, last_modified_date, record_timestamp, requester FROM "s3tablescatalog/aws-s3"."
b_
"."journal" WHERE requester = 's3.amazonaws.com' AND record_type = 'DELETE' AND record_timestamp > (current_date - interval '1' day)general-purpose-bucket-name
Visualizzazione dei metadati forniti da Amazon Bedrock
Alcuni AWS servizi (come Amazon Bedrock) caricano oggetti su Amazon S3. È possibile interrogare i metadati degli oggetti forniti da questi servizi. Ad esempio, la seguente query include la user_metadata
colonna per determinare se ci sono oggetti caricati da Amazon Bedrock in un bucket generico:
SELECT DISTINCT bucket, key, sequence_number, record_type, record_timestamp, user_metadata FROM "s3tablescatalog/aws-s3"."
b_
"."journal" WHERE record_type = 'CREATE' AND user_metadata['content-source'] = 'AmazonBedrock';general-purpose-bucket-name
Se Amazon Bedrock ha caricato un oggetto nel bucket, la colonna user_metadata
mostrerà i seguenti metadati associati all'oggetto nel risultato della query:
user_metadata {content-additional-params -> requestid="CVK8FWYRW0M9JW65", signedContentSHA384="38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b", content-model-id -> bedrock-model-arn, content-source -> AmazonBedrock}
Comprensione dello stato attuale degli oggetti
La seguente query può aiutare a determinare lo stato attuale degli oggetti. La query identifica la versione più recente di ogni oggetto, filtra gli oggetti eliminati e contrassegna la versione più recente di ogni oggetto in base ai numeri di sequenza. I risultati sono ordinati in base alle colonne bucket
, key
e sequence_number
.
WITH records_of_interest as ( -- Start with a query that can narrow down the records of interest. SELECT * from "s3tablescatalog/aws-s3"."
b_
"."journal" ), version_stacks as ( SELECT *, -- Introduce a column called 'next_sequence_number', which is the next larger -- sequence_number for the same key version_id in sorted order. LEAD(sequence_number, 1) over (partition by (bucket, key, coalesce(version_id, '')) order by sequence_number ASC) as next_sequence_number from records_of_interest ), -- Pick the 'tip' of each version stack triple: (bucket, key, version_id). -- The tip of the version stack is the row of that triple with the largest sequencer. -- Selecting only the tip filters out any row duplicates. -- This isn't typical, but some events can be delivered more than once to the table -- and include rows that might no longer exist in the bucket (since the -- table contains rows for both extant and extinct objects). -- In the next subquery, eliminate the rows that contain deleted objects. current_versions as ( SELECT * from version_stacks where next_sequence_number is NULL ), -- Eliminate the rows that are extinct from the bucket by filtering with -- record_type. An object version has been deleted from the bucket if its tip is -- record_type==DELETE. existing_current_versions as ( SELECT * from current_versions where not (record_type = 'DELETE' and is_delete_marker = FALSE) ), -- Optionally, to determine which of several object versions is the 'latest', -- you can compare their sequence numbers. A version_id is the latest if its -- tip's sequencer is the largest among all other tips in the same key. with_is_latest as ( SELECT *, -- Determine if the sequence_number of this row is the same as the largest sequencer for the key that still exists. sequence_number = (MAX(sequence_number) over (partition by (bucket, key))) as is_latest_version FROM existing_current_versions ) SELECT * from with_is_latest ORDER BY bucket, key, sequence_number;general-purpose-bucket-name
Domande di esempio relative alla tabella di inventario
È possibile utilizzare le seguenti query di esempio per interrogare le tabelle di inventario.
Scoperta di set di dati che utilizzano tag specifici
La seguente query restituisce il set di dati che utilizza i tag specificati:
SELECT * FROM "s3tablescatalog/aws-s3"."
b_
"."inventory" WHERE object_tags['key1'] = 'value1' AND object_tags['key2'] = 'value2';general-purpose-bucket-name
Elenco di oggetti non crittografati con SSE-KMS
La seguente query restituisce oggetti non crittografati con SSE-KMS:
SELECT key, encryption_status FROM "s3tablescatalog/aws-s3"."
b_
"."inventory" WHERE encryption_status != 'SSE-KMS';general-purpose-bucket-name
Elenco di oggetti non crittografati
La seguente query restituisce oggetti non crittografati:
SELECT bucket, key, version_id FROM "s3tablescatalog/aws-s3"."
b_
"."inventory" WHERE encryption_status IS NULL;general-purpose-bucket-name
Elencare oggetti generati da Amazon Bedrock
La seguente query elenca gli oggetti generati da Amazon Bedrock:
SELECT DISTINCT bucket, key, sequence_number, user_metadata FROM "s3tablescatalog/aws-s3"."
b_
"."inventory" WHERE user_metadata['content-source'] = 'AmazonBedrock';general-purpose-bucket-name
Riconciliazione della tabella dell'inventario con la tabella del diario
La seguente query genera un inventory-table-like elenco aggiornato con il contenuto corrente del bucket. Più precisamente, l'elenco risultante combina l'istantanea più recente della tabella di inventario con gli ultimi eventi nella tabella del diario.
Affinché questa query produca i risultati più accurati, sia la tabella di giornale che quella di inventario devono essere nello stato Attivo.
Si consiglia di utilizzare questa query per bucket generici contenenti meno di un miliardo (10^9) di oggetti.
Questa query di esempio applica le seguenti semplificazioni ai risultati dell'elenco (rispetto alla tabella di inventario):
-
Omissioni nelle colonne: le colonne
bucket
,,is_multipart
,encryption_status
is_bucket_key_enabled
kms_key_arn
, echecksum_algorithm
non fanno parte dei risultati finali. Mantenere al minimo il set di colonne opzionali migliora le prestazioni. -
Inclusione di tutti i record: la query restituisce tutte le chiavi e le versioni degli oggetti, inclusa la versione nulla (in bucket senza versione o con versione sospesa) e i marker di eliminazione. Per esempi su come filtrare i risultati per mostrare solo le chiavi che ti interessano, consulta la clausola alla fine della
WHERE
query. -
Riconciliazione accelerata: la query potrebbe, in rari casi, segnalare temporaneamente oggetti che non sono più nel bucket. Queste discrepanze vengono eliminate non appena diventa disponibile la successiva istantanea della tabella di inventario. Questo comportamento è un compromesso tra prestazioni e precisione.
WITH inventory_time_cte AS ( SELECT COALESCE(inventory_time_from_property, inventory_time_default) AS inventory_time FROM ( SELECT * FROM (VALUES (TIMESTAMP '2024-12-01 00:00')) AS T (inventory_time_default) LEFT OUTER JOIN ( SELECT from_unixtime(CAST(value AS BIGINT) / 1000.0) AS inventory_time_from_property FROM "journal$properties" WHERE key = 'aws.s3metadata.oldest-uncoalesced-record-timestamp' LIMIT 1 ) ON TRUE ) ), working_set AS ( SELECT key, sequence_number, version_id, is_delete_marker, size, COALESCE(last_modified_date, record_timestamp) AS last_modified_date, e_tag, storage_class, object_tags, user_metadata, (record_type = 'DELETE' AND NOT COALESCE(is_delete_marker, FALSE)) AS _is_perm_delete FROM journal j CROSS JOIN inventory_time_cte t WHERE j.record_timestamp > (t.inventory_time - interval '15' minute) UNION ALL SELECT key, sequence_number, version_id, is_delete_marker, size, last_modified_date, e_tag, storage_class, object_tags, user_metadata, FALSE AS _is_perm_delete FROM inventory i ), updated_inventory AS ( SELECT * FROM ( SELECT *, MAX(sequence_number) OVER (PARTITION BY key, version_id) AS _supremum_sn FROM working_set ) WHERE sequence_number = _supremum_sn ) SELECT key, sequence_number, version_id, is_delete_marker, size, last_modified_date, e_tag, storage_class, object_tags, user_metadata FROM updated_inventory -- This filter omits only permanent deletes from the results. Delete markers will still be shown. WHERE NOT _is_perm_delete -- You can add additional filters here. Examples: -- AND object_tags['department'] = 'billing' -- AND starts_with(key, 'reports/') ORDER BY key ASC, sequence_number DESC