yzeng1618 commented on code in PR #9743: URL: https://github.com/apache/seatunnel/pull/9743#discussion_r2506795058
########## seatunnel-connectors-v2/connector-hive/src/main/java/org/apache/seatunnel/connectors/seatunnel/hive/utils/HiveTableTemplateUtils.java: ########## @@ -0,0 +1,276 @@ +/* + * 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.seatunnel.hive.utils; + +import org.apache.seatunnel.api.table.catalog.TableSchema; + +import lombok.extern.slf4j.Slf4j; + +import java.util.List; +import java.util.stream.Collectors; + +@Slf4j +public class HiveTableTemplateUtils { + + /** Get default Hive table creation template for non-partitioned tables */ + public static String getDefaultNonPartitionedTemplate() { + return "CREATE TABLE IF NOT EXISTS `${database}`.`${table}` (\n" + + " ${rowtype_fields}\n" + + ")\n" + + "STORED AS PARQUET\n" + + "LOCATION '${table_location}'\n" + + "TBLPROPERTIES (\n" + + " 'seatunnel.creation.mode' = 'template',\n" + + " 'seatunnel.created.time' = '${current_timestamp}'\n" + + ")"; + } + + /** Get default Hive table creation template for partitioned tables */ + public static String getDefaultPartitionedTemplate() { + return "CREATE TABLE IF NOT EXISTS `${database}`.`${table}` (\n" + + " ${rowtype_fields}\n" + + ")\n" + + "PARTITIONED BY (${rowtype_partition_fields})\n" + + "STORED AS PARQUET\n" + + "LOCATION '${table_location}'\n" + + "TBLPROPERTIES (\n" + + " 'seatunnel.creation.mode' = 'template',\n" + + " 'seatunnel.created.time' = '${current_timestamp}'\n" + + ")"; + } + + /** Generate field definitions for table creation */ + public static String generateFieldsDefinition( + TableSchema tableSchema, List<String> partitionFields) { + return tableSchema.getColumns().stream() + .filter(column -> !partitionFields.contains(column.getName())) + .map( + column -> { + String hiveType = + HiveTypeConvertor.seatunnelToHiveType(column.getDataType()); + String comment = + column.getComment() != null + ? " COMMENT '" + column.getComment() + "'" + : ""; + return String.format( + " `%s` %s%s", column.getName(), hiveType, comment); + }) + .collect(Collectors.joining(",\n")); + } + + /** Generate partition field definitions for table creation */ + public static String generatePartitionDefinition( + TableSchema tableSchema, List<String> partitionFields) { + if (partitionFields == null || partitionFields.isEmpty()) { + return ""; + } + + return partitionFields.stream() + .map( + partitionField -> { + // Try to get type from source schema first + String hiveType = + tableSchema.getColumns().stream() + .filter(col -> col.getName().equals(partitionField)) + .findFirst() + .map( + col -> + HiveTypeConvertor.seatunnelToHiveType( + col.getDataType())) + .orElse("string"); // Default to string for new + // partition fields + + return String.format( + " `%s` %s COMMENT 'Partition field'", + partitionField, hiveType); + }) + .collect(Collectors.joining(",\n")); + } + + /** Replace template variables with actual values */ + public static String replaceTemplateVariables( + String template, + String database, + String table, + String fieldsDefinition, + String partitionDefinition, + String tableLocation) { + + return template.replace("${database}", database) + .replace("${table}", table) + .replace("${rowtype_fields}", fieldsDefinition) + .replace("${rowtype_partition_fields}", partitionDefinition) + .replace("${table_location}", tableLocation) + .replace("${current_timestamp}", String.valueOf(System.currentTimeMillis())); + } + + /** Get default table location */ + public static String getDefaultTableLocation(String database, String table) { + return String.format("file:/tmp/hive/warehouse/%s.db/%s", database, table); Review Comment: In the current direct modification method, when creating a table in the local (non-HDFS) e2e environment, an attempt to create the path /user/hive/warehouse… in the container's root directory fails. Currently, the code has been further optimized: it checks whether the fs.defaultFS configuration in the loaded Hadoop settings starts with hdfs://. If it does, the system constructs an HDFS path with authority; otherwise, it falls back to the path file:/tmp/hive/warehouse/db.db/{table}. -- 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]
