package org.bd.impala;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.bd.impala.utils.ImpaIaTableColumn;
import org.bd.impala.utils.ImpalaConnection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* <b>版权信息:</b> 2017,big data module<br/>
* <b>功能描述:</b> Impala的基本操作<br/>
* <b>版本历史:</b><br/>
* @author jasse | 2017年3月7日|创建
*/
@Deprecated
public class ImpalaCommonDao{
private static Logger logger = LoggerFactory.getLogger(ImpalaCommonDao.class);
private static ImpalaCommonDao commonDao = null;
private Connection conn = null;
private Statement stmt = null;
private ResultSet rs = null;
public ImpalaCommonDao(){
}
public static ImpalaCommonDao getInstance(){
try {
if(commonDao == null || commonDao.conn == null || commonDao.conn.isClosed()){
commonDao = new ImpalaCommonDao();
}
} catch (SQLException e) {
logger.error("实例ImpalaCommonDao异常",e.getMessage());
}
return commonDao;
}
/**获取Impala连接*/
public Connection getConn(){
try {
if(conn == null || conn.isClosed()){
conn = ImpalaConnection.getInstance().getConn();
}
} catch (SQLException e) {
logger.error("获取impala连接异常:{0}",e.getMessage());
}
return conn;
}
/** 关闭impala连接*/
public void close(){
try {rs.close();} catch (Exception e2) {
logger.error("ResultSet.close()异常:{0}",e2.getMessage());
}
try {stmt.close();} catch (Exception e2) {
logger.error("Statement.close()异常:{0}",e2.getMessage());
}
try {conn.close();} catch (Exception e2) {
logger.error("Connection.close()异常:{0}",e2.getMessage());
}
}
/**
* impala表文件格式的枚举
*/
public enum TabFileFormat {PARQUET,TEXTFILE,AVRO,SEQUENCEFILE,RCFILE};
/**
* 根据sql语句查询结果集合
* @param sql
* @return ResultSet
* @throws SQLException
*/
public ResultSet queryData(String sql) throws SQLException {
stmt = getConn().createStatement();
rs = stmt.executeQuery(sql);
return rs;
}
/**
* 执行DDL语句
* @param sql
* @return
* @throws SQLException
*/
public int executeSQL(CharSequence sql) throws SQLException {
stmt = getConn().createStatement();
return stmt.executeUpdate(sql.toString());
}
/**
* 根据表名统计全表数据量
* @param tableName
* @return
* @throws SQLException
*/
public int queryCount(String tableName) throws SQLException {
try {
String sql = "select count(*) from "+tableName;
rs = queryData(sql);
ResultSetMetaData metaData= rs.getMetaData();
while(rs.next()){
String colname = metaData.getColumnName(1);
int count = rs.getInt(colname);
return count;
}
} catch (Exception e) {
logger.error("根据表名{0}统计全表数据量,异常:{1}",tableName,e.getMessage());
throw new SQLException("根据表名"+tableName+"统计全表数据量异常!");
}finally{
close();
}
return 0;
}
/**
* 根据sql统计数据量
* @param sql 例如:select count(1) from tableA
* @return
* @throws SQLException
*/
public int queryCountBySql(String sql) throws SQLException {
try {
rs = queryData(sql);
ResultSetMetaData metaData= rs.getMetaData();
while(rs.next()){
String colname = metaData.getColumnName(1);
int count = rs.getInt(colname);
return count;
}
} catch (Exception e) {
logger.error("根据sql={0};统计全表数据量,异常:{1}",sql,e.getMessage());
throw new SQLException("根据sql="+sql+";统计数据量异常!");
}finally{
close();
}
return 0;
}
/**
* 检查表是否存在
* @param tableName tableA 或者 database.tableA
* @return
* @throws SQLException
*/
public boolean checkTable(String tableName) throws SQLException {
try {
if(StringUtils.isEmpty(tableName))
return false;
tableName = tableName.toLowerCase();//Impala默认表名及字段名小写
String sql = "show tables";
if(tableName.contains(".")){
String[] sp = tableName.split("\\.");
tableName = sp[1];
sql += " in " + sp[0];
}
sql += " like '" + tableName + "'";
rs = queryData(sql);
if(rs.next() && tableName.equals(rs.getString(1)))
return true;
} catch (Exception e) {
logger.error("根据表名={0},检查是否存在,异常:{1}",tableName,e.getMessage());
throw new SQLException("根据表名="+tableName+",检查是否存在异常!");
}finally{
close();
}
return false;
}
/**
* 判断Impala表字段是否存在
* @param tableName 表名
* @param column 列名
* @return
* @throws SQLException
*/
public boolean checkColumn(String tableName, String column) throws SQLException{
if(StringUtils.isEmpty(tableName) || StringUtils.isEmpty(column))
return false;
if(getFields(tableName).contains(column.toLowerCase().trim()))//Impala默认表名及字段名小写
return true;
return false;
}
/**
* 获取impala库名列表
* @return
* @throws SQLException
*/
public List<String> getDataBases() throws SQLException {
List<String> list = new ArrayList<String>();
String sql = "show databases";
try {
rs = queryData(sql);
while(rs.next()){
list.add(rs.getString(1));
}
} catch (SQLException e) {
logger.error("获取impala库名列表异常!");
throw new SQLException("获取impala库名列表异常!");
}
return list;
}
/**
* 获取Impala数据库下全部表名
* @param databaseName 库名称 如果查默认库的,则传null 或者"" 值
* @return
* @throws SQLException
*/
public List<String> getTables(String databaseName) throws SQLException {
List<String> list = new ArrayList<String>();
try {
String sql = "show tables";
if(StringUtils.isNotEmpty(databaseName) && StringUtils.isNotBlank(databaseName)){
sql += " in " + databaseName;
}
rs = queryData(sql);
while(rs.next()){
list.add(rs.getString(1));
}
} catch (Exception e) {
logger.error("根据库名={0}获取Impala数据库下全部表名异常:{1}",databaseName,e.getMessage());
throw new SQLException("根据库名="+databaseName+",获取Impala数据库下全部表名异常");
}finally{
close();
}
return list;
}
/**
* 查询该库下有多少张表
* @param databaseName 库名
* @return
* @throws SQLException
*/
public int getTableCountByDataBase(String databaseName) throws SQLException {
int count = 0;
try {
List<String> list = getTables(databaseName);
if (list != null && list.size() > 0) {
count = list.size();
}
} catch (SQLException e) {
logger.error("根据库名={0}查询该库下有多少张表异常:{1}",databaseName,e.getMessage());
throw new SQLException("根据库名="+databaseName+",查询该库下有多少张表异常");
}
return count;
}
/**
* 根据表名获取全部字段名
* @param tableName 表名 (例如:tableA 或者 database.tableA)
* @return
* @throws SQLException
*/
public List<String> getFields(String tableName) throws SQLException {
List<String> list = new ArrayList<String>();
try {
if(StringUtils.isEmpty(tableName))
return list;
rs = queryData("de
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论




























收起资源包目录





































































































共 252 条
- 1
- 2
- 3
资源评论


日刷百题
- 粉丝: 6837
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- 嵌入式系统复习题1.doc
- 沁阳市第一中学多媒体设备及计算机设备采购项目.doc
- 肯德基网络营销策划分析ppt课件.ppt
- 有答案的《工程项目管理》复习题.doc
- 石油总公司中下游工程建设项目管理规定教材.doc
- 某自动化股份公司IEC61850技术培训.pptx
- 云计算建设方案样本.doc
- 工程网络计划网络图.ppt
- 数学建模网络赛特等奖土地储备风险评估方案.doc
- 网络故障分析报告.pdf
- 李宁电子商务方案解读.ppt
- 网络时间协议简介.doc
- (源码)基于C++的Vive Lighthouse室内定位传感器系统.zip
- 两个开挂的Excel同步数据到Word技巧!(联动)get√.pdf
- 智慧城市建设带动实体经济发展.docx
- 三级网络第一章的重点(最新整理).pdf
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



安全验证
文档复制为VIP权益,开通VIP直接复制
