List of usage examples for java.sql Connection rollback
void rollback() throws SQLException;
Connection
object. From source file:org.apache.hadoop.hive.metastore.MyXid.java
@Override public boolean grantRoleToUser(String userName, List<String> roles) throws NoSuchObjectException, InvalidObjectException, MetaException { Connection con = null; ;/* ww w . j a va 2 s.c om*/ PreparedStatement ps = null; boolean success = false; userName = userName.toLowerCase(); List<String> roleLowerCase = new ArrayList<String>(roles.size()); for (String role : roles) { roleLowerCase.add(role.toLowerCase()); } roles = roleLowerCase; try { con = getGlobalConnection(); } catch (MetaStoreConnectException e1) { LOG.error("grant role to user error , user=" + userName + ", msg=" + e1.getMessage()); throw new MetaException(e1.getMessage()); } catch (SQLException e1) { LOG.error("grant role to user error , user=" + userName + ", msg=" + e1.getMessage()); throw new MetaException(e1.getMessage()); } try { con.setAutoCommit(false); con.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ); ps = con.prepareStatement("select user_name from tdwuser where user_name=?"); ps.setString(1, userName); boolean isPrivFind = false; ResultSet userSet = ps.executeQuery(); while (userSet.next()) { isPrivFind = true; break; } userSet.close(); ps.close(); if (!isPrivFind) { throw new NoSuchObjectException("can not find user:" + userName); } ps = con.prepareStatement("select role_name from tdwuserrole where user_name=?"); ps.setString(1, userName); List<String> roleSet = new ArrayList<String>(); ResultSet roleRetSet = ps.executeQuery(); while (roleRetSet.next()) { roleSet.add(roleRetSet.getString(1)); } roleRetSet.close(); ps.close(); roles.removeAll(roleSet); if (!roles.isEmpty()) { ps = con.prepareStatement("insert into tdwuserrole(user_name, role_name) values(?,?)"); for (String role : roles) { ps.setString(1, userName); ps.setString(2, role); ps.addBatch(); } ps.executeBatch(); } con.commit(); success = true; } catch (SQLException ex) { LOG.error("grant auth sys error , user=" + userName + ", msg=" + ex.getMessage()); ex.printStackTrace(); throw new MetaException(ex.getMessage()); } finally { if (!success) { try { con.rollback(); } catch (SQLException e) { } } closeStatement(ps); closeConnection(con); } return success; }
From source file:org.apache.hadoop.hive.metastore.MyXid.java
@Override public List<FieldSchema> getPartFieldsJdbc(String dbName, String tableName) throws MetaException { Connection con = null; ;//from w ww . j a v a 2 s.c om Statement ps = null; boolean success = false; List<FieldSchema> columnInfo = new ArrayList<FieldSchema>(); dbName = dbName.toLowerCase(); tableName = tableName.toLowerCase(); try { con = getSegmentConnection(dbName); } catch (MetaStoreConnectException e1) { LOG.error( "get partition field error, db=" + dbName + ", tbl=" + tableName + ", msg=" + e1.getMessage()); throw new MetaException(e1.getMessage()); } catch (SQLException e1) { LOG.error( "get partition field error, db=" + dbName + ", tbl=" + tableName + ", msg=" + e1.getMessage()); throw new MetaException(e1.getMessage()); } try { con.setAutoCommit(false); con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); ps = con.createStatement(); String sql = "select tbl_id, pri_part_key, sub_part_key from tbls where db_name='" + dbName + "' " + " and tbl_name='" + tableName + "'"; ResultSet tblSet = ps.executeQuery(sql); String priPartKey = null; String subPartKey = null; long tblID = 0; boolean isTblFind = false; while (tblSet.next()) { isTblFind = true; tblID = tblSet.getLong(1); priPartKey = tblSet.getString(2); subPartKey = tblSet.getString(3); } tblSet.close(); if (isTblFind) { sql = "select column_name, type_name, comment, column_len from " + " columns where tbl_id=" + tblID + " and ( column_name='" + priPartKey + "' or column_name='" + subPartKey + "')"; ResultSet colSet = ps.executeQuery(sql); while (colSet.next()) { FieldSchema field = new FieldSchema(); field.setName(colSet.getString(1)); field.setType(colSet.getString(2)); field.setComment(colSet.getString(3)); columnInfo.add(field); } } con.commit(); success = true; } catch (SQLException sqlex) { LOG.error("get partition field error, db=" + dbName + ", tbl=" + tableName + ", msg=" + sqlex.getMessage()); sqlex.printStackTrace(); throw new MetaException(sqlex.getMessage()); } finally { if (!success) { try { con.rollback(); } catch (SQLException e) { } } closeStatement(ps); closeConnection(con); } return columnInfo; }
From source file:org.apache.hadoop.hive.metastore.MyXid.java
@Override public boolean grantRoleToRole(String roleName, List<String> roles) throws NoSuchObjectException, InvalidObjectException, MetaException { Connection con = null; ;//from w w w .ja va2s. c om Statement ps = null; boolean success = false; roleName = roleName.toLowerCase(); List<String> roleLowerCase = new ArrayList<String>(roles.size()); for (String role : roles) { roleLowerCase.add(role.toLowerCase()); } try { con = getGlobalConnection(); } catch (MetaStoreConnectException e1) { LOG.error("grant role error, role=" + roleName + ", msg=" + e1.getMessage()); throw new MetaException(e1.getMessage()); } catch (SQLException e1) { LOG.error("grant role error, role=" + roleName + ", msg=" + e1.getMessage()); throw new MetaException(e1.getMessage()); } try { con.setAutoCommit(false); con.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ); ps = con.createStatement(); String sql = "select role_name from tdwrole where role_name='" + roleName.toLowerCase() + "'"; boolean isRoleFind = false; ResultSet roleSet = ps.executeQuery(sql); while (roleSet.next()) { isRoleFind = true; break; } roleSet.close(); if (!isRoleFind) { throw new NoSuchObjectException("can not find role:" + roleName); } Set<String> sonRoleNameSet = new HashSet<String>(); sql = "select sonrole_name from tdwsonrole where role_name='" + roleName.toLowerCase() + "'"; ResultSet sonroleSet = ps.executeQuery(sql); while (sonroleSet.next()) { sonRoleNameSet.add(sonroleSet.getString(1)); } sonroleSet.close(); List<String> needAddRoles = new ArrayList<String>(); for (String role : roles) { if (!roleName.equalsIgnoreCase(role) && !sonRoleNameSet.contains(role)) { needAddRoles.add(role); } } if (!needAddRoles.isEmpty()) { for (String role : needAddRoles) { ps.addBatch("insert into tdwsonrole(role_name, sonrole_name) values('" + roleName.toLowerCase() + "', '" + role.toLowerCase() + "')"); } ps.executeBatch(); } con.commit(); success = true; } catch (SQLException sqlex) { sqlex.printStackTrace(); LOG.error("grant role error, role=" + roleName + ", msg=" + sqlex.getMessage()); throw new MetaException(sqlex.getMessage()); } finally { if (!success) { try { con.rollback(); } catch (SQLException e) { } } closeStatement(ps); closeConnection(con); } return success; }
From source file:org.apache.hadoop.hive.metastore.MyXid.java
@Override public int grantUserGroup(String groupName, String namelist, String user) throws MetaException { Connection con = null; Statement ps = null;/*from w ww . jav a 2s . c o m*/ boolean success = false; groupName = groupName.toLowerCase(); namelist = namelist.toLowerCase(); user = user.toLowerCase(); try { con = getGlobalConnection(); } catch (MetaStoreConnectException e1) { LOG.error("grant user group error, groupName=" + groupName + ", namelist=" + namelist + ", user=" + user + ", msg=" + e1.getMessage()); throw new MetaException(e1.getMessage()); } catch (SQLException e1) { LOG.error("grant user group error, groupName=" + groupName + ", namelist=" + namelist + ", user=" + user + ", msg=" + e1.getMessage()); throw new MetaException(e1.getMessage()); } try { con.setAutoCommit(false); con.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ); ps = con.createStatement(); String sql = "select group_name from tdwuser where user_name='" + namelist + "'"; boolean isUserFind = false; String oldGroupName = null; ResultSet groupNameSet = ps.executeQuery(sql); while (groupNameSet.next()) { isUserFind = true; oldGroupName = groupNameSet.getString(1); break; } groupNameSet.close(); if (!isUserFind) { LOG.error("Can not find user group:" + groupName); return 1; } if (groupName.equalsIgnoreCase(oldGroupName)) { LOG.error("grant user group error, groupName=" + groupName + ", namelist=" + namelist + ", user=" + user + " group name is same to old group name"); return 2; } sql = "select creator from usergroup where group_name='" + groupName + "'"; boolean isNewGroupFind = false; String newGroupCreator = null; ResultSet groupCreatorSet = ps.executeQuery(sql); while (groupCreatorSet.next()) { isNewGroupFind = true; newGroupCreator = groupCreatorSet.getString(1); break; } groupCreatorSet.close(); if (!isNewGroupFind) { LOG.error("revoke user group error, groupName=" + groupName + ", namelist=" + namelist + ", user=" + user); return 4; } if (!newGroupCreator.equalsIgnoreCase(user) && !user.equalsIgnoreCase("root")) { LOG.error("revoke user group error, groupName=" + groupName + ", namelist=" + namelist + ", user=" + user); return 5; } sql = "update tdwuser set group_name='" + groupName.toLowerCase() + "' where user_name='" + namelist.toLowerCase() + "'"; ps.executeUpdate(sql); con.commit(); success = true; } catch (SQLException sqlex) { LOG.error("grant user group error, groupName=" + groupName + ", namelist=" + namelist + ", user=" + user + ", msg=" + sqlex.getMessage()); sqlex.printStackTrace(); throw new MetaException(sqlex.getMessage()); } finally { if (!success) { try { con.rollback(); } catch (SQLException e) { } } closeStatement(ps); closeConnection(con); } if (success) { return 0; } else { return 6; } }
From source file:org.apache.hadoop.hive.metastore.MyXid.java
@Override public int revokeUserGroup(String groupName, String namelist, String user) throws MetaException { Connection con = null; Statement ps = null;//from w w w. j a va 2 s . com boolean success = false; groupName = groupName.toLowerCase(); namelist = namelist.toLowerCase(); user = user.toLowerCase(); try { con = getGlobalConnection(); } catch (MetaStoreConnectException e1) { LOG.error("revoke user group error, groupName=" + groupName + ", namelist=" + namelist + ", user=" + user + ", msg=" + e1.getMessage()); throw new MetaException(e1.getMessage()); } catch (SQLException e1) { LOG.error("revoke user group error, groupName=" + groupName + ", namelist=" + namelist + ", user=" + user + ", msg=" + e1.getMessage()); throw new MetaException(e1.getMessage()); } try { con.setAutoCommit(false); con.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ); ps = con.createStatement(); String sql = "select group_name from tdwuser where user_name='" + namelist + "'"; boolean isUserFind = false; String oldGroupName = null; ResultSet groupNameSet = ps.executeQuery(sql); while (groupNameSet.next()) { isUserFind = true; oldGroupName = groupNameSet.getString(1); break; } groupNameSet.close(); if (!isUserFind) { LOG.error("revoke user group error, groupName=" + groupName + ", namelist=" + namelist + ", user=" + user); return 1; } if (!groupName.equalsIgnoreCase(oldGroupName)) { LOG.error("revoke user group error, groupName=" + groupName + ", namelist=" + namelist + ", user=" + user); return 2; } sql = "select creator from usergroup where group_name='" + groupName + "'"; boolean isNewGroupFind = false; String newGroupCreator = null; ResultSet groupCreatorSet = ps.executeQuery(sql); while (groupCreatorSet.next()) { isNewGroupFind = true; newGroupCreator = groupCreatorSet.getString(1); break; } groupCreatorSet.close(); if (!isNewGroupFind) { LOG.error("revoke user group error, groupName=" + groupName + ", namelist=" + namelist + ", user=" + user); return 3; } if (!newGroupCreator.equalsIgnoreCase(user) && !user.equalsIgnoreCase("root")) { LOG.error("revoke user group error, groupName=" + groupName + ", namelist=" + namelist + ", user=" + user); return 5; } sql = "update tdwuser set group_name='" + HiveMetaStore.DEFAULT + "' where user_name='" + namelist.toLowerCase() + "'"; ps.executeUpdate(sql); con.commit(); success = true; } catch (SQLException sqlex) { LOG.error("revoke user group error, groupName=" + groupName + ", namelist=" + namelist + ", user=" + user + ", msg=" + sqlex.getMessage()); sqlex.printStackTrace(); throw new MetaException(sqlex.getMessage()); } finally { if (!success) { try { con.rollback(); } catch (SQLException e) { } } closeStatement(ps); closeConnection(con); } if (success) { return 0; } else { return 6; } }
From source file:org.apache.hadoop.hive.metastore.MyXid.java
@Override public boolean updatePBInfo(String dbName, String tableName, String modifiedTime) throws InvalidOperationException, MetaException { Connection con = null; PreparedStatement ps = null;/*w w w . j av a2 s. com*/ boolean success = false; dbName = dbName.toLowerCase(); tableName = tableName.toLowerCase(); String jarName = "./auxlib/" + dbName + "_" + tableName + "_" + modifiedTime + ".jar"; String className = dbName + "_" + tableName + "_" + modifiedTime; try { con = getSegmentConnection(dbName); } catch (MetaStoreConnectException e1) { LOG.error("updatePBInfo, db=" + dbName + ", table=" + tableName + ", msg=" + e1.getMessage()); throw new MetaException(e1.getMessage()); } catch (SQLException e1) { LOG.error("replace column error, db=" + dbName + ", table=" + tableName + ", msg=" + e1.getMessage()); throw new MetaException(e1.getMessage()); } try { con.setAutoCommit(false); con.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ); ps = con.prepareStatement("select tbl_id, serde_lib" + " from tbls where db_name=? and tbl_name=?"); ps.setString(1, dbName); ps.setString(2, tableName); String serdeLib = null; boolean isTblFind = false; long tblID = 0; ResultSet tblSet = ps.executeQuery(); while (tblSet.next()) { isTblFind = true; tblID = tblSet.getLong(1); serdeLib = tblSet.getString(2); } tblSet.close(); ps.close(); if (!isTblFind) { throw new MetaException("can not find table " + dbName + ":" + tableName); } if (!serdeLib.equals(ProtobufSerDe.class.getName())) { throw new MetaException("sorry, can only update jar info for a pb table "); } Map<String, String> tblParamMap = new HashMap<String, String>(); ps = con.prepareStatement( "select param_key, param_value from table_params where tbl_id=? and param_type='TBL'"); ps.setLong(1, tblID); ResultSet paramSet = ps.executeQuery(); while (paramSet.next()) { tblParamMap.put(paramSet.getString(1), paramSet.getString(2)); } paramSet.close(); ps.close(); boolean containJar = false; boolean containClass = false; if (tblParamMap.containsKey("pb.jar")) containJar = true; if (tblParamMap.containsKey("pb.outer.class.name")) containClass = true; if (containJar && containClass) { ps = con.prepareStatement( "update table_params set param_value=? where tbl_id=? and param_type='TBL' and param_key=?"); ps.setString(1, jarName); ps.setLong(2, tblID); ps.setString(3, "pb.jar"); ps.addBatch(); ps.setString(1, className); ps.setLong(2, tblID); ps.setString(3, "pb.outer.class.name"); ps.addBatch(); ps.executeBatch(); ps.close(); } con.commit(); success = true; } catch (SQLException ex) { ex.printStackTrace(); LOG.error("updatePBInfo, db=" + dbName + ", tbl=" + tableName + ", msg=" + ex.getMessage()); throw new MetaException(ex.getMessage()); } finally { if (!success) { try { con.rollback(); } catch (SQLException e) { } } closeStatement(ps); closeConnection(con); } return true; }
From source file:org.apache.hadoop.hive.metastore.MyXid.java
@Override public List<String> getPartitionNames(String dbName, String tableName, int level) throws MetaException { boolean success = false; Connection con = null; Statement ps = null;/* w ww . j av a 2 s.c om*/ Partition part = null; dbName = dbName.toLowerCase(); tableName = tableName.toLowerCase(); List<String> ret = new ArrayList<String>(); Map<String, List<String>> partNameMap = new LinkedHashMap<String, List<String>>(); try { con = getSegmentConnection(dbName); } catch (MetaStoreConnectException e1) { LOG.error("get partition name error, db=" + dbName + ", tbl=" + tableName + ", level=" + level + ", msg=" + e1.getMessage()); throw new MetaException(e1.getMessage()); } catch (SQLException e1) { LOG.error("get partition name error, db=" + dbName + ", tbl=" + tableName + ", level=" + level + ", msg=" + e1.getMessage()); throw new MetaException(e1.getMessage()); } try { con.setAutoCommit(false); con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); ps = con.createStatement(); long tblID = 0; boolean isTblFind = false; String priPartType = null; String subPartType = null; boolean hasPriPart = false; boolean hasSubPart = false; String sql = "SELECT tbl_id, pri_part_type, sub_part_type from TBLS where db_name='" + dbName + "' and tbl_name='" + tableName + "'"; ResultSet tblSet = ps.executeQuery(sql); while (tblSet.next()) { isTblFind = true; tblID = tblSet.getLong(1); priPartType = tblSet.getString(2); subPartType = tblSet.getString(3); if (priPartType != null && !priPartType.isEmpty()) { hasPriPart = true; } if (subPartType != null && !subPartType.isEmpty()) { hasSubPart = true; } if (hasPriPart && level == 0) { part = new Partition(); part.setParType(tblSet.getString(4)); break; } if (hasSubPart && level == 1) { part = new Partition(); part.setParType(tblSet.getString(5)); break; } throw new MetaException( "can not find partition of level " + level + " for table " + dbName + ":" + tableName); } tblSet.close(); if (!isTblFind) { throw new MetaException("can not find table " + dbName + ":" + tableName); } sql = "select part_name from PARTITIONS where tbl_id=" + tblID + " and level=" + level; ResultSet partSet = ps.executeQuery(sql); while (partSet.next()) { String partName = partSet.getString(1); ret.add(partName); } part.setParSpaces(partNameMap); part.setDbName(dbName); part.setTableName(tableName); part.setLevel(level); con.commit(); success = true; } catch (SQLException sqlex) { LOG.error("get partition error, db=" + dbName + ", tbl=" + tableName + ", level=" + level + ", msg=" + sqlex.getMessage()); sqlex.printStackTrace(); throw new MetaException(sqlex.getMessage()); } finally { if (!success) { try { con.rollback(); } catch (SQLException e) { } } closeStatement(ps); closeConnection(con); } if (success) return ret; else return null; }
From source file:org.apache.hadoop.hive.metastore.MyXid.java
@Override public boolean dropTable(String dbName, String tableName, boolean deleteData) throws MetaException, NoSuchObjectException { Connection con = null; Statement ps = null;//from ww w. j a v a2s .c om Warehouse wh = new Warehouse(hiveConf); boolean success = false; String tblLocation = null; String tblType = null; String tblFormat = null; String host = null; String port = null; String db = null; String user = null; String pass = null; dbName = dbName.toLowerCase(); tableName = tableName.toLowerCase(); try { con = getSegmentConnection(dbName); } catch (MetaStoreConnectException e1) { LOG.error("drop table error, db=" + dbName + ", tbl=" + tableName + ", msg=" + e1.getMessage()); return true; } catch (SQLException e1) { LOG.error("drop table error, db=" + dbName + ", tbl=" + tableName + ", msg=" + e1.getMessage()); return true; } try { con.setAutoCommit(false); con.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ); ps = con.createStatement(); String sql = "select tbl_id, tbl_type, tbl_format, tbl_location from tbls where db_name='" + dbName + "' and tbl_name='" + tableName + "'"; long tblID = 0; boolean isTblFind = false; ResultSet tblSet = ps.executeQuery(sql); while (tblSet.next()) { isTblFind = true; tblID = tblSet.getLong(1); tblType = tblSet.getString(2); tblFormat = tblSet.getString(3); tblLocation = tblSet.getString(4); break; } tblSet.close(); if (!isTblFind) { con.commit(); success = true; return success; } if (tblFormat != null && tblFormat.equalsIgnoreCase("pgdata")) { sql = "select param_key, param_value from table_params where tbl_id=" + tblID + " and param_type='SERDE'"; ResultSet pSet = ps.executeQuery(sql); Map<String, String> pMap = new HashMap<String, String>(); while (pSet.next()) { pMap.put(pSet.getString(1), pSet.getString(2)); } pSet.close(); host = pMap.get("ip"); port = pMap.get("port"); db = pMap.get("db_name"); user = pMap.get("user_name"); pass = pMap.get("pwd"); } sql = "delete from tbls where tbl_id=" + tblID; ps.executeUpdate(sql); boolean deleteSuccess = false; if (deleteData && (tblLocation != null) && !tblType.equalsIgnoreCase("EXTERNAL_TABLE")) { Path tblPath = new Path(tblLocation); deleteSuccess = wh.deleteDir(tblPath, true); HiveConf hconf = (HiveConf) hiveConf; if ((!deleteSuccess) && hconf.getBoolVar(HiveConf.ConfVars.HIVE_DELETE_DIR_ERROR_THROW)) { throw new MetaException("can not delete hdfs path, drop table failed!"); } } con.commit(); success = true; } catch (Exception sqlex) { sqlex.printStackTrace(); LOG.error("drop table error, db=" + dbName + ", tbl=" + tableName + ", msg=" + sqlex.getMessage()); throw new MetaException(sqlex.getMessage()); } finally { if (!success) { try { con.rollback(); } catch (SQLException e) { } } //else { // if (deleteData && (tblLocation != null) // && !tblType.equalsIgnoreCase("EXTERNAL_TABLE")) { // Path tblPath = new Path(tblLocation); // wh.deleteDir(tblPath, true); // } //} closeStatement(ps); closeConnection(con); } if (success && tblFormat != null && tblFormat.equalsIgnoreCase("pgdata") && host != null && port != null && db != null && user != null && pass != null) { Connection conn = null; try { String sql = "drop table " + tableName; String url = "jdbc:postgresql://" + host + ":" + port + "/" + db; conn = DriverManager.getConnection(url, user, pass); conn.setAutoCommit(false); Statement s = conn.createStatement(); s.execute(sql); conn.commit(); } catch (Exception e) { success = false; throw new MetaException(e.getMessage()); } finally { closeConnection(conn); } } success = false; try { con = getGlobalConnection(); } catch (MetaStoreConnectException e1) { LOG.error("drop table error, db=" + dbName + ", tbl=" + tableName + ", msg=" + e1.getMessage()); throw new MetaException(e1.getMessage()); } catch (SQLException e1) { LOG.error("drop table error, db=" + dbName + ", tbl=" + tableName + ", msg=" + e1.getMessage()); throw new MetaException(e1.getMessage()); } try { con.setAutoCommit(false); con.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ); ps = con.createStatement(); String sql = "delete from tblpriv where db_name='" + dbName.toLowerCase() + "' and tbl_name='" + tableName.toLowerCase() + "'"; ps.executeUpdate(sql); try { sql = "delete from tblsensitivity where db_name='" + dbName.toLowerCase() + "' and tbl_name='" + tableName.toLowerCase() + "'"; ps.executeUpdate(sql); } catch (Exception x) { } con.commit(); success = true; } catch (SQLException x) { LOG.error("drop table error, db=" + dbName + ", tbl=" + tableName + ", msg=" + x.getMessage()); throw new MetaException(x.getMessage()); } finally { if (!success) { try { con.rollback(); } catch (SQLException e) { } } closeStatement(ps); closeConnection(con); } return success; }
From source file:org.apache.hadoop.hive.metastore.MyXid.java
@Override public void addTblProps(String dbName, String tblName, String modifyUser, Map<String, String> props) throws InvalidOperationException, MetaException { Connection con; PreparedStatement ps = null;//from w ww .j a va 2s . co m boolean success = false; dbName = dbName.toLowerCase(); tblName = tblName.toLowerCase(); try { con = getSegmentConnection(dbName); } catch (MetaStoreConnectException e1) { LOG.error("add table props error, db=" + dbName + ", tbl=" + tblName + ", msg=" + e1.getMessage()); throw new MetaException(e1.getMessage()); } catch (SQLException e1) { LOG.error("add table props error, db=" + dbName + ", tbl=" + tblName + ", msg=" + e1.getMessage()); throw new MetaException(e1.getMessage()); } try { con.setAutoCommit(false); con.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ); ps = con.prepareStatement("select tbls.tbl_id from tbls where " + "tbls.db_name=? and tbls.tbl_name=?"); ps.setString(1, dbName); ps.setString(2, tblName); boolean isTblFind = false; long tblID = 0; ResultSet tblSet = ps.executeQuery(); while (tblSet.next()) { isTblFind = true; tblID = tblSet.getLong(1); } tblSet.close(); ps.close(); if (!isTblFind) { throw new MetaException("can not find table " + dbName + ":" + tblName); } ps = con.prepareStatement( "select param_key, param_value from table_params where tbl_id=? and param_type='TBL'"); ps.setLong(1, tblID); ResultSet paramSet = ps.executeQuery(); Map<String, String> oldParamMap = new HashMap<String, String>(); while (paramSet.next()) { oldParamMap.put(paramSet.getString(1), paramSet.getString(2)); } paramSet.close(); ps.close(); Map<String, String> needUpdateMap = new HashMap<String, String>(); Map<String, String> needAddMap = new HashMap<String, String>(); for (Entry<String, String> entry : props.entrySet()) { if (oldParamMap.containsKey(entry.getKey())) { needUpdateMap.put(entry.getKey(), entry.getValue()); } else { needAddMap.put(entry.getKey(), entry.getValue()); } } if (oldParamMap.containsKey("last_modified_time")) { needUpdateMap.put("last_modified_time", String.valueOf(System.currentTimeMillis() / 1000)); } else { needAddMap.put("last_modified_time", String.valueOf(System.currentTimeMillis() / 1000)); } if (oldParamMap.containsKey("last_modified_by")) { needUpdateMap.put("last_modified_by", modifyUser); } else { needAddMap.put("last_modified_by", modifyUser); } if (!needUpdateMap.isEmpty()) { ps = con.prepareStatement("update table_params set param_value=? where " + " tbl_id=? and param_type='TBL' and param_key=?"); for (Entry<String, String> entry : needUpdateMap.entrySet()) { ps.setString(1, entry.getValue()); ps.setLong(2, tblID); ps.setString(3, entry.getKey()); ps.addBatch(); } ps.executeBatch(); ps.close(); } if (!needAddMap.isEmpty()) { ps = con.prepareStatement("insert into table_params(tbl_id, param_type, " + "param_key, param_value) values(?,?,?,?)"); for (Map.Entry<String, String> entry : needAddMap.entrySet()) { ps.setLong(1, tblID); ps.setString(2, "TBL"); ps.setString(3, entry.getKey()); ps.setString(4, entry.getValue()); ps.addBatch(); } ps.executeBatch(); ps.close(); } con.commit(); success = true; } catch (SQLException ex) { LOG.error("add table props error, db=" + dbName + ", tbl=" + tblName + ", msg=" + ex.getMessage()); throw new MetaException(ex.getMessage()); } finally { if (!success) { try { con.rollback(); } catch (SQLException e) { } } closeStatement(ps); closeConnection(con); } return; }
From source file:org.apache.hadoop.hive.metastore.MyXid.java
@Override public List<List<String>> getPartitionNames(String dbName, String tableName) throws MetaException { boolean success = false; Connection con = null; Statement ps = null;/*from w w w.j a va2 s.co m*/ List<List<String>> ret = new ArrayList<List<String>>(); dbName = dbName.toLowerCase(); tableName = tableName.toLowerCase(); List<String> priPartNames = new ArrayList<String>(); List<String> subPartNames = new ArrayList<String>(); try { con = getSegmentConnection(dbName); } catch (MetaStoreConnectException e1) { LOG.error( "get partition names error, db=" + dbName + ", tbl=" + tableName + ", msg=" + e1.getMessage()); throw new MetaException(e1.getMessage()); } catch (SQLException e1) { LOG.error( "get partition names error, db=" + dbName + ", tbl=" + tableName + ", msg=" + e1.getMessage()); throw new MetaException(e1.getMessage()); } try { con.setAutoCommit(false); con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); ps = con.createStatement(); long tblID = 0; boolean isTblFind = false; String priPartType = null; String subPartType = null; boolean hasPriPart = false; boolean hasSubPart = false; String sql = "SELECT tbl_id, pri_part_type, sub_part_type from TBLS where db_name='" + dbName + "' and tbl_name='" + tableName + "'"; ResultSet tblSet = ps.executeQuery(sql); while (tblSet.next()) { isTblFind = true; tblID = tblSet.getLong(1); priPartType = tblSet.getString(2); subPartType = tblSet.getString(3); if (priPartType != null && !priPartType.isEmpty()) { hasPriPart = true; } if (subPartType != null && !subPartType.isEmpty()) { hasSubPart = true; } if (!hasPriPart) { throw new MetaException("get partition names error, db=" + dbName + ", tbl=" + tableName + ", msg=table is not a partition table"); } } tblSet.close(); if (!isTblFind) { LOG.error("get partition names error, db=" + dbName + ", tbl=" + tableName); throw new MetaException("can not find table " + dbName + ":" + tableName); } sql = "select part_name, level from PARTITIONS where tbl_id=" + tblID; ResultSet partSet = ps.executeQuery(sql); while (partSet.next()) { String partName = partSet.getString(1); int level = partSet.getInt(2); if (level == 0) { priPartNames.add(partName); } else if (level == 1) { subPartNames.add(partName); } } if (hasPriPart) { if (priPartType.equalsIgnoreCase("hash")) { int numOfHashPar = hiveConf.getInt("hive.hashPartition.num", 500); ret.add(new ArrayList()); ret.get(0).add("hash(" + numOfHashPar + ")"); } else { if (priPartNames.contains("default")) { priPartNames.remove("default"); priPartNames.add("default"); } ret.add(priPartNames); } } if (hasSubPart) { if (subPartType.equalsIgnoreCase("hash")) { int numOfHashPar = hiveConf.getInt("hive.hashPartition.num", 500); ret.add(new ArrayList()); ret.get(1).add("hash(" + numOfHashPar + ")"); } else { if (subPartNames.contains("default")) { subPartNames.remove("default"); subPartNames.add("default"); } ret.add(subPartNames); } } else { ret.add(subPartNames); } con.commit(); success = true; } catch (SQLException sqlex) { LOG.error("get partition names error, db=" + dbName + ", tbl=" + tableName + ", msg=" + sqlex.getMessage()); sqlex.printStackTrace(); throw new MetaException(sqlex.getMessage()); } finally { if (!success) { try { con.rollback(); } catch (SQLException e) { } } closeStatement(ps); closeConnection(con); } if (success) return ret; else return null; }