|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Alibaba Group Holding Ltd. |
| 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 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.alibaba.fluss.connector.spark.catalog; |
| 18 | + |
| 19 | +import com.alibaba.fluss.client.Connection; |
| 20 | +import com.alibaba.fluss.client.ConnectionFactory; |
| 21 | +import com.alibaba.fluss.client.admin.Admin; |
| 22 | +import com.alibaba.fluss.config.ConfigOptions; |
| 23 | +import com.alibaba.fluss.config.Configuration; |
| 24 | +import com.alibaba.fluss.connector.spark.SparkConnectorOptions; |
| 25 | +import com.alibaba.fluss.connector.spark.exception.CatalogException; |
| 26 | +import com.alibaba.fluss.connector.spark.table.SparkTable; |
| 27 | +import com.alibaba.fluss.connector.spark.utils.SparkConversions; |
| 28 | +import com.alibaba.fluss.metadata.TableDescriptor; |
| 29 | +import com.alibaba.fluss.metadata.TableInfo; |
| 30 | +import com.alibaba.fluss.metadata.TablePath; |
| 31 | +import com.alibaba.fluss.utils.CatalogExceptionUtils; |
| 32 | +import com.alibaba.fluss.utils.ExceptionUtils; |
| 33 | +import com.alibaba.fluss.utils.IOUtils; |
| 34 | +import com.alibaba.fluss.utils.Preconditions; |
| 35 | + |
| 36 | +import org.apache.spark.sql.catalyst.analysis.NamespaceAlreadyExistsException; |
| 37 | +import org.apache.spark.sql.catalyst.analysis.NoSuchFunctionException; |
| 38 | +import org.apache.spark.sql.catalyst.analysis.NoSuchNamespaceException; |
| 39 | +import org.apache.spark.sql.catalyst.analysis.NoSuchTableException; |
| 40 | +import org.apache.spark.sql.catalyst.analysis.NonEmptyNamespaceException; |
| 41 | +import org.apache.spark.sql.catalyst.analysis.TableAlreadyExistsException; |
| 42 | +import org.apache.spark.sql.connector.catalog.FunctionCatalog; |
| 43 | +import org.apache.spark.sql.connector.catalog.Identifier; |
| 44 | +import org.apache.spark.sql.connector.catalog.NamespaceChange; |
| 45 | +import org.apache.spark.sql.connector.catalog.StagedTable; |
| 46 | +import org.apache.spark.sql.connector.catalog.StagingTableCatalog; |
| 47 | +import org.apache.spark.sql.connector.catalog.SupportsNamespaces; |
| 48 | +import org.apache.spark.sql.connector.catalog.Table; |
| 49 | +import org.apache.spark.sql.connector.catalog.TableCatalog; |
| 50 | +import org.apache.spark.sql.connector.catalog.TableChange; |
| 51 | +import org.apache.spark.sql.connector.catalog.functions.UnboundFunction; |
| 52 | +import org.apache.spark.sql.connector.expressions.Transform; |
| 53 | +import org.apache.spark.sql.types.StructType; |
| 54 | +import org.apache.spark.sql.util.CaseInsensitiveStringMap; |
| 55 | + |
| 56 | +import java.io.Closeable; |
| 57 | +import java.util.Arrays; |
| 58 | +import java.util.Collections; |
| 59 | +import java.util.List; |
| 60 | +import java.util.Map; |
| 61 | + |
| 62 | +/** A Spark Catalog for Fluss. */ |
| 63 | +public class SparkCatalog |
| 64 | + implements StagingTableCatalog, |
| 65 | + SupportsNamespaces, |
| 66 | + FunctionCatalog, |
| 67 | + TableCatalog, |
| 68 | + Closeable { |
| 69 | + |
| 70 | + private static final String[] DEFAULT_NAMESPACE = new String[] {"fluss"}; |
| 71 | + |
| 72 | + private String bootstrapServers; |
| 73 | + private String catalogName; |
| 74 | + private Connection connection; |
| 75 | + private Admin admin; |
| 76 | + |
| 77 | + @Override |
| 78 | + public void initialize(String name, CaseInsensitiveStringMap options) { |
| 79 | + this.catalogName = name; |
| 80 | + this.bootstrapServers = options.get(SparkConnectorOptions.BOOTSTRAP_SERVERS.key()); |
| 81 | + Configuration flussConfigs = new Configuration(); |
| 82 | + flussConfigs.setString(ConfigOptions.BOOTSTRAP_SERVERS.key(), bootstrapServers); |
| 83 | + connection = ConnectionFactory.createConnection(flussConfigs); |
| 84 | + admin = connection.getAdmin(); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public String[] defaultNamespace() { |
| 89 | + return DEFAULT_NAMESPACE; |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public String name() { |
| 94 | + return this.catalogName; |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public boolean namespaceExists(String[] namespace) { |
| 99 | + isValidateNamespace(namespace); |
| 100 | + try { |
| 101 | + return admin.databaseExists(namespace[0]).get(); |
| 102 | + } catch (Exception e) { |
| 103 | + throw new CatalogException( |
| 104 | + String.format("Failed to check if database %s exists in %s", namespace, name()), |
| 105 | + e); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public String[][] listNamespaces() { |
| 111 | + try { |
| 112 | + List<String> databases = admin.listDatabases().get(); |
| 113 | + String[][] namespaces = new String[databases.size()][]; |
| 114 | + |
| 115 | + for (int i = 0; i < databases.size(); ++i) { |
| 116 | + namespaces[i] = new String[] {databases.get(i)}; |
| 117 | + } |
| 118 | + |
| 119 | + return namespaces; |
| 120 | + } catch (Exception e) { |
| 121 | + throw new CatalogException( |
| 122 | + String.format("Failed to list all databases in %s", name()), e); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public String[][] listNamespaces(String[] namespace) throws NoSuchNamespaceException { |
| 128 | + if (namespace.length == 0) { |
| 129 | + return listNamespaces(); |
| 130 | + } else { |
| 131 | + isValidateNamespace(namespace); |
| 132 | + if (namespaceExists(namespace)) { |
| 133 | + return new String[0][]; |
| 134 | + } |
| 135 | + throw new NoSuchNamespaceException(namespace); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public Map<String, String> loadNamespaceMetadata(String[] namespace) |
| 141 | + throws NoSuchNamespaceException { |
| 142 | + isValidateNamespace(namespace); |
| 143 | + if (namespaceExists(namespace)) { |
| 144 | + return Collections.emptyMap(); |
| 145 | + } |
| 146 | + throw new NoSuchNamespaceException(namespace); |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + public void createNamespace(String[] namespace, Map<String, String> metadata) |
| 151 | + throws NamespaceAlreadyExistsException { |
| 152 | + isValidateNamespace(namespace); |
| 153 | + try { |
| 154 | + admin.createDatabase(namespace[0], false).get(); |
| 155 | + } catch (Exception e) { |
| 156 | + Throwable t = ExceptionUtils.stripExecutionException(e); |
| 157 | + if (CatalogExceptionUtils.isDatabaseAlreadyExist(t)) { |
| 158 | + throw new NamespaceAlreadyExistsException(namespace); |
| 159 | + } else { |
| 160 | + throw new CatalogException( |
| 161 | + String.format("Failed to create database %s in %s", namespace, name()), t); |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public void alterNamespace(String[] namespace, NamespaceChange... changes) |
| 168 | + throws NoSuchNamespaceException { |
| 169 | + throw new UnsupportedOperationException(); |
| 170 | + } |
| 171 | + |
| 172 | + @Override |
| 173 | + public boolean dropNamespace(String[] namespace, boolean cascade) |
| 174 | + throws NoSuchNamespaceException, NonEmptyNamespaceException { |
| 175 | + isValidateNamespace(namespace); |
| 176 | + try { |
| 177 | + admin.deleteDatabase(namespace[0], false, cascade).get(); |
| 178 | + return true; |
| 179 | + } catch (Exception e) { |
| 180 | + Throwable t = ExceptionUtils.stripExecutionException(e); |
| 181 | + if (CatalogExceptionUtils.isDatabaseNotExist(t)) { |
| 182 | + throw new NoSuchNamespaceException(namespace); |
| 183 | + } else if (CatalogExceptionUtils.isDatabaseNotEmpty(t)) { |
| 184 | + throw new NonEmptyNamespaceException(namespace); |
| 185 | + } else { |
| 186 | + throw new CatalogException( |
| 187 | + String.format("Failed to drop database %s in %s", namespace, name()), t); |
| 188 | + } |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + @Override |
| 193 | + public StagedTable stageCreate( |
| 194 | + Identifier ident, |
| 195 | + StructType schema, |
| 196 | + Transform[] partitions, |
| 197 | + Map<String, String> properties) |
| 198 | + throws TableAlreadyExistsException, NoSuchNamespaceException { |
| 199 | + throw new UnsupportedOperationException(); |
| 200 | + } |
| 201 | + |
| 202 | + @Override |
| 203 | + public StagedTable stageReplace( |
| 204 | + Identifier ident, |
| 205 | + StructType schema, |
| 206 | + Transform[] partitions, |
| 207 | + Map<String, String> properties) |
| 208 | + throws NoSuchNamespaceException, NoSuchTableException { |
| 209 | + throw new UnsupportedOperationException(); |
| 210 | + } |
| 211 | + |
| 212 | + @Override |
| 213 | + public StagedTable stageCreateOrReplace( |
| 214 | + Identifier ident, |
| 215 | + StructType schema, |
| 216 | + Transform[] partitions, |
| 217 | + Map<String, String> properties) |
| 218 | + throws NoSuchNamespaceException { |
| 219 | + throw new UnsupportedOperationException(); |
| 220 | + } |
| 221 | + |
| 222 | + @Override |
| 223 | + public Table loadTable(Identifier ident, String version) throws NoSuchTableException { |
| 224 | + throw new UnsupportedOperationException(); |
| 225 | + } |
| 226 | + |
| 227 | + @Override |
| 228 | + public Table loadTable(Identifier ident, long timestamp) throws NoSuchTableException { |
| 229 | + throw new UnsupportedOperationException(); |
| 230 | + } |
| 231 | + |
| 232 | + @Override |
| 233 | + public void invalidateTable(Identifier ident) { |
| 234 | + throw new UnsupportedOperationException(); |
| 235 | + } |
| 236 | + |
| 237 | + @Override |
| 238 | + public boolean tableExists(Identifier ident) { |
| 239 | + try { |
| 240 | + return admin.tableExists(toTablePath(ident)).get(); |
| 241 | + } catch (Exception e) { |
| 242 | + throw new CatalogException( |
| 243 | + String.format("Failed to check if table %s exists in %s", ident, name()), |
| 244 | + ExceptionUtils.stripExecutionException(e)); |
| 245 | + } |
| 246 | + } |
| 247 | + |
| 248 | + @Override |
| 249 | + public boolean purgeTable(Identifier ident) throws UnsupportedOperationException { |
| 250 | + throw new UnsupportedOperationException(); |
| 251 | + } |
| 252 | + |
| 253 | + @Override |
| 254 | + public Identifier[] listTables(String[] namespace) throws NoSuchNamespaceException { |
| 255 | + isValidateNamespace(namespace); |
| 256 | + try { |
| 257 | + List<String> tables = admin.listTables(namespace[0]).get(); |
| 258 | + Identifier[] identifiers = new Identifier[tables.size()]; |
| 259 | + for (int i = 0; i < tables.size(); i++) { |
| 260 | + identifiers[i] = Identifier.of(namespace, tables.get(i)); |
| 261 | + } |
| 262 | + return identifiers; |
| 263 | + } catch (Exception e) { |
| 264 | + Throwable t = ExceptionUtils.stripExecutionException(e); |
| 265 | + if (CatalogExceptionUtils.isDatabaseNotExist(t)) { |
| 266 | + throw new NoSuchNamespaceException(namespace); |
| 267 | + } |
| 268 | + throw new CatalogException( |
| 269 | + String.format( |
| 270 | + "Failed to list all tables in database %s in %s", namespace, name()), |
| 271 | + t); |
| 272 | + } |
| 273 | + } |
| 274 | + |
| 275 | + @Override |
| 276 | + public Table loadTable(Identifier ident) throws NoSuchTableException { |
| 277 | + try { |
| 278 | + TableInfo tableInfo = admin.getTable(toTablePath(ident)).get(); |
| 279 | + return new SparkTable(tableInfo.getTablePath(), tableInfo.getTableDescriptor()); |
| 280 | + } catch (Exception e) { |
| 281 | + Throwable t = ExceptionUtils.stripExecutionException(e); |
| 282 | + if (CatalogExceptionUtils.isTableNotExist(t)) { |
| 283 | + throw new NoSuchTableException(ident); |
| 284 | + } else { |
| 285 | + throw new CatalogException( |
| 286 | + String.format("Failed to get table %s in %s", ident, name()), t); |
| 287 | + } |
| 288 | + } |
| 289 | + } |
| 290 | + |
| 291 | + @Override |
| 292 | + public Table createTable( |
| 293 | + Identifier ident, |
| 294 | + StructType schema, |
| 295 | + Transform[] partitions, |
| 296 | + Map<String, String> properties) |
| 297 | + throws TableAlreadyExistsException, NoSuchNamespaceException { |
| 298 | + try { |
| 299 | + TableDescriptor tableDescriptor = |
| 300 | + SparkConversions.toFlussTable(schema, partitions, properties); |
| 301 | + TablePath tablePath = toTablePath(ident); |
| 302 | + admin.createTable(tablePath, tableDescriptor, false).get(); |
| 303 | + return new SparkTable(tablePath, tableDescriptor); |
| 304 | + } catch (Exception e) { |
| 305 | + Throwable t = ExceptionUtils.stripExecutionException(e); |
| 306 | + if (CatalogExceptionUtils.isDatabaseNotExist(t)) { |
| 307 | + throw new NoSuchNamespaceException(ident.namespace()); |
| 308 | + } else if (CatalogExceptionUtils.isTableAlreadyExist(t)) { |
| 309 | + throw new TableAlreadyExistsException(ident); |
| 310 | + } else { |
| 311 | + throw new CatalogException( |
| 312 | + String.format("Failed to create table %s in %s", ident, name()), t); |
| 313 | + } |
| 314 | + } |
| 315 | + } |
| 316 | + |
| 317 | + @Override |
| 318 | + public Table alterTable(Identifier ident, TableChange... changes) throws NoSuchTableException { |
| 319 | + throw new UnsupportedOperationException(); |
| 320 | + } |
| 321 | + |
| 322 | + @Override |
| 323 | + public boolean dropTable(Identifier ident) { |
| 324 | + try { |
| 325 | + admin.deleteTable(toTablePath(ident), false).get(); |
| 326 | + return true; |
| 327 | + } catch (Exception e) { |
| 328 | + Throwable t = ExceptionUtils.stripExecutionException(e); |
| 329 | + throw new CatalogException( |
| 330 | + String.format("Failed to drop table %s in %s", ident, name()), t); |
| 331 | + } |
| 332 | + } |
| 333 | + |
| 334 | + @Override |
| 335 | + public void renameTable(Identifier oldIdent, Identifier newIdent) |
| 336 | + throws NoSuchTableException, TableAlreadyExistsException { |
| 337 | + throw new UnsupportedOperationException(); |
| 338 | + } |
| 339 | + |
| 340 | + @Override |
| 341 | + public Identifier[] listFunctions(String[] namespace) throws NoSuchNamespaceException { |
| 342 | + throw new UnsupportedOperationException(); |
| 343 | + } |
| 344 | + |
| 345 | + @Override |
| 346 | + public UnboundFunction loadFunction(Identifier ident) throws NoSuchFunctionException { |
| 347 | + throw new UnsupportedOperationException(); |
| 348 | + } |
| 349 | + |
| 350 | + @Override |
| 351 | + public void close() { |
| 352 | + IOUtils.closeQuietly(admin, "fluss-admin"); |
| 353 | + IOUtils.closeQuietly(connection, "fluss-connection"); |
| 354 | + } |
| 355 | + |
| 356 | + private void isValidateNamespace(String[] namespace) { |
| 357 | + Preconditions.checkArgument( |
| 358 | + namespace.length == 1, "Namespace %s is not valid", Arrays.toString(namespace)); |
| 359 | + } |
| 360 | + |
| 361 | + private TablePath toTablePath(Identifier ident) { |
| 362 | + isValidateNamespace(ident.namespace()); |
| 363 | + return TablePath.of(ident.namespace()[0], ident.name()); |
| 364 | + } |
| 365 | +} |
0 commit comments