jackye1995 commented on code in PR #6723:
URL: https://github.com/apache/iceberg/pull/6723#discussion_r1093762783


##########
docs/branching-and-tagging.md:
##########
@@ -0,0 +1,218 @@
+---
+title: "Branching and Tagging"
+url: configuration
+aliases:
+    - "tables/branching"
+menu:
+    main:
+        parent: Tables
+        weight: 0
+---
+
+<!--
+ - Licensed to the Apache Software Foundation (ASF) under one or more
+ - contributor license agreements.  See the NOTICE file distributed with
+ - this work for additional information regarding copyright ownership.
+ - The ASF licenses this file to You under the Apache License, Version 2.0
+ - (the "License"); you may not use this file except in compliance with
+ - the License.  You may obtain a copy of the License at
+ -
+ -   http://www.apache.org/licenses/LICENSE-2.0
+ -
+ - Unless required by applicable law or agreed to in writing, software
+ - distributed under the License is distributed on an "AS IS" BASIS,
+ - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ - See the License for the specific language governing permissions and
+ - limitations under the License.
+ -->
+
+# Branching and Tagging
+
+## Overview
+
+Iceberg table metadata maintains a log of snapshots which represent the 
changes applied to a table. Snapshots are fundamental in Iceberg as they are 
the basis for reader isolation and time travel queries. For controlling 
metadata size and storage costs, Iceberg provides snapshot lifecycle management 
procedures such as [`expireSnapshots`](../../../javadoc/{{% icebergVersion 
%}}/org/apache/iceberg/Table.html#expireSnapshots--) for removing unused 
snapshots and no longer neccessary data files based on table snapshot retention 
properties.
+
+**For more sophisticated snapshot lifecycle management, Iceberg also supports 
branches and tags which are named references to snapshots.** Branches are 
independent lineages of snapshots and point to the head of the lineage. 
Branches and tags have independent lifecycles with regards to snapshot 
retention during expireSnapshots.
+
+Branching and tagging can be used for handling GDPR requirements and retaining 
important historical snapshots for auditing. Branches can also be used as part 
of data engineering workflows, for enabling experimental branches for testing 
and validating new jobs. New jobs can write to the branch without impacting the 
main table state. After validation of the new job, the main branch can be fast 
forwarded. 
+
+Branches and tags have a maximum reference age property 
+which control when the reference to the snapshot itself should be expired. 
Branches have retention properties which define the minimum number of snapshots 
to retain on a branch as well as the maximum age of individual snapshots to 
retain on the branch. These properties are used when the expireSnapshots 
procedure is run. For details on the algorithm for expireSnapshots, refer to 
the [spec](../../../spec/#snapshot-retention-policy).
+
+## Usage 
+
+### Iceberg Java Library
+
+The Iceberg Java library can be used for performing operations on branches and 
tags.
+
+#### Creating branches and tags
+
+New branches and tags can be created via the Java library's ManageSnapshots 
API. 
+
+```java
+import org.apache.iceberg.Table;
+
+Table table = catalog.loadTable(...);
+
+/* Create a branch test-branch which is retained for 1 week, and the latest 2 
snapshots on test-branch will always be retained. 
+Snapshots on test-branch which are created within the last hour will also be 
retained. */
+
+String branch = "test-branch";
+table.manageSnapshots()
+    .createBranch(branch, 3)
+    .setMinSnapshotsToKeep(branch, 2)
+    .setMaxSnapshotAgeMs(branch, 3600000)
+    .setMaxRefAgeMs(branch, 604800000)
+    .commit();
+
+// Create a tag historical-tag at snapshot 10 which is retained for a day
+String tag = "historical-tag"
+table.manageSnapshots()
+    .createTag(tag, 10)
+    .setMaxRefAgeMs(tag, 86400000)
+    .commit();
+```
+
+#### Writing to branches
+
+Writing to a branch can be performed by specifying `toBranch` in the 
operation. For the full list refer to 
[UpdateOperations](../../java/api/#update-operations). 
+```java
+
+// Append FILE_A to branch test-branch 
+String branch = "test-branch";
+
+table.newAppend()
+    .appendFile(FILE_A)
+    .toBranch(branch)
+    .commit();
+
+
+// Perform row level updates on "test-branch"
+table.newRowDelta()
+    .addRows(DATA_FILE)
+    .addDeletes(DELETES)
+    .toBranch(branch)
+    .commit();
+
+
+// Perform a rewrite operation replacing small_file_1 and small_file_2 on 
"test-branch" with compacted_file.
+table.newRewrite()
+    .rewriteFiles(ImmutableSet.of(small_file_1,small_file_2), 
ImmutableSet.of(compacted_file))
+    .toBranch(branch)
+    .commit();
+
+```
+
+#### Reading from branches and tags
+Reading from a branch or tag can be done as usual via the Table Scan API, by 
passing in a branch or tag in the useRef API. When a branch is passed in, the 
snapshot that's used is the head of the branch. Note that currently reading 
from a branch and specifying an asOfSnapshotId in the scan is not supported. 
+
+```java
+// Read from the head snapshot of test-branch
+TableScan branchRead = table.newScan().useRef("test-branch");
+
+// Read from the snapshot referenced by audit-tag
+Table tagRead = table.newScan().useRef("audit-tag");
+```
+
+#### Replacing and fast forwarding branches and tags
+
+The snapshots which existing branches and tags point to can be updated via the 
`replace` APIs. The fast forward operation is similar to git fast-forwarding. 
Fast forward can be used to advance a target branch to the head of a source 
branch or a tag when the target branch is an ancestor of the source. For both 
fast forward and replace, retention properties of the target branch are 
maintained by default.
+
+```java
+import org.apache.iceberg.Table;
+
+Table table = catalog.loadTable(...)
+String branch = "test-branch";
+
+// Update "test-branch" to point to snapshot 4
+table.manageSnapshots()
+     .replaceBranch(branch, 4)
+     .commit()
+
+table.manageSnapshots()
+     .replaceBranch(branch, 4)
+     .commit()
+
+String tag = "audit-tag";
+// Replace "audit-tag" to point to snapshot 3 and update its retention
+table.manageSnapshots()
+     .replaceBranch(tag, 4)
+     .setMaxRefAgeMs(1000)
+     .commit()
+
+
+```
+
+#### Updating retention properties
+
+Retention properties for branches and tags can be updated as well.
+Use the setMaxRefAgeMs for updating the retention property of the branch or 
tag itself. Branch snapshot retention properties can be updated via the 
setMinSnapshotsToKeep and setMaxSnapshotAgeMs APIs. 
+
+```java
+import org.apache.iceberg.Table;
+
+Table table = catalog.loadTable(...)
+
+String branch = "test-branch";
+// Update retention properties for test-branch
+table.manageSnapshots()
+    .setMinSnapshotsToKeep(branch, 10)
+    .setMaxSnapshotAgeMs(branch, 7200000)
+    .setMaxRefAgeMs(branch, 604800000)
+    .commit();
+
+// Update retention properties for test-tag
+table.manageSnapshots()
+    .setMaxRefAgeMs("test-tag", 604800000)
+    .commit();
+```
+
+#### Removing branches and tags
+
+Branches and tags can be removed via the removeBranch API
+
+```java
+import org.apache.iceberg.Table;
+
+Table table = catalog.loadTable(...)
+
+// Remove test-branch
+table.manageSnapshots()
+     .removeBranch("test-branch")
+     .commit()
+
+// Remove test-tag
+table.manageSnapshots()
+     .removeTag("test-tag")
+     .commit()
+```
+
+### Spark Integration

Review Comment:
   I think we should try to have different pages reference each other as much 
as possible. That means, in this page we can focus more on describing use cases 
and core concepts, and less about engine integration. And we can add more 
information in Spark, Flink pages for code usages such that this page links to 
them and those pages link back to this page. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org
For additional commands, e-mail: issues-h...@iceberg.apache.org

Reply via email to