|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Altinity Inc and/or its affiliates. All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + */ |
| 10 | +package com.altinity.ice.cli.internal.cmd; |
| 11 | + |
| 12 | +import com.fasterxml.jackson.annotation.JsonInclude; |
| 13 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 14 | +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; |
| 15 | +import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator; |
| 16 | +import java.io.IOException; |
| 17 | +import java.util.List; |
| 18 | +import java.util.stream.Collectors; |
| 19 | +import org.apache.iceberg.catalog.Namespace; |
| 20 | +import org.apache.iceberg.catalog.TableIdentifier; |
| 21 | +import org.apache.iceberg.rest.RESTCatalog; |
| 22 | + |
| 23 | +public final class ListTables { |
| 24 | + |
| 25 | + private ListTables() {} |
| 26 | + |
| 27 | + public static void run(RESTCatalog catalog, Namespace namespace, boolean json) |
| 28 | + throws IOException { |
| 29 | + List<TableIdentifier> tables = catalog.listTables(namespace); |
| 30 | + List<String> names = |
| 31 | + tables.stream().map(TableIdentifier::toString).sorted().collect(Collectors.toList()); |
| 32 | + var result = new Result(namespace.toString(), names); |
| 33 | + output(result, json); |
| 34 | + } |
| 35 | + |
| 36 | + private static void output(Result result, boolean json) throws IOException { |
| 37 | + ObjectMapper mapper = |
| 38 | + json |
| 39 | + ? new ObjectMapper() |
| 40 | + : new ObjectMapper(new YAMLFactory().enable(YAMLGenerator.Feature.MINIMIZE_QUOTES)); |
| 41 | + mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); |
| 42 | + System.out.println(mapper.writeValueAsString(result)); |
| 43 | + } |
| 44 | + |
| 45 | + @JsonInclude(JsonInclude.Include.NON_NULL) |
| 46 | + record Result(String namespace, List<String> tables) {} |
| 47 | +} |
0 commit comments