Querying Iceberg Table Data Using Spark
To use Iceberg in Spark, first configure Spark catalogs. For details about how to configure spark_catalog and prod catalogs, see Iceberg Catalog Configuration. Iceberg uses Apache Spark's DataSourceV2 API for data sources and catalog implementations.
Querying table data
In Spark 3, tables use identifiers that include a catalog name. Querying table data includes:
Create a table. For example:
CREATE TABLE prod.db.table (id bigint, data string, category string, ts timestamp, shard int) USING iceberg PARTITIONED BY (category);
- Query table data.
SELECT * FROM prod.db.table;
- Metadata tables, like the history and snapshots, can use the Iceberg table name as a namespace. For example:
SELECT * FROM prod.db.table.files;
Loading Tables as DataFrames
To load a table as a DataFrame, use spark.table.
//spark-shell
val df = spark.table("prod.db.table") Using Catalogs with DataFrameReader
Paths and table names can be loaded with Spark's DataFrameReader API. How tables are loaded depends on how the identifier is specified. When spark.read.format("iceberg").load(table) or spark.table(table) is used, the table variable can take a number of forms as listed below.
- file:///path/to/table: loads a Hadoop-based Iceberg table from a given path.
- tablename: loads from current namespace of the current catalog, that is currentCatalog.currentNamespace.tablename.
- catalog.tablename: loads tables from the specified catalog.
- namespace.tablename: loads tables in the specified namespace from the current catalog.
- catalog.namespace.tablename: loads tables in the specified namespace from the specified catalog.
- namespace1.namespace2.tablename: loads tables in a multi-level namespace from the current catalog.
The above list is in order of priority. For example, a matching catalog will take priority over any namespace resolution.
Time Travel
- SQL Commands
Spark 3.3 and later versions support time travel in SQL queries using TIMESTAMP AS OF or VERSION AS OF clauses. The VERSION AS OF clause can contain a long snapshot ID or a string branch or tag name.
- Time travel to October 26, 1986 at 01:21:00
SELECT * FROM prod.db.table TIMESTAMP AS OF '1986-10-26 01:21:00';
- Time travel to the snapshot with ID 10963874102873
SELECT * FROM prod.db.table VERSION AS OF 10963874102873;
Command for querying the ID of snapshot current-snapshot-id:
DESCRIBE EXTENDED prod.db.table;
- Time travel to the head snapshot of audit-branch.
SELECT * FROM prod.db.table VERSION AS OF 'audit-branch';
- Time travel to the snapshot referenced by the tag historical-snapshot.
SELECT * FROM prod.db.table VERSION AS OF 'historical-snapshot';
In addition, FOR SYSTEM_TIME AS OF and FOR SYSTEM_VERSION AS OF clauses are also supported. They provide the same functions as TIMESTAMP AS OF and VERSION AS OF.
- Example 1:
SELECT * FROM prod.db.table FOR SYSTEM_TIME AS OF '1986-10-26 01:21:00';
- Example 2:
SELECT * FROM prod.db.table FOR SYSTEM_VERSION AS OF 10963874102873;
- Example 3:
SELECT * FROM prod.db.table FOR SYSTEM_VERSION AS OF 'audit-branch';
- Example 4:
SELECT * FROM prod.db.table FOR SYSTEM_VERSION AS OF 'historical-snapshot';
Timestamps may also be supplied as a Unix timestamp, in seconds. For example:
SELECT * FROM prod.db.table TIMESTAMP AS OF 499162860;
or
SELECT * FROM prod.db.table FOR SYSTEM_TIME AS OF 499162860;
The branch or tag may also be specified using a similar syntax to metadata tables, with branch_<Branch name> or tag_<Tag name>.
SELECT * FROM prod.db.table.`branch_audit-branch`;
or
SELECT * FROM prod.db.table.`tag_historical-snapshot`;
Identifiers with - are not valid, and so must be escaped using back quotes (`). Note that the identifier with branch or tag (specified by branch_<Branch name> or tag_<Tag name>) may not be used in combination with VERSION AS OF.
- Time travel to October 26, 1986 at 01:21:00
- Schema selection in time travel queries
Different time travel queries can use either the snapshot's schema or the table's schema based on their purposes. The rules are as follows:
- Use the snapshot's schema.
- Example 1:
SELECT * FROM prod.db.table TIMESTAMP AS OF '1986-10-26 01:21:00';
- Example 2:
SELECT * FROM prod.db.table VERSION AS OF 10963874102873;
- Example 1:
- Use the table's schema.
- Example 1:
SELECT * FROM prod.db.table VERSION AS OF 'audit-branch';
- Example 2:
SELECT * FROM prod.db.table.`branch_audit-branch`;
- Example 1:
- Use the snapshot's schema.
- Example 1:
SELECT * FROM prod.db.table VERSION AS OF 'historical-snapshot';
- Example 2:
SELECT * FROM prod.db.table.`tag_historical-snapshot`;
- Example 1:
- Use the snapshot's schema.
- Time travel queries with DataFrame
To select a specific table snapshot or the snapshot at a specified time point in the DataFrame API, Iceberg supports four Spark read options:
- snapshot-id: Select a specific table snapshot. You need to specify the snapshot ID.
- as-of-timestamp: Select the current snapshot at a Unix timestamp, in milliseconds.
- branch: Select the head snapshot of the specified branch.
- tag: Select the snapshot associated with the specified tag.
For example:
- Time travel to October 26, 1986 at 01:21:00
spark.read.option("as-of-timestamp", "499162860000").format("iceberg").load("path/to/table") - Time travel to the snapshot with ID 10963874102873
spark.read.option("snapshot-id", 10963874102873L).format("iceberg").load("path/to/table") - Time travel to the snapshot referenced by the tag historical-snapshot.
spark.read.option(SparkReadOptions.TAG, "historical-snapshot").format("iceberg").load("path/to/table") - Time travel to the head snapshot of audit-branch.
spark.read.option(SparkReadOptions.BRANCH, "audit-branch").format("iceberg").load("path/to/table")
- Incremental read
To read appended data incrementally, use the following options:
- start-snapshot-id: Start snapshot ID used in incremental scans (exclusive).
- end-snapshot-id: End snapshot ID used in incremental scans (inclusive). This is optional. Omitting it will default to the current snapshot.
spark.read.format("iceberg").option("start-snapshot-id", "10963874102873").option("end-snapshot-id", "63874143573109").load("path/to/table")
Inspecting Tables
To inspect a table's history, snapshots, and other metadata, Iceberg supports metadata tables.
Metadata tables are identified by adding the metadata table name after the original table name. For example, the history for table db.table is read using db.table.history.
- History
Use the following statement to show table history:
SELECT * FROM prod.db.table.history;
- Metadata log entries
Use the following statement to show table metadata log entries:
SELECT * from prod.db.table.metadata_log_entries;
- Snapshots
Use the following statement to show all valid snapshots for a table:
SELECT * FROM prod.db.table.snapshots;
- Entries
Use the following statement to show all the table's current manifest entries for both data and delete files.
SELECT * FROM prod.db.table.entries;
- Files
Use the following statement to show a table's current files:
SELECT * FROM prod.db.table.files;
- Manifests
Use the following statement to show a table's current file manifests:
SELECT * FROM prod.db.table.manifests;
Fields within the partition_summaries column of the manifests table correspond to the field_summary structs within the manifest list, with the following order:
- contains_null
- contains_nan
- lower_bound
- upper_bound
contains_nan could return null, which indicates that this information is not available from the file's metadata. This usually occurs when reading from the V1 table, where contains_nan is not populated.
- Partitions
Use the following statement to show a table's current partitions:
SELECT * FROM prod.db.table.partitions;
For unpartitioned tables, the partitions table will not contain the partition and spec_id fields.
The partitions metadata table shows partitions with data files or delete files in the current snapshot. However, delete files are not applied, and so in some cases partitions may be shown even though all their data rows are marked deleted by delete files.
- Positional delete files
Use the following statement to show all positional delete files from the current snapshot of table:
SELECT * from prod.db.table.position_deletes;
- All data files
Use the following statement to show all of the table's data files and each file's metadata:
SELECT * FROM prod.db.table.all_data_files;
- All delete files
Use the following statement to show the table's delete files and each file's metadata from all the snapshots:
SELECT * FROM prod.db.table.all_delete_files;
- All entries
Use the following statement to show the table's manifest entries from all the snapshots for both data and delete files:
SELECT * FROM prod.db.db.table.all_entries;
- All manifests
Use the following statement to show all of the table's manifest files:
SELECT * FROM prod.db.table.all_manifests;
Fields within the partition_summaries column of the manifests table correspond to the field_summary structs within manifest list, with the following order:
- contains_null
- contains_nan
- lower_bound
- upper_bound
contains_nan could return null, which indicates that this information is not available from the file's metadata. This usually occurs when reading from V1 table, where contains_nan is not populated.
- References
Use the following statement to show a table's known snapshot references:
SELECT * FROM prod.db.table.refs;
- Inspecting with DataFrames
Metadata tables can be loaded using the DataFrameReader API:
- Example 1:
spark.read.format("iceberg").load("db.table.files"); - Example 2:
spark.read.format("iceberg").load("hdfs://nn:8020/path/to/table#files");
- Example 1:
- Time travel with metadata tables
Inspect tables' metadata with the time travel feature.
- Example 1:
SELECT * FROM prod.db.table.manifests TIMESTAMP AS OF '2021-09-20 08:00:00';
- Example 2:
SELECT * FROM prod.db.table.partitions VERSION AS OF 10963874102873;
Metadata tables can also be inspected with time travel using the DataFrameReader API.spark.read.format("iceberg").option("snapshot-id", 10963874102873L).load("db.table.files") - Example 1:
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot