yihua commented on code in PR #18227: URL: https://github.com/apache/hudi/pull/18227#discussion_r2957089844
########## hudi-sync/hudi-hive-sync/src/main/java/org/apache/hudi/hive/ddl/JDBCBasedMetadataOperator.java: ########## @@ -0,0 +1,274 @@ +/* + * 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.hudi.hive.ddl; + +import org.apache.hudi.common.util.Option; +import org.apache.hudi.hive.HoodieHiveSyncException; +import org.apache.hudi.sync.common.model.FieldSchema; +import org.apache.hudi.sync.common.model.Partition; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +/** + * Provides metadata query operations (tableExists, databaseExists, + * getTableSchema, etc.) backed by a JDBC connection to HiveServer2. + * + * <p>This is used as a fallback when the Thrift-based + * {@link org.apache.hadoop.hive.metastore.IMetaStoreClient} is + * incompatible with the target HMS version (e.g., HMS 4.x changed the + * Thrift API from {@code get_table} to {@code get_table_req}). + * + * <p>All SQL queries use standard HiveQL that is stable across Hive + * versions. The operator does not manage the lifecycle of the JDBC + * connection — the caller ({@link JDBCExecutor}) owns it. + */ +public class JDBCBasedMetadataOperator { + + private static final Logger LOG = LoggerFactory.getLogger(JDBCBasedMetadataOperator.class); + + private final Connection connection; + private final String databaseName; + + public JDBCBasedMetadataOperator(Connection connection, String databaseName) { + this.connection = connection; + this.databaseName = databaseName; + } + + /** + * Checks if a table exists via {@code SHOW TABLES ... LIKE}. + */ + public boolean tableExists(String tableName) { + try (Statement stmt = connection.createStatement(); + ResultSet rs = stmt.executeQuery( + "SHOW TABLES IN `" + databaseName + "` LIKE '" + tableName + "'")) { + return rs.next(); + } catch (SQLException e) { + throw new HoodieHiveSyncException( + "Failed to check if table exists via JDBC: " + tableName, e); + } + } + + /** + * Checks if a database exists via {@code SHOW DATABASES LIKE}. + */ + public boolean databaseExists(String dbName) { + try (Statement stmt = connection.createStatement(); + ResultSet rs = stmt.executeQuery( + "SHOW DATABASES LIKE '" + dbName + "'")) { + return rs.next(); + } catch (SQLException e) { + throw new HoodieHiveSyncException( + "Failed to check if database exists via JDBC: " + dbName, e); + } + } + + /** + * Retrieves field schemas via {@code DESCRIBE}. + */ + public List<FieldSchema> getFieldSchemas(String tableName) { + List<FieldSchema> fields = new ArrayList<>(); + String fqTable = "`" + databaseName + "`.`" + tableName + "`"; + try (Statement stmt = connection.createStatement(); + ResultSet rs = stmt.executeQuery("DESCRIBE " + fqTable)) { + while (rs.next()) { + String colName = rs.getString(1); + String colType = rs.getString(2); + String comment = rs.getString(3); + if (colName != null && !colName.trim().isEmpty() + && !colName.startsWith("#")) { + fields.add(new FieldSchema( + colName.trim(), + colType != null ? colType.trim() : "", + comment)); + } + } + } catch (SQLException e) { + throw new HoodieHiveSyncException( + "Failed to get field schemas via JDBC for: " + tableName, e); + } + return fields; + } + + /** + * Retrieves a single table property via {@code SHOW TBLPROPERTIES}. + */ + public Option<String> getTableProperty(String tableName, String key) { + String fqTable = "`" + databaseName + "`.`" + tableName + "`"; + try (Statement stmt = connection.createStatement(); + ResultSet rs = stmt.executeQuery( + "SHOW TBLPROPERTIES " + fqTable + " ('" + key + "')")) { + if (rs.next()) { + String value = rs.getString(2); + if (value != null && !value.contains("does not exist")) { + return Option.of(value); + } + } + return Option.empty(); + } catch (SQLException e) { + throw new HoodieHiveSyncException( + "Failed to get table property via JDBC: " + key, e); + } + } + + /** + * Sets table properties via {@code ALTER TABLE ... SET TBLPROPERTIES}. + */ + public void setTableProperties(String tableName, Map<String, String> properties) { + String fqTable = "`" + databaseName + "`.`" + tableName + "`"; + StringBuilder sb = new StringBuilder("ALTER TABLE ") + .append(fqTable).append(" SET TBLPROPERTIES ("); + boolean first = true; + for (Map.Entry<String, String> entry : properties.entrySet()) { + if (!first) { + sb.append(", "); + } + sb.append("'").append(entry.getKey()).append("'='") + .append(entry.getValue()).append("'"); Review Comment: This is resolved. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
