You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@seatunnel.apache.org by "Hisoka-X (via GitHub)" <gi...@apache.org> on 2023/09/14 03:26:59 UTC

[GitHub] [seatunnel] Hisoka-X commented on a diff in pull request #5175: [Feature][Catalog] Doris Catalog

Hisoka-X commented on code in PR #5175:
URL: https://github.com/apache/seatunnel/pull/5175#discussion_r1325266732


##########
seatunnel-connectors-v2/connector-doris/src/main/java/org/apache/seatunnel/connectors/doris/catalog/DorisCatalog.java:
##########
@@ -0,0 +1,330 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.seatunnel.connectors.doris.catalog;
+
+import org.apache.seatunnel.api.table.catalog.Catalog;
+import org.apache.seatunnel.api.table.catalog.CatalogTable;
+import org.apache.seatunnel.api.table.catalog.PhysicalColumn;
+import org.apache.seatunnel.api.table.catalog.PrimaryKey;
+import org.apache.seatunnel.api.table.catalog.TableIdentifier;
+import org.apache.seatunnel.api.table.catalog.TablePath;
+import org.apache.seatunnel.api.table.catalog.TableSchema;
+import org.apache.seatunnel.api.table.catalog.exception.CatalogException;
+import org.apache.seatunnel.api.table.catalog.exception.DatabaseAlreadyExistException;
+import org.apache.seatunnel.api.table.catalog.exception.DatabaseNotExistException;
+import org.apache.seatunnel.api.table.catalog.exception.TableAlreadyExistException;
+import org.apache.seatunnel.api.table.catalog.exception.TableNotExistException;
+import org.apache.seatunnel.connectors.doris.config.DorisConfig;
+import org.apache.seatunnel.connectors.doris.config.DorisOptions;
+import org.apache.seatunnel.connectors.doris.util.DorisCatalogUtil;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class DorisCatalog implements Catalog {
+
+    private static final Logger LOG = LoggerFactory.getLogger(DorisCatalog.class);
+
+    private final String catalogName;
+
+    private final String[] frontEndNodes;
+
+    private final Integer queryPort;
+
+    private final String username;
+
+    private final String password;
+
+    private String defaultDatabase = "information_schema";
+
+    private Connection conn;
+
+    private DorisConfig dorisConfig;
+
+    public DorisCatalog(
+            String catalogName,
+            String frontEndNodes,
+            Integer queryPort,
+            String username,
+            String password) {
+        this.catalogName = catalogName;
+        this.frontEndNodes = frontEndNodes.split(",");
+        this.queryPort = queryPort;
+        this.username = username;
+        this.password = password;
+    }
+
+    public DorisCatalog(
+            String catalogName,
+            String frontEndNodes,
+            Integer queryPort,
+            String username,
+            String password,
+            DorisConfig config) {
+        this(catalogName, frontEndNodes, queryPort, username, password);
+        this.dorisConfig = config;
+    }
+
+    public DorisCatalog(
+            String catalogName,
+            String frontEndNodes,
+            Integer queryPort,
+            String username,
+            String password,
+            DorisConfig config,
+            String defaultDatabase) {
+        this(catalogName, frontEndNodes, queryPort, username, password, config);
+        this.defaultDatabase = defaultDatabase;
+    }
+
+    @Override
+    public void open() throws CatalogException {
+        String jdbcUrl =
+                DorisCatalogUtil.getJdbcUrl(
+                        DorisCatalogUtil.randomFrontEndHost(frontEndNodes),
+                        queryPort,
+                        defaultDatabase);
+        try {
+            conn = DriverManager.getConnection(jdbcUrl, username, password);
+            conn.getCatalog();
+        } catch (SQLException e) {
+            throw new CatalogException(String.format("Failed to connect url %s", jdbcUrl), e);
+        }
+        LOG.info("Catalog {} established connection to {} success", catalogName, jdbcUrl);
+    }
+
+    @Override
+    public void close() throws CatalogException {
+        try {
+            conn.close();
+        } catch (SQLException e) {
+            throw new CatalogException("close doris catalog failed", e);
+        }
+    }
+
+    @Override
+    public String getDefaultDatabase() throws CatalogException {
+        return defaultDatabase;
+    }
+
+    @Override
+    public boolean databaseExists(String databaseName) throws CatalogException {
+        String query = DorisCatalogUtil.getDatabaseQuery();
+        try (PreparedStatement ps = conn.prepareStatement(query)) {
+            ps.setString(1, databaseName);
+            ResultSet rs = ps.executeQuery();
+            return rs.next();
+        } catch (SQLException e) {
+            throw new CatalogException("check database exists failed", e);
+        }
+    }
+
+    @Override
+    public List<String> listDatabases() throws CatalogException {
+        String query = DorisCatalogUtil.getAllDatabasesQuery();
+        List<String> databases = new ArrayList<>();
+        try (PreparedStatement ps = conn.prepareStatement(query)) {
+            ResultSet rs = ps.executeQuery();
+            while (rs.next()) {
+                String database = rs.getString(1);
+                databases.add(database);
+            }
+        } catch (SQLException e) {
+            throw new CatalogException("list databases failed", e);
+        }
+        Collections.sort(databases);
+        return databases;
+    }
+
+    @Override
+    public List<String> listTables(String databaseName)
+            throws CatalogException, DatabaseNotExistException {
+        String query = DorisCatalogUtil.getTablesQueryWithDatabase();
+        List<String> tables = new ArrayList<>();
+        try (PreparedStatement ps = conn.prepareStatement(query)) {
+            ps.setString(1, databaseName);
+            ResultSet rs = ps.executeQuery();
+            while (rs.next()) {
+                String table = rs.getString(1);
+                tables.add(table);
+            }
+        } catch (SQLException e) {
+            throw new CatalogException(
+                    String.format("list tables of database [%s] failed", databaseName), e);
+        }
+        Collections.sort(tables);
+        return tables;
+    }
+
+    @Override
+    public boolean tableExists(TablePath tablePath) throws CatalogException {
+        String query = DorisCatalogUtil.getTablesQueryWithIdentifier();
+        try (PreparedStatement ps = conn.prepareStatement(query)) {
+            ps.setString(1, tablePath.getDatabaseName());
+            ps.setString(2, tablePath.getTableName());
+            ResultSet rs = ps.executeQuery();
+            return rs.next();
+        } catch (SQLException e) {
+            throw new CatalogException(
+                    String.format("check table [%s] exists failed", tablePath.getFullName()), e);
+        }
+    }
+
+    @Override
+    public CatalogTable getTable(TablePath tablePath)
+            throws CatalogException, TableNotExistException {
+
+        if (!tableExists(tablePath)) {
+            throw new TableNotExistException(catalogName, tablePath);
+        }
+        TableSchema.Builder builder = TableSchema.builder();
+        String query = DorisCatalogUtil.getTableSchemaQuery();
+        try (PreparedStatement ps = conn.prepareStatement(query)) {
+
+            List<String> keyList = new ArrayList<>();
+            ps.setString(1, tablePath.getDatabaseName());
+            ps.setString(2, tablePath.getTableName());
+            ResultSet rs = ps.executeQuery();
+            while (rs.next()) {
+                String name = rs.getString(1);
+                int size = rs.getInt(6);
+                boolean nullable = rs.getBoolean(4);
+                String defaultVal = rs.getString(3);
+                String comment = rs.getString(10);
+                builder.column(
+                        PhysicalColumn.of(
+                                name,
+                                DorisCatalogUtil.fromDorisType(rs),
+                                size,
+                                nullable,
+                                defaultVal,
+                                comment));
+                if ("UNI".equalsIgnoreCase(rs.getString(7))) {
+                    keyList.add(name);
+                }
+            }
+            if (!keyList.isEmpty()) {
+                builder.primaryKey(
+                        PrimaryKey.of(
+                                "uk_"
+                                        + tablePath.getDatabaseName()
+                                        + "_"
+                                        + tablePath.getTableName(),
+                                keyList));
+            }
+
+        } catch (SQLException e) {
+            throw new CatalogException(
+                    String.format("get table [%s] failed", tablePath.getFullName()), e);
+        }
+
+        String comment = "";
+
+        return CatalogTable.of(
+                TableIdentifier.of(
+                        catalogName, tablePath.getDatabaseName(), tablePath.getTableName()),
+                builder.build(),
+                connectorOptions(),
+                Collections.emptyList(),
+                comment);

Review Comment:
   ```suggestion
           return CatalogTable.of(
                   TableIdentifier.of(
                           catalogName, tablePath.getDatabaseName(), tablePath.getTableName()),
                   builder.build(),
                   connectorOptions(),
                   Collections.emptyList(),
                   "");
   ```



##########
seatunnel-connectors-v2/connector-doris/src/main/java/org/apache/seatunnel/connectors/doris/util/DorisCatalogUtil.java:
##########
@@ -0,0 +1,279 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.seatunnel.connectors.doris.util;
+
+import org.apache.seatunnel.api.sink.SaveModeConstants;
+import org.apache.seatunnel.api.table.catalog.CatalogTable;
+import org.apache.seatunnel.api.table.catalog.Column;
+import org.apache.seatunnel.api.table.catalog.TablePath;
+import org.apache.seatunnel.api.table.catalog.TableSchema;
+import org.apache.seatunnel.api.table.catalog.exception.CatalogException;
+import org.apache.seatunnel.api.table.type.BasicType;
+import org.apache.seatunnel.api.table.type.DecimalType;
+import org.apache.seatunnel.api.table.type.LocalTimeType;
+import org.apache.seatunnel.api.table.type.SeaTunnelDataType;
+
+import org.apache.commons.lang3.StringUtils;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+
+public class DorisCatalogUtil {
+
+    public static String randomFrontEndHost(String[] frontEndNodes) {
+        if (frontEndNodes.length == 1) {
+            return frontEndNodes[0].split(":")[0];
+        }
+        List<String> list = Arrays.asList(frontEndNodes);
+        Collections.shuffle(list);
+        return list.get(0).split(":")[0];
+    }
+
+    public static String getJdbcUrl(String host, Integer port, String database) {
+        return String.format("jdbc:mysql://%s:%d/%s", host, port, database);
+    }
+
+    public static String getAllDatabasesQuery() {
+        return "SELECT SCHEMA_NAME FROM information_schema.schemata WHERE CATALOG_NAME = 'internal' ORDER BY SCHEMA_NAME";
+    }
+
+    public static String getDatabaseQuery() {
+        return "SELECT SCHEMA_NAME FROM information_schema.schemata "
+                + "WHERE CATALOG_NAME = 'internal' AND SCHEMA_NAME = ? "
+                + "ORDER BY SCHEMA_NAME";
+    }
+
+    public static String getTablesQueryWithDatabase() {
+        return "SELECT TABLE_NAME FROM information_schema.tables "
+                + "WHERE TABLE_CATALOG = 'internal' AND TABLE_SCHEMA = ? "
+                + "ORDER BY TABLE_NAME";
+    }
+
+    public static String getTablesQueryWithIdentifier() {
+        return "SELECT TABLE_NAME FROM information_schema.tables "
+                + "WHERE TABLE_CATALOG = 'internal' AND TABLE_SCHEMA = ? AND TABLE_NAME = ? "
+                + "ORDER BY TABLE_NAME";
+    }
+
+    public static String getTableSchemaQuery() {
+        return "SELECT COLUMN_NAME,ORDINAL_POSITION,COLUMN_DEFAULT,IS_NULLABLE,COLUMN_TYPE,COLUMN_SIZE,"
+                + "COLUMN_KEY,NUMERIC_PRECISION,NUMERIC_SCALE,COLUMN_COMMENT "
+                + "FROM information_schema.columns "
+                + "WHERE TABLE_CATALOG = 'internal' AND TABLE_SCHEMA = ? AND TABLE_NAME = ? "
+                + "ORDER BY ORDINAL_POSITION";
+    }

Review Comment:
   Why put these sql as method? Please use `static final String` to store these sql.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@seatunnel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org