解析oracle的DDL语句生成高斯内表及表字段主键配置

news/2024/12/22 13:13:35/

oracle的DDL语句如下:

CREATE TABLE TPPROD.CONFIG (NO VARCHAR2(50),CONFIGCODE VARCHAR2(400),CONFIGVALUE VARCHAR2(400),CONSTRAINT PK_GUENDORASSISTCONFIG PRIMARY KEY (NO,CONFIGCODE)
);
CREATE UNIQUE INDEX PK_GUENDORASSISTCONFIG ON TPPROD.GUENDORASSISTCONFIG (NO,CONFIGCODE);

解析oracle表生成高斯内表入口:Oracle2GaussCreateMana.scala

package com.tpiods.sqoop.oracle2gaussimport java.io.{File, FileInputStream, PrintWriter}
/*** 输入: oracle建表语句* 输出: gauss内表建表语句*/
object Oracle2GaussCreateMana {def main(args: Array[String]): Unit = {// 指定输入sql语句的文件val input = "ods_etl/src/main/resources/work0409_test/test_tab.sql"val lines = scala.io.Source.fromInputStream(new FileInputStream(input)).getLines()val str = readSql(lines)val str1 = replaceSql(str)val str2 = addTeacCols3(str1)// 指定输出sql语句的文件val output1 = "ods_etl/src/main/resources/work0409_test/test_tab_mana.sql"val writer = new PrintWriter(new File(output1))writer.write(str2)writer.close()}/*** 处理建表语句的转换逻辑* @param lines* @return*/def readSql(lines: Iterator[String]): String = {val sb = new StringBuildervar colCnt = 0var pkCols: String = ""for (line <- lines if line != null) {if (line.startsWith("CREATE TABLE") || line.startsWith("  CREATE TABLE") || line.startsWith("  CREATE TABLE")) {colCnt = colCnt + 1val tabName = line.split("\\.")(1).replaceAll(" ", "").replaceAll("\\(", "")if (sb.length >= 1) {sb.append(s"""|set search_path = ods;|drop table if exists ${tabName};|create table if not exists ${tabName} (\t\n""".stripMargin)} else {sb.append(s"""|set search_path = ods;|drop table if exists ${tabName};|create table if not exists ${tabName} (\t\n""".stripMargin)}}if (line.startsWith("\t\"")  || line.startsWith("\t")) {val split = line.split("\\s+")if (!("CONSTRAINT" == split(1)) && split.length >= 3) {if (colCnt == 1) {sb.append(" ").append(split(1)).append("\t").append(split(2)).append("\n")} else {sb.append(",").append(split(1)).append("\t").append(split(2)).append("\n")}}colCnt = 0}if (line.startsWith("   (") || line.startsWith(" (    ")) {val split = line.split("\\s+")sb.append(split(2)).append("\t").append(split(3)).append("\n")}if (line.indexOf("PRIMARY KEY ")> 0) {pkCols = line.split("PRIMARY KEY ")(1)}if (line.startsWith(");") || line.endsWith("MOVEMENT  |") || line.startsWith("   )")) {
//        sb.append(
//          s""") WITH (orientation=column, compression=low)
//            |DISTRIBUTE BY HASH${pkCols}
//            |;
//            |""".stripMargin)// 不按主键做hash,则打开下面这段代码sb.append(s""");|""".stripMargin)}}sb.toString()}/*** oracle数据类型替换成gauss对应的数据类型* @param str* @return*/def replaceSql(str: String): String = {str.toLowerCase().replaceAll("\"", "").replaceAll("number\\(\\*+,\\d*\\)", "number").replaceAll(",\n", "\n").replaceAll("\\b" + "nchar" + "\\b", "char").replaceAll("\\b" + "varchar2|nvarchar2" + "\\b", "varchar").replaceAll("\\b" + "long" + "\\b", "text")}/**** @param lines2 读入字段类型替换后的sql* @return 业务字段之后增加技术字段*/def addTeacCols(lines2: String): String = {lines2.replaceAll("\\) with",""",oper	varchar|,mtime	numeric(38,5)|,source_sys	varchar|,etl_time	varchar|,etl_date	varchar|)|with""".stripMargin)}/***不按主键做hash,调用该方法* @param lines2 读入字段类型替换后的sql* @return 业务字段之后增加技术字段*/def addTeacCols2(lines2: String): String = {lines2.replaceAll("\\);",""",oper	varchar|,mtime	numeric(38,5)|,source_sys	varchar|,etl_time	varchar|,etl_date	varchar|);|""".stripMargin)}/*** 按主键做hash* @param lines2 读入字段类型替换后的sql* @return 业务字段之前增加技术字段*/def addTeacCols3(lines2: String): String = {lines2.replaceAll("\t\n","""| tabname varchar|,oper	varchar|,mtime	numeric(38,5)|,source_sys	varchar|,etl_time	varchar|,etl_date	varchar|,""".stripMargin)}/*** 不按主键做hash,调用这个方法* @param lines2 读入字段类型替换后的sql* @return 业务字段之前增加技术字段*/def addTeacCols4(lines2: String): String = {lines2.replaceAll("\t\n","""| tabname varchar|,oper	varchar|,mtime	numeric(38,5)|,source_sys	varchar|,etl_time	varchar|,etl_date	varchar|,""".stripMargin)}
}

运行以上代码,输出如下:

set search_path = ods;
drop table if exists config;
create table if not exists config (tabname varchar
,oper	varchar
,mtime	numeric(38,5)
,source_sys	varchar
,etl_time	varchar
,etl_date	varchar
, no	varchar(50)
,configcode	varchar(400)
,configvalue	varchar(400)
);

解析oracle表生成表名字段主键配置入口:OracleGeneTabColsPk.scala

package com.tpiods.sqoop.oracle2gaussimport java.io.{File, FileInputStream, PrintWriter}
/*** 输入: oracle源系统建表语句* 输出: 表名|列名...|主键*/
object OracleGeneTabColsPk {def main(args: Array[String]): Unit = {// 指定输入sql语句的文件val input = "ods_etl/src/main/resources/work0409_test/test_tab.sql"val lines = scala.io.Source.fromInputStream(new FileInputStream(input)).getLines()val str = readSql(lines)// 指定输出sql语句的文件val output = "ods_etl/src/main/resources/work0409_test/test_tab_tabcolspk.txt"val writer = new PrintWriter(new File(output))writer.write(str)writer.close()}def readSql(lines: Iterator[String]): String = {val sb = new StringBuildervar tableName: String = nullfor (tempString <- lines if tempString != null) {if (tempString.startsWith("CREATE TABLE") || tempString.startsWith("  CREATE TABLE")) {tableName = tempString.split("\\.")(1).replaceAll(" ", "")sb.append("\n").append(tableName).append("|")} else if (tempString.indexOf("PRIMARY KEY") >= 0) {val pkName = tempString.split("\\(")(1).replaceAll(" ", "").replaceAll("\\)", "")sb.append("|").append(pkName)}else if (tempString.startsWith("   (") || tempString.startsWith(" (    ")) {val split = tempString.split("\\s+")sb.append(split(2)).append(",")}else if (tempString.startsWith("\t\"") || tempString.startsWith("\t")) {val split = tempString.split("\\s+")if (!("CONSTRAINT" == split(1)) && split.length >= 3) {sb.append(split(1)).append(",")}}}val sb2 = sb.toString().toLowerCase().replaceAll("\"", "").replaceAll(",\n", "\n").replaceAll(",\\|", "\\|").replaceAll("\\(\\|", "\\|")if (sb2.endsWith(",")) {sb2.substring(0, sb2.length() - 1)} else {sb2}}}

运行以上代码,输出如下:

config|no,configcode,configvalue|no,configcode

http://www.ppmy.cn/news/1413669.html

相关文章

HDLbits 刷题 --7420

The 7400-series integrated circuits are a series of digital chips with a few gates each. The 7420 is a chip with two 4-input NAND gates. Create a module with the same functionality as the 7420 chip. It has 8 inputs and 2 outputs. 译&#xff1a; 7400系列集…

阿里云ACK k8s集群迁移

1、创建k8s集群 阿里云ACK创建 2、创建所需的第三方插件 ACK ingress创建 3、导出原有ACK集群配置 包含deployment、service、ingress、PersistentVolumeClaim 执行命令 kubectl get deployment,service,secret,ingress,PersistentVolumeClaim -n <namespace_name> -o y…

问题整理【2024-04-10】

一、根据实体类自动生成数据表字段 &#xff08;actable框架&#xff09; 1.1、源由 一直很疑惑&#xff0c;xxx项目是怎么做到根据实体类生成/更新数据表字段的&#xff08;猜测是根据xxx注解&#xff0c;然后使用mysql的DDL操作表的结构&#xff09; 后来在同事的帮助下&a…

javascript密码验证规则,数字、字母、特殊符号两种以上

在验证密码复杂的正则表达试中&#xff0c;几位码友有发布过从两种字符里选两种的&#xff0c;从三种字符里选有三种的。 今天我提一个从三种里选两种的。有喜欢的朋友拿去用。 密码须包含数字、字母、特殊符号两种以上&#xff0c;且不少于8位&#xff0c;不多于32位。 pass…

20240410解决OK3588-C的核心板刷机之后无法启动的问题

20240410解决OK3588-C的核心板刷机之后无法启动的问题 2024/4/10 19:38 1、编译OK3588的LINUX/Buildroot&#xff1f;forlinxubuntu: ~/3588/OK3588_Linux_fs$ sudo ./build.sh BoardConfig-linuxfs-ok3588.mk 2、进行全编译 forlinxubuntu: ~/3588/OK3588_Linux_fs$ sudo ./bu…

ETLCloud结合kafka的数据集成

一、ETLCloud中实时数据集成的使用 在ETLCloud中数据集成有两种方式&#xff0c;一种是离线数据集成&#xff0c;另一种便是我们今天所要介绍的实时数据集成了&#xff0c;两者的区别从名字便可以得知&#xff0c;前者处理的数据是离线的没有时效性的&#xff0c;后者的数据是…

Spring和Spring MVC和MyBatis面试题

面试题1&#xff1a;请简述Spring、Spring MVC和MyBatis在整合开发中的作用&#xff1f; 答案&#xff1a; Spring&#xff1a;是一个轻量级的控制反转&#xff08;IoC&#xff09;和面向切面&#xff08;AOP&#xff09;的容器框架。它提供了强大的依赖注入功能&#xff0c;…

Spring与SpringBoot的区别

Spring是一个开源的Java应用程序框架&#xff0c;旨在简化企业级Java应用程序的开发。它提供了一个轻量级的容器&#xff0c;用于管理应用程序中的各个组件&#xff08;如依赖注入、AOP等&#xff09;&#xff0c;并提供了丰富的功能和模块&#xff0c;用于处理数据库访问、事务…