Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions ice/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@ ice files flowers.iris
# list partitions
ice list-partitions nyc.taxis_p_by_day

# list current and previous snapshots
ice list-snapshots flowers.iris

# only the latest 5
ice list-snapshots flowers.iris --limit 5

# describe a parquet file directly
ice describe-parquet file://iris.parquet
```
Expand Down
25 changes: 25 additions & 0 deletions ice/src/main/java/com/altinity/ice/cli/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.altinity.ice.cli.internal.cmd.InsertWatch;
import com.altinity.ice.cli.internal.cmd.ListNamespaces;
import com.altinity.ice.cli.internal.cmd.ListPartitions;
import com.altinity.ice.cli.internal.cmd.ListSnapshots;
import com.altinity.ice.cli.internal.cmd.ListTables;
import com.altinity.ice.cli.internal.cmd.Scan;
import com.altinity.ice.cli.internal.config.Config;
Expand Down Expand Up @@ -733,6 +734,30 @@ void listPartitions(
}
}

@CommandLine.Command(
name = "list-snapshots",
description = "List current and previous snapshots of a table.")
void listSnapshots(
@CommandLine.Parameters(
arity = "1",
paramLabel = "<name>",
description = "Table name (e.g. ns1.table1)")
String name,
@CommandLine.Option(
names = {"--limit"},
description = "Show only the most recent N snapshots (0 = all)",
defaultValue = "0")
int limit,
@CommandLine.Option(
names = {"--json"},
description = "Output JSON instead of YAML")
boolean json)
throws IOException {
try (RESTCatalog catalog = loadCatalog()) {
ListSnapshots.run(catalog, TableIdentifier.parse(name), json, limit);
}
}

@CommandLine.Command(name = "delete-table", description = "Delete table.")
void deleteTable(
@CommandLine.Parameters(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright (c) 2025 Altinity Inc and/or its affiliates. All rights reserved.
*
* Licensed 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
*/
package com.altinity.ice.cli.internal.cmd;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;
import java.io.IOException;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import org.apache.iceberg.Snapshot;
import org.apache.iceberg.Table;
import org.apache.iceberg.catalog.TableIdentifier;
import org.apache.iceberg.rest.RESTCatalog;

public final class ListSnapshots {

private ListSnapshots() {}

public static void run(RESTCatalog catalog, TableIdentifier tableId, boolean json, int limit)
throws IOException {
Table table = catalog.loadTable(tableId);
Long currentSnapshotId =
table.currentSnapshot() != null ? table.currentSnapshot().snapshotId() : null;

List<SnapshotInfo> rows = new ArrayList<>();
for (Snapshot snapshot : table.snapshots()) {
Long parentId = snapshot.parentId();
rows.add(
new SnapshotInfo(
snapshot.snapshotId(),
parentId,
snapshot.sequenceNumber(),
snapshot.timestampMillis(),
Instant.ofEpochMilli(snapshot.timestampMillis()).toString(),
snapshot.operation(),
currentSnapshotId != null && snapshot.snapshotId() == currentSnapshotId,
snapshot.summary(),
snapshot.manifestListLocation()));
}

rows.sort(Comparator.comparingLong(SnapshotInfo::timestampMillis));

if (limit > 0 && rows.size() > limit) {
rows = new ArrayList<>(rows.subList(rows.size() - limit, rows.size()));
}

var result = new Result(tableId.toString(), currentSnapshotId, rows);
output(result, json);
}

private static void output(Result result, boolean json) throws IOException {
ObjectMapper mapper =
json
? new ObjectMapper()
: new ObjectMapper(new YAMLFactory().enable(YAMLGenerator.Feature.MINIMIZE_QUOTES));
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
System.out.println(mapper.writeValueAsString(result));
}

@JsonInclude(JsonInclude.Include.NON_NULL)
record Result(String table, Long currentSnapshotId, List<SnapshotInfo> snapshots) {}

@JsonInclude(JsonInclude.Include.NON_NULL)
record SnapshotInfo(
long snapshotId,
Long parentId,
long sequenceNumber,
long timestampMillis,
String timestamp,
String operation,
boolean current,
Map<String, String> summary,
String manifestListLocation) {}
}
Loading