|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package org.apache.beam.sdk.extensions.sql.meta.provider.iceberg; |
| 19 | + |
| 20 | +import static org.apache.beam.sdk.util.Preconditions.checkStateNotNull; |
| 21 | +import static org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Preconditions.checkArgument; |
| 22 | + |
| 23 | +import java.util.HashMap; |
| 24 | +import java.util.Map; |
| 25 | +import org.apache.beam.sdk.extensions.sql.TableUtils; |
| 26 | +import org.apache.beam.sdk.extensions.sql.impl.TableName; |
| 27 | +import org.apache.beam.sdk.extensions.sql.meta.BeamSqlTable; |
| 28 | +import org.apache.beam.sdk.extensions.sql.meta.Table; |
| 29 | +import org.apache.beam.sdk.extensions.sql.meta.provider.TableProvider; |
| 30 | +import org.apache.beam.sdk.extensions.sql.meta.store.InMemoryMetaStore; |
| 31 | +import org.apache.beam.sdk.io.iceberg.IcebergCatalogConfig; |
| 32 | +import org.apache.beam.sdk.io.iceberg.IcebergCatalogConfig.IcebergTableInfo; |
| 33 | +import org.apache.beam.sdk.io.iceberg.TableAlreadyExistsException; |
| 34 | +import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.annotations.VisibleForTesting; |
| 35 | +import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.ImmutableMap; |
| 36 | +import org.checkerframework.checker.nullness.qual.Nullable; |
| 37 | +import org.slf4j.Logger; |
| 38 | +import org.slf4j.LoggerFactory; |
| 39 | + |
| 40 | +public class IcebergMetastore extends InMemoryMetaStore { |
| 41 | + private static final Logger LOG = LoggerFactory.getLogger(IcebergMetastore.class); |
| 42 | + @VisibleForTesting final IcebergCatalogConfig catalogConfig; |
| 43 | + private final Map<String, Table> cachedTables = new HashMap<>(); |
| 44 | + private final String database; |
| 45 | + |
| 46 | + public IcebergMetastore(String db, IcebergCatalogConfig catalogConfig) { |
| 47 | + this.database = db; |
| 48 | + this.catalogConfig = catalogConfig; |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public String getTableType() { |
| 53 | + return "iceberg"; |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public void createTable(Table table) { |
| 58 | + if (!table.getType().equals("iceberg")) { |
| 59 | + getProvider(table.getType()).createTable(table); |
| 60 | + } else { |
| 61 | + String identifier = getIdentifier(table); |
| 62 | + try { |
| 63 | + catalogConfig.createTable(identifier, table.getSchema(), table.getPartitionFields()); |
| 64 | + } catch (TableAlreadyExistsException e) { |
| 65 | + LOG.info( |
| 66 | + "Iceberg table '{}' already exists at location '{}'.", table.getName(), identifier); |
| 67 | + } |
| 68 | + } |
| 69 | + cachedTables.put(table.getName(), table); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public void dropTable(String tableName) { |
| 74 | + String identifier = getIdentifier(tableName); |
| 75 | + if (catalogConfig.dropTable(identifier)) { |
| 76 | + LOG.info("Dropped table '{}' (path: '{}').", tableName, identifier); |
| 77 | + } else { |
| 78 | + LOG.info( |
| 79 | + "Ignoring DROP TABLE call for '{}' (path: '{}') because it does not exist.", |
| 80 | + tableName, |
| 81 | + identifier); |
| 82 | + } |
| 83 | + cachedTables.remove(tableName); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public Map<String, Table> getTables() { |
| 88 | + for (String id : catalogConfig.listTables(database)) { |
| 89 | + String name = TableName.create(id).getTableName(); |
| 90 | + @Nullable Table cachedTable = cachedTables.get(name); |
| 91 | + if (cachedTable == null) { |
| 92 | + Table table = checkStateNotNull(loadTable(id)); |
| 93 | + cachedTables.put(name, table); |
| 94 | + } |
| 95 | + } |
| 96 | + return ImmutableMap.copyOf(cachedTables); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public @Nullable Table getTable(String name) { |
| 101 | + if (cachedTables.containsKey(name)) { |
| 102 | + return cachedTables.get(name); |
| 103 | + } |
| 104 | + @Nullable Table table = loadTable(getIdentifier(name)); |
| 105 | + if (table != null) { |
| 106 | + cachedTables.put(name, table); |
| 107 | + } |
| 108 | + return table; |
| 109 | + } |
| 110 | + |
| 111 | + private String getIdentifier(String name) { |
| 112 | + return database + "." + name; |
| 113 | + } |
| 114 | + |
| 115 | + private String getIdentifier(Table table) { |
| 116 | + checkArgument( |
| 117 | + table.getLocation() == null, "Cannot create Iceberg tables using LOCATION property."); |
| 118 | + return getIdentifier(table.getName()); |
| 119 | + } |
| 120 | + |
| 121 | + private @Nullable Table loadTable(String identifier) { |
| 122 | + @Nullable IcebergTableInfo tableInfo = catalogConfig.loadTable(identifier); |
| 123 | + if (tableInfo == null) { |
| 124 | + return null; |
| 125 | + } |
| 126 | + return Table.builder() |
| 127 | + .type(getTableType()) |
| 128 | + .name(identifier) |
| 129 | + .schema(tableInfo.getSchema()) |
| 130 | + .properties(TableUtils.parseProperties(tableInfo.getProperties())) |
| 131 | + .build(); |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public BeamSqlTable buildBeamSqlTable(Table table) { |
| 136 | + if (table.getType().equals("iceberg")) { |
| 137 | + return new IcebergTable(getIdentifier(table), table, catalogConfig); |
| 138 | + } |
| 139 | + return getProvider(table.getType()).buildBeamSqlTable(table); |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public boolean supportsPartitioning(Table table) { |
| 144 | + if (table.getType().equals("iceberg")) { |
| 145 | + return true; |
| 146 | + } |
| 147 | + return getProvider(table.getType()).supportsPartitioning(table); |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public void registerProvider(TableProvider provider) { |
| 152 | + super.registerProvider(provider); |
| 153 | + } |
| 154 | +} |
0 commit comments