View a markdown version of this page

Write to Iceberg tables - Amazon Athena
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Write to Iceberg tables

The following procedure creates an Iceberg table in an S3 table bucket, inserts and updates rows, and reads the table. Start a new local session and complete Install the extension to load the extension and create athena_creds.

Step 1: Attach your table bucket

Run the following command. Replace us-west-2, my-duckdb-workgroup, amzn-s3-demo-table-bucket, and retail with your Region, workgroup, table bucket, and namespace. The command creates the local sales catalog alias.

ATTACH 'athena://us-west-2/my-duckdb-workgroup' AS sales (TYPE athena, Catalog 's3tablescatalog/amzn-s3-demo-table-bucket', Database 'retail', Secret 'athena_creds');

Step 2: Create an Iceberg table

Run the following command to create the example orders table in your namespace.

CREATE TABLE sales.retail.orders (id INTEGER, product VARCHAR, qty INTEGER) WITH (type = 'ICEBERG');

Step 3: Insert and update rows

Run the following commands to insert two example rows and update the quantity for the first row.

INSERT INTO sales.retail.orders VALUES (1, 'Widget A', 3), (2, 'Widget B', 5); UPDATE sales.retail.orders SET qty = 4 WHERE id = 1;

Step 4: Read the table

Run the following query to read the example table.

SELECT * FROM sales.retail.orders ORDER BY id;

Delete rows

After reading the example table, run the following command to delete its second row.

DELETE FROM sales.retail.orders WHERE id = 2;

Create a table from a query and add a column

The following commands create an Iceberg table from the orders table, read it, and add a column to orders. Both the source and destination belong to the same attached sales catalog.

CREATE TABLE sales.retail.widget_a_orders WITH (type = 'ICEBERG') AS SELECT * FROM sales.retail.orders WHERE product = 'Widget A'; SELECT * FROM sales.retail.widget_a_orders ORDER BY id; ALTER TABLE sales.retail.orders ADD COLUMN note VARCHAR; DESCRIBE sales.retail.orders;

Insert rows from another Athena table

After creating widget_a_orders in the preceding example, copy rows from orders with INSERT SELECT. Both tables belong to the same attached sales catalog. Run this example before cleanup. This command appends another copy of the rows already selected by CTAS; omit the INSERT if you want to keep a single copy.

-- Copy matching rows with a single remote statement. INSERT INTO sales.retail.widget_a_orders (id, product, qty) SELECT id, product, qty FROM sales.retail.orders WHERE product = 'Widget A'; -- Inspect the destination after the write. SELECT * FROM sales.retail.widget_a_orders ORDER BY id;

Create a schema and a view

The following examples use the Glue catalog attached as ath. The first creates and drops an empty example database. The second creates, reads, and drops a view of a table with c1 and c2 columns.

CREATE SCHEMA ath.duckdb_demo; DROP SCHEMA ath.duckdb_demo;
CREATE VIEW ath.my_database.demo_view AS SELECT c1, c2 FROM ath.my_database.my_table WHERE c1 IS NOT NULL; SELECT count(*) FROM ath.my_database.demo_view; DROP VIEW ath.my_database.demo_view;

Write requirements and limitations

Direct writes and DDL targeting attached Athena catalogs require autocommit. The extension refuses these direct statements inside an explicit local transaction. Finish the local transaction first. A local ROLLBACK cannot undo a remote Athena write.

A direct write is sent to Athena as one statement. Every table it reads must be in the catalog being written, and the query must be eligible for pushdown. An INSERT, CREATE TABLE AS, or COPY FROM that reads local tables, local files, or a different attached catalog is refused. To load local data, first stage it in Amazon S3 and make it available as a table in the target catalog.

Qualify the database in write and DDL targets, or select a resolvable database with the attachment's Database option, USE, or the search path. An unresolved default main target is refused. An INSERT with a RETURNING clause is refused; run the write and then read with SELECT. A catalog attached with READ_ONLY refuses writes.

Submit a statement with athena_execute

The following command uses athena_execute to submit a statement exactly as written and wait for completion. It returns one Count row with the affected-row count when Athena reports one, or 0 otherwise.

athena_execute and athena_query can submit writes while an explicit local transaction is open, including a local read-only transaction. They do not use the direct-write autocommit guard. A local ROLLBACK cannot undo those writes. Use autocommit for remote writes and use a READ_ONLY attachment to refuse writes through these functions.

CALL athena_execute('sales', 'INSERT INTO retail.orders (id, product, qty) VALUES (3, ''Widget C'', 2)');

Clean up the example tables

After completing the table-bucket examples, drop their two tables and detach the local catalog.

DROP TABLE sales.retail.widget_a_orders; DROP TABLE sales.retail.orders; DETACH sales;