代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
读取Hive中所有表的表结构,并在新Hive库中创建表,索引
作者:
/ 发布于
2017/3/29
/
978
读取Hive中表的结构。本文中包含Table类,Field类用于封装表结构
1. Table类 public class Table { private String tableName; private List<Field> field; public Table() { } public Table(String tableName, List<Field> field) { this.tableName = tableName; this.field = field; } public String getTableName() { return tableName; } public void setTableName(String tableName) { this.tableName = tableName; } public List<Field> getField() { return field; } public void setField(List<Field> field) { this.field = field; } @Override public String toString() { return "Table{" + "tableName=" + tableName + ", field=" + field + '}'; } } 2. Field类 public class Field { private String columnName; private String typeName; private int columnSize; private int decimal_digits; private int nullable; public Field() { } public Field(String columnName, String typeName, int columnSize, int decimal_digits, int nullable) { this.columnName = columnName; this.typeName = typeName; this.columnSize = columnSize; this.decimal_digits = decimal_digits; this.nullable = nullable; } public String getColumnName() { return columnName; } public void setColumnName(String columnName) { this.columnName = columnName; } public String getTypeName() { return typeName; } public void setTypeName(String typeName) { this.typeName = typeName; } public int getColumnSize() { return columnSize; } public void setColumnSize(int columnSize) { this.columnSize = columnSize; } public int getDecimal_digits() { return decimal_digits; } public void setDecimal_digits(int decimal_digits) { this.decimal_digits = decimal_digits; } public int getNullable() { return nullable; } public void setNullable(int nullable) { this.nullable = nullable; } @Override public String toString() { return "Field{" + "columnName=" + columnName + ", typeName=" + typeName + ", columnSize=" + columnSize + ", decimal_digits=" + decimal_digits + ", nullable=" + nullable + '}'; } } 3. 主要方法 public List<Table> export() { String showtablesSQL = "show tables"; List<Table> tableList = new ArrayList<Table>(); List<String> tableNameList = new ArrayList<String>(); Connection conn = HiveUtil.getConnection(); Statement stmt = null; ResultSet tableRs = null; // 存库元数据 ResultSet colRs = null;//存储表元数据 try { stmt = conn.createStatement(); stmt.executeQuery("use database"); //获取表名 tableRs = stmt.executeQuery(showtablesSQL); while (tableRs.next()) { String table = tableRs.getString(1); tableNameList.add(table); } //获取表结构 Field field = null; Table table = null; for (int i = 0; i < tableNameList.size(); i++) { String descTableSQL = "describe "; List<Field> fieldList = new ArrayList<Field>(); descTableSQL = descTableSQL + tableNameList.get(i).trim();//拼接sql colRs = stmt.executeQuery(descTableSQL); while (colRs.next()) { field = new Field(); field.setColumnName(colRs.getString(1)); field.setTypeName(colRs.getString(2));//测试大小 fieldList.add(field); } table = new Table(); table.setTableName(tableNameList.get(i).trim()); table.setField(fieldList); tableList.add(table); } } catch (SQLException ex) { Logger.getLogger(ExportOracleTable.class.getName()).log(Level.SEVERE, null, ex); } finally { if (colRs != null) { try { colRs.close(); } catch (SQLException ex) { Logger.getLogger(ExportOracleTable.class.getName()).log(Level.SEVERE, null, ex); } } if (tableRs != null) { try { tableRs.close(); } catch (SQLException ex) { Logger.getLogger(ExportOracleTable.class.getName()).log(Level.SEVERE, null, ex); } } if (conn != null) { try { conn.close(); } catch (SQLException ex) { Logger.getLogger(ExportOracleTable.class.getName()).log(Level.SEVERE, null, ex); } } } return tableList; } 4. 在新库中建表 public void createTableToHive(List<Table> list) { Connection conn = HiveUtil.getTargetConnection(); Statement stmt = null; try { for (Table table : list) { StringBuffer sql = new StringBuffer(); StringBuffer schemaSQL = new StringBuffer(); StringBuffer indexSQL = new StringBuffer(); sql.append("create table if not exists "); sql.append(table.getTableName());//create table test for (int i = 0; i < table.getField().size(); i++) { sql.append(" (" + table.getField().get(i).getColumnName() + " " + table.getField().get(i).getTypeName());//create table test (name varchar(2), age int) // if (table.getField().get(i).getColumnSize() == 0) {//条件需要测试 // sql.append("(" + table.getField().get(i).getColumnSize() + ")"); // } if (i == table.getField().size() - 1) { continue; } sql.append(","); } //执行 stmt = conn.createStatement(); stmt.executeQuery("use database"); stmt.executeQuery(sql.toString().trim()); // stmt.executeQuery(schemaSQL ); } conn.commit(); } catch (SQLException ex) { Logger.getLogger(CreateHiveTable.class.getName()).log(Level.SEVERE, null, ex); } finally { if(stmt != null) { try { stmt.close(); } catch (SQLException ex) { Logger.getLogger(CreateHiveTable.class.getName()).log(Level.SEVERE, null, ex); } } if(conn != null) { try { conn.close(); } catch (SQLException ex) { Logger.getLogger(CreateHiveTable.class.getName()).log(Level.SEVERE, null, ex); } } } }
试试其它关键字
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
贡献的其它代码
Label
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3