|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.seatunnel.connectors.seatunnel.file.catalog; |
| 19 | + |
| 20 | +import org.apache.seatunnel.api.table.catalog.Catalog; |
| 21 | +import org.apache.seatunnel.api.table.catalog.CatalogTable; |
| 22 | +import org.apache.seatunnel.api.table.catalog.TablePath; |
| 23 | +import org.apache.seatunnel.api.table.catalog.exception.CatalogException; |
| 24 | +import org.apache.seatunnel.api.table.catalog.exception.DatabaseAlreadyExistException; |
| 25 | +import org.apache.seatunnel.api.table.catalog.exception.DatabaseNotExistException; |
| 26 | +import org.apache.seatunnel.api.table.catalog.exception.TableAlreadyExistException; |
| 27 | +import org.apache.seatunnel.api.table.catalog.exception.TableNotExistException; |
| 28 | +import org.apache.seatunnel.connectors.seatunnel.file.hadoop.HadoopFileSystemProxy; |
| 29 | + |
| 30 | +import org.apache.commons.collections4.CollectionUtils; |
| 31 | +import org.apache.hadoop.fs.LocatedFileStatus; |
| 32 | + |
| 33 | +import lombok.SneakyThrows; |
| 34 | + |
| 35 | +import java.util.List; |
| 36 | + |
| 37 | +public abstract class AbstractFileCatalog implements Catalog { |
| 38 | + |
| 39 | + protected final String catalogName; |
| 40 | + private final HadoopFileSystemProxy hadoopFileSystemProxy; |
| 41 | + private final String filePath; |
| 42 | + |
| 43 | + protected AbstractFileCatalog( |
| 44 | + HadoopFileSystemProxy hadoopFileSystemProxy, String filePath, String catalogName) { |
| 45 | + this.catalogName = catalogName; |
| 46 | + this.filePath = filePath; |
| 47 | + this.hadoopFileSystemProxy = hadoopFileSystemProxy; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public void open() throws CatalogException {} |
| 52 | + |
| 53 | + @Override |
| 54 | + public void close() throws CatalogException {} |
| 55 | + |
| 56 | + @Override |
| 57 | + public String name() { |
| 58 | + return catalogName; |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public String getDefaultDatabase() throws CatalogException { |
| 63 | + return null; |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public boolean databaseExists(String databaseName) throws CatalogException { |
| 68 | + return false; |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public List<String> listDatabases() throws CatalogException { |
| 73 | + return null; |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public List<String> listTables(String databaseName) |
| 78 | + throws CatalogException, DatabaseNotExistException { |
| 79 | + return null; |
| 80 | + } |
| 81 | + |
| 82 | + @SneakyThrows |
| 83 | + @Override |
| 84 | + public boolean tableExists(TablePath tablePath) throws CatalogException { |
| 85 | + return hadoopFileSystemProxy.fileExist(filePath); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public CatalogTable getTable(TablePath tablePath) |
| 90 | + throws CatalogException, TableNotExistException { |
| 91 | + return null; |
| 92 | + } |
| 93 | + |
| 94 | + @SneakyThrows |
| 95 | + @Override |
| 96 | + public void createTable(TablePath tablePath, CatalogTable table, boolean ignoreIfExists) |
| 97 | + throws TableAlreadyExistException, DatabaseNotExistException, CatalogException { |
| 98 | + hadoopFileSystemProxy.createDir(filePath); |
| 99 | + } |
| 100 | + |
| 101 | + @SneakyThrows |
| 102 | + @Override |
| 103 | + public void dropTable(TablePath tablePath, boolean ignoreIfNotExists) |
| 104 | + throws TableNotExistException, CatalogException { |
| 105 | + hadoopFileSystemProxy.deleteFile(filePath); |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public void createDatabase(TablePath tablePath, boolean ignoreIfExists) |
| 110 | + throws DatabaseAlreadyExistException, CatalogException {} |
| 111 | + |
| 112 | + @Override |
| 113 | + public void dropDatabase(TablePath tablePath, boolean ignoreIfNotExists) |
| 114 | + throws DatabaseNotExistException, CatalogException {} |
| 115 | + |
| 116 | + @SneakyThrows |
| 117 | + @Override |
| 118 | + public void truncateTable(TablePath tablePath, boolean ignoreIfNotExists) |
| 119 | + throws TableNotExistException, CatalogException { |
| 120 | + hadoopFileSystemProxy.deleteFile(filePath); |
| 121 | + hadoopFileSystemProxy.createDir(filePath); |
| 122 | + } |
| 123 | + |
| 124 | + @SneakyThrows |
| 125 | + @Override |
| 126 | + public boolean isExistsData(TablePath tablePath) { |
| 127 | + final List<LocatedFileStatus> locatedFileStatuses = |
| 128 | + hadoopFileSystemProxy.listFile(filePath); |
| 129 | + return CollectionUtils.isNotEmpty(locatedFileStatuses); |
| 130 | + } |
| 131 | +} |
0 commit comments