使用java代码编写脚本,把oracle建表语句变成hive建表语句

本文介绍了如何使用Java代码实现从Oracle SQL建表语句到Hive SQL的转换,通过`O2H`类展示了将VARCHAR、TIMESTAMP等数据类型映射并简化的过程,最终生成了Hive创表语句。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用java代码编写脚本,把oracle建表语句变成hive建表语句

java代码

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class O2H {
    public static void main(String[] args) throws Exception {
        getFileContentThree("oracle.sql");
    }
    public static void getFileContentThree(String str) throws IOException {


        Scanner in = new Scanner(new File("D:\\Ideaprojects\\bigdata15_0612\\data\\oracle_sql\\"+str));
        while (in.hasNextLine()) {
            String s = in.nextLine();
            String s1 = s.replaceAll("\"", "");
            String s2 = s1.replaceAll("VARCHAR2\\(64 BYTE\\)", "string");
            String s3 = s2.replaceAll("NVARCHAR2\\(200\\)", "string");
            String s4 = s3.replaceAll("VARCHAR2\\(2000 BYTE\\)", "string");
            String s5 = s4.replaceAll("VARCHAR2\\(500 BYTE\\)", "string");
            String s6 = s5.replaceAll("VARCHAR2\\(200 BYTE\\)", "string");
            String s7 = s6.replaceAll("CLOB", "string");
            String s8 = s7.replaceAll("VARCHAR2\\(64 BYTE\\)", "string");
            String s9 = s8.replaceAll("TIMESTAMP\\(6\\)", "timestamp");
            String s10 = s9.replaceAll("NVARCHAR2\\(255\\)", "string");
            String s11 = s10.replaceAll("CHAR\\(1 BYTE\\)", "string");
            String s12 = s11.replaceAll("DEFAULT '0'   NOT NULL", "");
            String s13 = s12.replaceAll("NOT NULL", "");
            String s14 = s13.replaceAll("TEST\\.", "ODS_");
            String s15 = s14.replaceAll("VARCHAR2\\(255 BYTE\\)", "string");
            String s16 = s15.replaceAll("VARCHAR2\\(1000 BYTE\\)", "string");
            String s17 = s16.replaceAll("NUMBER", "double");
            String s18 = s17.replaceAll("VARCHAR2\\(400 BYTE\\)", "string");
            String s19 = s18.replaceAll("VARCHAR2\\(2550 BYTE\\)", "string");
            String s20 = s19.replaceAll("NVARCHAR2\\(64\\)", "string");
            String s21 = s20.replaceAll("NVARCHAR2\\(2000\\)", "string");
            String s22 = s21.replaceAll("DEFAULT \'\'", "");
            String s23 = s22.replaceAll("VARCHAR2\\(1 BYTE\\)", "string");
            String s24 = s23.replaceAll("VARCHAR2\\(100 BYTE\\)", "string");
            String s25 = s24.replaceAll("VARCHAR2\\(4000 BYTE\\)", "string");
            String s26 = s25.replaceAll("NVARCHAR2\\(500\\)", "string");
            String s27 = s26.replaceAll("NVARCHAR2\\(100\\)", "string");
            String s28 = s27.replaceAll("NUMBER\\(10,0\\)", "double");
            String s29 = s28.toLowerCase();
            String s30 = s29.replaceAll("\\)", ");");
            String s31 = s30.replaceAll("blob", "string");
            String s32 = s31.replaceAll("long", "string");


            System.out.println(s32);
        }
    }
}

测试oracle.sql

CREATE TABLE "TEST"."ORACLE" (
  "ID" VARCHAR2(64 BYTE) NOT NULL,
  "CATEGORY_ID" VARCHAR2(64 BYTE) NOT NULL,
  "CONTENT_ID" VARCHAR2(64 BYTE) NOT NULL,
  "TITLE" VARCHAR2(255 BYTE),
  "CONTENT" VARCHAR2(255 BYTE),
  "NAME" VARCHAR2(100 BYTE),
  "CREATE_DATE" TIMESTAMP(6) NOT NULL
)

生成hive创表语句

create table ods_oracle (
  id string ,
  category_id string ,
  content_id string ,
  title string,
  content string,
  name string,
  create_date timestamp
);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值