1.写出文件工具类

package ccc.utile;

import java.io.*;

/**
* @author ccc
* @version 1.0.0
* @ClassName WriteToFileExample.java
* @Description TODO IO流
* @ProjectName ccc
* @createTime 2021年08月07日 18:32:00
*/
public class WriteToFileExample {
/**
* 追加写入数据到指定文件
*
* @param str
* @param path
*/
public void writeFileSQL(String str, String path) {
FileWriter fw = null;
try {
File f = new File(path);
fw = new FileWriter(f, true);
} catch (IOException e) {
e.printStackTrace();
}
PrintWriter pw = new PrintWriter(fw);
pw.println(str);
pw.flush();
try {
fw.flush();
pw.close();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
} /**
* 清空文件内容
*
* @param fileName
*/
public void clearInfoForFile(String fileName) {
File file = new File(fileName);
try {
if (!file.exists()) {
file.createNewFile();
}
FileWriter fileWriter = new FileWriter(file);
fileWriter.write("");
fileWriter.flush();
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
} }

2.jdbc工具类:

package ccc.utile;

import java.sql.*;
import java.util.Map; /**
* @author ccc
* @version 1.0.0
* @ClassName JDBCMySQL.java
* @Description TODO MySQLJDBC链接
* @ProjectName ccc
* @createTime 2021年08月06日 14:19:00
*/
public class JDBCJAVAMySQL {
public static Connection getConnection() {
//定义Connection对象
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");// conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/", "root", "123456");
} catch (Exception e) {
e.printStackTrace();
}
return conn;
} private static void connection(Connection connection) {
if (connection != null) {
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
} private static void resultSet(ResultSet resultSet) {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
} private static void preparedStatement(PreparedStatement preparedStatement) {
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
} /*
* @Description TODO 关闭连接
* @Date 2021/7/21 22:42
* @param
* @return
*/
public static void close(Connection connection, ResultSet resultSet, PreparedStatement preparedStatement) {
connection(connection);
resultSet(resultSet);
preparedStatement(preparedStatement);
}
}

3.表属性实体类:

package ccc.enty;

/**
* @author ccc
* @version 1.0.0
* @ClassName TableSchema.java
* @Description TODO
* @ProjectName ccc
* @createTime 2021年08月06日 14:58:00
*/
public class TableSchema {
private String table_name;
private String table_comment; public String getTable_name() {
return table_name;
} public void setTable_name(String table_name) {
this.table_name = table_name;
} public String getTable_comment() {
return table_comment;
} public void setTable_comment(String table_comment) {
this.table_comment = table_comment;
} @Override
public String toString() {
return "TableSchema{" +
"table_name='" + table_name + '\'' +
", table_comment='" + table_comment + '\'' +
'}';
}
}

4.表结构实体类:

package ccc.enty;

/**
* @author ccc
* @version 1.0.0
* @ClassName ColumnSchema.java
* @Description TODO
* @ProjectName ccc
* @createTime 2021年08月06日 14:59:00
*/
public class ColumnSchema {
private String column_name;
private String column_comment;
private String column_type; public String getColumn_name() {
return column_name;
} public void setColumn_name(String column_name) {
this.column_name = column_name;
} public String getColumn_comment() {
return column_comment;
} public void setColumn_comment(String column_comment) {
this.column_comment = column_comment;
} public String getColumn_type() {
return column_type;
} public void setColumn_type(String column_type) {
this.column_type = column_type;
} @Override
public String toString() {
return "ColumnSchema{" +
"column_name='" + column_name + '\'' +
", column_comment='" + column_comment + '\'' +
", column_type='" + column_type + '\'' +
'}';
}
}

5.启动类:

package ccc.contorller;

import ccc.enty.ColumnSchema;
import ccc.enty.TableSchema;
import ccc.utile.JDBCJAVAMySQL;
import ccc.utile.WriteToFileExample; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List; /** 需关闭连接
* @author ccc
* @version 1.0.0
* @ClassName BatchMySQL2HIVE.java
* @Description TODO 通过MySQL原数据信息,生成HIVE建表语句
* @ProjectName ccc
* @createTime 2021年08月06日 14:52:00
*/
public class BatchMySQL2HIVE { /**
* 获取表信息
*
* @return
*/
public static List<TableSchema> getTable_schema(String databases) {
List<TableSchema> list = new ArrayList<TableSchema>();
String sql = "SELECT a.table_name,a.table_comment FROM information_schema.`TABLES` a where a.table_schema=" + "\"" + databases + "\"";
PreparedStatement ps = null;
ResultSet resultSet = null;
Connection connection = JDBCJAVAMySQL.getConnection();
try {
ps = connection.prepareStatement(sql);
resultSet = ps.executeQuery();
while (resultSet.next()) {
TableSchema a = new TableSchema();
a.setTable_name(resultSet.getString("table_name"));
a.setTable_comment(resultSet.getString("table_comment"));
list.add(a);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
JDBCJAVAMySQL.close(connection, resultSet, ps);
}
return list;
} /**
* 获取表结构信息
*
* @return
*/
public static List<ColumnSchema> getColumn_schema(String database, String table_name) {
List<ColumnSchema> list = new ArrayList<ColumnSchema>();
String c = "SELECT a.column_name,a.column_comment,a.data_type FROM information_schema.`COLUMNS` a where a.table_schema=" + "\"" + database + "\" ";
String b = " and a.table_name=" + "\"" + table_name + "\"";
String sql = c + b;
System.out.println(sql);
Connection connection = JDBCJAVAMySQL.getConnection();
PreparedStatement ps = null;
ResultSet resultSet = null;
try {
ps = connection.prepareStatement(sql);
resultSet = ps.executeQuery();
while (resultSet.next()) {
ColumnSchema a = new ColumnSchema();
a.setColumn_comment(resultSet.getString("column_comment"));
a.setColumn_name(resultSet.getString("column_name"));
a.setColumn_type(resultSet.getString("data_type"));
list.add(a);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return list;
} /**
* 生成表结构
*
* @param j
* @return
*/
public static String createTable(String database, int j) {
StringBuffer sb = new StringBuffer();
List<TableSchema> table_schema = getTable_schema(database);
List<ColumnSchema> column_schema = getColumn_schema(database, table_schema.get(j).getTable_name());
sb.append("--" + getTable_comment(table_schema.get(j).getTable_comment(), table_schema.get(j).getTable_name()) + ":" + table_schema.get(j).getTable_name() + "\n");
sb.append("CREATE TABLE IF NOT EXISTS " + table_schema.get(j).getTable_name() + "(" + "\n");
int f = 0;
for (int i = 0; i < column_schema.size(); i++) {
//判断是否是最后一个字段,如果是则不加都号
if (f == column_schema.size() - 1) {
sb.append(" " + tranColumn2xx(column_schema.get(i).getColumn_name()) + " " + getColumn_type(column_schema.get(i).getColumn_type()) + " COMMENT " + getColumn_Comment(column_schema.get(i).getColumn_comment()) + "\n");
} else {
sb.append(" " + tranColumn2xx(column_schema.get(i).getColumn_name()) + " " + getColumn_type(column_schema.get(i).getColumn_type()) + " COMMENT " + getColumn_Comment(column_schema.get(i).getColumn_comment()) + "," + "\n");
}
f++;
}
sb.append(") COMMENT " + "\"" + getTable_comment(table_schema.get(j).getTable_comment(), table_schema.get(j).getTable_name()) + "\"" + ";" + "\n");
return sb.toString();
} /**
* 填充字段注释
*
* @param comment
* @return
*/
public static String getColumn_Comment(String comment) {
if (comment == null || comment.equals("")) {
return "\"\"";
} else {
return "\"" + comment + "\"";
}
} /**
* 填充表注释
*
* @param comment
* @param table_name
* @return
*/
public static String getTable_comment(String comment, String table_name) {
if (comment == null || comment.equals("")) {
return table_name;
} else {
return comment;
}
} /**
* 匹配类型
*
* @param column_type
* @return
*/
public static String getColumn_type(String column_type) {
if ("int".equals(column_type)) {
return "BIGINT";
} else if ("tinyint".equals(column_type)) {
return "BIGINT";
} else if ("bigint".equals(column_type)) {
return "BIGINT";
} else if ("smallint".equals(column_type)) {
return "BIGINT";
} else if ("mediumint".equals(column_type)) {
return "BIGINT";
} else if ("float".equals(column_type)) {
return "DOUBLE";
} else if ("double".equals(column_type)) {
return "DOUBLE";
} else if ("decimal".equals(column_type)) {
return "STRING";
} else if ("numeric".equals(column_type)) {
return "STRING";
} else if ("bit".equals(column_type)) {
return "STRING";
} else if ("char".equals(column_type)) {
return "STRING";
} else if ("varchar".equals(column_type)) {
return "STRING";
} else if ("blob".equals(column_type)) {
return "STRING";
} else if ("mediumblob".equals(column_type)) {
return "STRING";
} else if ("longblob".equals(column_type)) {
return "STRING";
} else if ("tinytext".equals(column_type)) {
return "STRING";
} else if ("mediumtext".equals(column_type)) {
return "STRING";
} else if ("longtext".equals(column_type)) {
return "STRING";
} else if ("binary".equals(column_type)) {
return "STRING";
} else if ("varbinary".equals(column_type)) {
return "STRING";
} else if ("time".equals(column_type)) {
return "STRING";
} else if ("datetime".equals(column_type)) {
return "STRING";
} else if ("timestemp".equals(column_type)) {
return "STRING";
} else if ("year".equals(column_type)) {
return "STRING";
} else if ("date".equals(column_type)) {
return "STRING";
} else if ("text".equals(column_type)) {
return "STRING";
}else if ("longtext".equals(column_type)) {
return "STRING";
} else {
return "STRING";
}
} /**
* 字段转小写
*
* @param column_name 传入原始字段
* @return 返回转换字段
*/
public static String tranColumn2xx(String column_name) {
return column_name.toLowerCase();
} /**
* 批量启动
*
* @param database 数据库名称
* @param path 写入文件路径
*/
public static void start(String database, String path) {
List<TableSchema> table_schema = getTable_schema(database);
WriteToFileExample writeToFileExample = new WriteToFileExample();
writeToFileExample.clearInfoForFile(path);
int f = 0;
for (int i = 0; i < table_schema.size(); i++) {
String table = createTable(database, i);
System.out.println(table);
writeToFileExample.writeFileSQL(table, path);
f++;
}
System.out.println("共记录:" + f + "条数据!");
} public static void main(String[] args) {
start("CCC", "mysql2HIVE.sql");
}
}

利用MySQL原数据信息批量转换指定库数据表生成Hive建表语句的更多相关文章

  1. mysql根据查询结果批量更新多条数据(插入或更新)

    mysql根据查询结果批量更新多条数据(插入或更新) 1.1 前言 mysql根据查询结果执行批量更新或插入时经常会遇到1093的错误问题.基本上批量插入或新增都会涉及到子查询,mysql是建议不要对 ...

  2. MySQL的表分区详解 - 查看分区数据量,查看全库数据量----转http://blog.csdn.net/xj626852095/article/details/51245844

    查看分区数据量,查看全库数据量 USE information_schema; SELECT PARTITION_NAME,TABLE_ROWS FROM INFORMATION_SCHEMA.PAR ...

  3. Mysql元数据生成Hive建表语句注释脚本

    在将数据从Mysql 等其他关系型数据库 抽取到Hive 表中时,需要同步mysql表中的注释,以下脚本可以生成hive表字段注释修改语句. 注:其他关系型数据库如:oracle 可以通过相同的思路, ...

  4. [Hive_3] Hive 建表指定分隔符

    0. 说明 Hive 建表示例及指定分隔符 1. Hive 建表 Demo 在 Hive 中输入以下命令创建表 user2 create table users2 (id int, name stri ...

  5. hive建表没使用LZO存储格式,可是数据是LZO格式时遇到的问题

    今天微博大数据平台发邮件来说.他们有一个hql执行失败.可是从gateway上面的日志看不出来是什么原因导致的,我帮忙看了一下.最后找到了问题的解决办法,下面是分析过程: 1.执行失败的hql: IN ...

  6. hive建表与数据的导入导出

    建表: create EXTERNAL table tabtext(IMSI string,MDN string,MEID string,NAI string,DestinationIP string ...

  7. ABAP 动态备份自建表数据到新表(自建表有数据的情况下要改字段长度或者其他)

    当abaper开发好一个程序给用户使用一段时间后,发现某个字段的长度需要修改,但数据库表中已经存在很多数据,冒然直接改表字段可能会导致数据丢失,这种问题的后果可能非常严重. 所以我想到先复制出一个新表 ...

  8. mysql存储过程命令行批量插入N条数据命令

    原文:http://blog.csdn.net/tomcat_2014/article/details/53377924 delimiter $$ create procedure myproc () ...

  9. SqlServer与MySql 系统表查询自建表数据

    SqlServer: SELECT * FROM sys.sysobjects WHERE type='U' ORDER BY name SELECT * FROM sys.syscolumns WH ...

随机推荐

  1. unity调用安卓方法实现安装apk文件(androidx)

    原文链接:点击打开 unity想要实现安装apk文件需要与安卓通讯,所以需要自己来实现安卓代码. 第一步先要新建一个安卓项目提供给unity来使用,我这里使用的工具是android studio4.1 ...

  2. 『心善渊』Selenium3.0基础 — 15、Selenium对多窗口的操作

    目录 1.多标签/多窗口之间的切换 2.句柄练习 1.多标签/多窗口之间的切换 (1)多标签/多窗口场景: 在页面操作过程中有时候点击某个链接会弹出新的窗口,这时就需要切换到新打开的窗口上进行操作,如 ...

  3. 在Linux/Unix系统下用iconv命令处理文本文件中文乱码问题

    iconv命令是运行于linux/unix平台的文件编码装换工具.当我们在linux/unix系统shell查看文本文件时,常常会发现文件的中文是乱码的,这是由于文本文件的编码与当前操作系统设置的编码 ...

  4. 9.11、mysql增量备份和增量恢复介绍

    1.增量备份: 增量数据是从上次全量备份之后,更新的新数据,对于mysql来说,binlog日志就是mysql的增量数据: (1)按天进行备份: 周一00点全量备份 周二00点全量备份 ...... ...

  5. 13、mysql主从复制原理解析

    13.1.mysql主从复制介绍: 1.普通文件,磁盘上的文件的同步方法: (1)nfs网络文件共享可以同步数据存储: (2)samba共享数据: (3)ftp数据同步: (4)定时任务:cronta ...

  6. Vue 动态组件和异步组件

    基础案例 动态组件切换类比"bilibili-个人中心"的横向菜单切换不同的标签页的功能. 在Vue中可以使用 component 标签,并加一个特殊的属性(attribute) ...

  7. 01_JVM与Java体系结构

    JVM发展历程 Sun Classic VM Exact VM 为了解决上一个虚拟机问题,jdk1.2时,sun提供了此虚拟机. Exact Memory Management:准确式内存管理 SUN ...

  8. 记一次Struts中文乱码

    起因 最近公司一个智能家具的项目,需要开发后端,APP/WEB的所有请求通过HTPP发送到后台,后台通过socket连接到智能设备.公司只有一个Java技术栈的同事,而他负责设备方面,我只能赶鸭子上架 ...

  9. Windows软件包管理工具:Scoop

    前言 删库跑路后,Windows系统如何快速安装应用程序,部署环境呢? 以前想过这个问题,最近在安装Hugo时发现使用软件包管理工具可以解决这个问题. 阅读建议 首先需要测试下载速度,尝试从官网下载, ...

  10. ZYNQ 中PS端GPIO EMIO使用

    ZYNQ 中PS端GPIO EMIO使用 在使用ZYNQ进行开发设计时,往往需要对一些GPIO引脚进行配置,传统的配置方法通常在PL端进行管脚约束之后在Verilog代码中对相应引脚进行配置.这样如果 ...