|
| 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.kyuubi.engine.flink.operation |
| 19 | + |
| 20 | +import scala.collection.JavaConverters._ |
| 21 | + |
| 22 | +import org.apache.commons.lang3.StringUtils |
| 23 | +import org.apache.flink.table.api.{DataTypes, ResultKind, TableEnvironment} |
| 24 | +import org.apache.flink.table.catalog.Column |
| 25 | +import org.apache.flink.types.Row |
| 26 | + |
| 27 | +import org.apache.kyuubi.engine.flink.result.ResultSet |
| 28 | +import org.apache.kyuubi.engine.flink.util.StringUtils.filterPattern |
| 29 | +import org.apache.kyuubi.operation.OperationType |
| 30 | +import org.apache.kyuubi.operation.meta.ResultSetSchemaConstant._ |
| 31 | +import org.apache.kyuubi.session.Session |
| 32 | + |
| 33 | +class GetSchemas(session: Session, catalogName: String, schema: String) |
| 34 | + extends FlinkOperation(OperationType.GET_SCHEMAS, session) { |
| 35 | + |
| 36 | + override protected def runInternal(): Unit = { |
| 37 | + try { |
| 38 | + val schemaPattern = toJavaRegex(schema) |
| 39 | + val tableEnv: TableEnvironment = sessionContext.getExecutionContext.getTableEnvironment |
| 40 | + val schemas = tableEnv.listCatalogs() |
| 41 | + .filter { c => StringUtils.isEmpty(catalogName) || c == catalogName } |
| 42 | + .flatMap { c => |
| 43 | + val catalog = tableEnv.getCatalog(c).get() |
| 44 | + filterPattern(catalog.listDatabases().asScala, schemaPattern) |
| 45 | + .map { d => Row.of(d, c) } |
| 46 | + } |
| 47 | + resultSet = ResultSet.builder.resultKind(ResultKind.SUCCESS_WITH_CONTENT) |
| 48 | + .columns( |
| 49 | + Column.physical(TABLE_SCHEM, DataTypes.STRING()), |
| 50 | + Column.physical(TABLE_CATALOG, DataTypes.STRING())) |
| 51 | + .data(schemas) |
| 52 | + .build |
| 53 | + } catch { |
| 54 | + onError() |
| 55 | + } |
| 56 | + } |
| 57 | +} |
0 commit comments