將mysql表創建到hive腳本

將mysql表創建到hive腳本

mysql.java

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class mysql {

    private static String[] deleteArrayNull(String string[]) {
        ArrayList<String> strList = new ArrayList<>();

        // Loop through the input array and add non-null elements to the ArrayList
        for (String str : string) {
            if (str != null && !str.isEmpty()) {
                strList.add(str);
            }
        }

        // Convert the ArrayList back to an array of strings
        return strList.toArray(new String[0]);
    }

    public static String findColumnType(String str) {
        str = str.toLowerCase();
        String type;

        if (str.startsWith("int")) {
            type = "int";
        } else if (str.startsWith("bigint")) {
            type = "bigint";
        } else if (str.startsWith("decimal")) {
            type = "decimal"; // Assuming Hive's decimal type matches SQL's decimal type
        } else if (str.startsWith("bit")) {
            type = "int"; // Assuming mapping to INT in Hive
        } else if (str.startsWith("datetime") || str.startsWith("date") || str.startsWith("time")) {
            type = "string"; // These date-time related types mapped to STRING in Hive
        } else if (str.startsWith("float")) {
            type = "float";
        } else if (str.startsWith("double")) {
            type = "double";
        } else if (str.startsWith("boolean")) {
            type = "boolean";
        } else if (str.startsWith("tinyint")) {
            type = "int";
        }else {
            type = "string"; // Defaulting to STRING for unmatched types
        }

        return type;
    }

    public static void main(String[] args) {

        //mysql
        String str9 = "CREATE TABLE `us_position_md` (\n" +
                "  `pk_pos_id` int(10) NOT NULL AUTO_INCREMENT,\n" +
                "  `id` char(36) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,\n" +
                "  `unit_id` char(36) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,\n" +
                "  `position_name` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL,\n" +
                "  `bk_amount` varchar(250) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,\n" +
                "  `department_id` int(11) NOT NULL,\n" +
                "  `status` tinyint(1) NOT NULL,\n" +
                "  `car_status` tinyint(1) DEFAULT NULL,\n" +
                "  `explain` varchar(250) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT '岗位说明',\n" +
                "  `ex01` char(1) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,\n" +
                "  `ex02` char(1) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,\n" +
                "  `updated_at` datetime DEFAULT NULL,\n" +
                "  `updated_by` char(36) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,\n" +
                "  `created_at` datetime DEFAULT NULL,\n" +
                "  `created_by` char(36) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,\n" +
                "  `is_job` tinyint(4) DEFAULT NULL,\n" +
                "  PRIMARY KEY (`pk_pos_id`)\n" +
                ") ENGINE=InnoDB AUTO_INCREMENT=2852 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC";

        //mysql
        String tableName = str9.split("` \\(\n")[0].split("`")[1].toLowerCase();
        String[] columnLines = str9.split("` \\(\n")[1].split("PRIMARY KEY \\(")[0].split(",\n");
        StringBuilder hiveSqlStr = new StringBuilder();



        // Begin building the Hive SQL script
        hiveSqlStr.append("drop table if exists hr_us.ods_").append(tableName).append("_full;\n")
                .append("create external table if not exists hr_us.ods_").append(tableName).append("_full (").append("\n");

        String tableComment = "";
        Pattern tableCommentPattern = Pattern.compile("COMMENT='([^']*)'");
        Matcher tableCommentMatcher = tableCommentPattern.matcher(str9);
        if (tableCommentMatcher.find()) {
            tableComment = tableCommentMatcher.group(1);
        }
        // Regular expression to match comments in SQL
        Pattern commentPattern = Pattern.compile("COMMENT '([^']*)'");

        for (String line : columnLines) {
            String[] column = deleteArrayNull(line.replace("\n", "").split(" "));
            if (column.length >= 2) {
                String columnName = column[0].replace("[", "").replace("]", "").replace("`","").toLowerCase();

                Matcher matcher = commentPattern.matcher(line);
                String comment = "";
                if (columnName.equals("id")) {
                    comment = "id"; // Set comment as "id" if column name is "ID"
                }
                if (matcher.find()) {
                    comment = matcher.group(1);
                }
                if (columnName.equals("id") && comment.isEmpty()) {
                    comment = "id"; // Set default comment as "id" if the column name is "id" and the comment is empty
                }
                String typeName = findColumnType(column[1]);
                hiveSqlStr.append("  ").append(columnName).append("  ").append(typeName).append(" comment '").append(comment).append("',\n");
            }
        }

        hiveSqlStr.delete(hiveSqlStr.length() - 2, hiveSqlStr.length());
        hiveSqlStr.append("\n) comment '").append(tableComment).append("'\n")
                .append("partitioned by (dt string)\n")
                .append("ROW FORMAT DELIMITED FIELDS TERMINATED BY '\\001'\nNULL DEFINED AS ''\nLOCATION '/warehouse/hr_us/ods/ods_")
                .append(tableName).append("_full';");
        System.out.println(hiveSqlStr);
    }
}

相关推荐

  1. mysqlhive

    2024-03-21 14:26:03       19 阅读
  2. SqlServerhive

    2024-03-21 14:26:03       21 阅读
  3. Python脚本同步Hive结构MySQL

    2024-03-21 14:26:03       13 阅读
  4. Spark SQL将Hive中的数据写入MySQL数据库中

    2024-03-21 14:26:03       41 阅读
  5. MySQL 约束

    2024-03-21 14:26:03       22 阅读
  6. HiveMySQL、Oracle内函数对照表

    2024-03-21 14:26:03       42 阅读
  7. mysql&索引语句

    2024-03-21 14:26:03       20 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-21 14:26:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-21 14:26:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-21 14:26:03       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-21 14:26:03       20 阅读

热门阅读

  1. MPI4.1文档4-MPI数据类型 MPI DataTypes

    2024-03-21 14:26:03       18 阅读
  2. 邦芒解析:职场中得不到理想薪水的八大原因

    2024-03-21 14:26:03       18 阅读
  3. CSS是什么,它主要用于做什么?

    2024-03-21 14:26:03       15 阅读
  4. Android 图形渲染和显示系统关系

    2024-03-21 14:26:03       19 阅读
  5. CSS有哪些选择器?

    2024-03-21 14:26:03       21 阅读
  6. 共享旅游卡是什么?千益畅行算不算?

    2024-03-21 14:26:03       24 阅读
  7. HTML与WXML 、 CSS与WXSS的区别

    2024-03-21 14:26:03       19 阅读
  8. 设计模式(行为型设计模式——中介者模式)

    2024-03-21 14:26:03       22 阅读