.../hadoop/hive/metastore/MetaStoreUtils.java | 86 ++++++++-------------- 1 file changed, 32 insertions(+), 54 deletions(-) diff --git a/metastore/src/java/org/apache/hadoop/hive/metastore/MetaStoreUtils.java b/metastore/src/java/org/apache/hadoop/hive/metastore/MetaStoreUtils.java index 1aaba4c..99b18f0 100644 --- a/metastore/src/java/org/apache/hadoop/hive/metastore/MetaStoreUtils.java +++ b/metastore/src/java/org/apache/hadoop/hive/metastore/MetaStoreUtils.java @@ -29,6 +29,7 @@ import java.net.URLClassLoader; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; @@ -43,6 +44,8 @@ import com.google.common.base.Predicates; import com.google.common.collect.Maps; +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.collections.ListUtils; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -129,7 +132,7 @@ public static Table createColumnsetSchema(String name, List columns, serdeInfo.getParameters().put(org.apache.hadoop.hive.serde.serdeConstants.SERIALIZATION_FORMAT, DEFAULT_SERIALIZATION_FORMAT); - List fields = new ArrayList(); + List fields = new ArrayList(columns.size()); sd.setCols(fields); for (String col : columns) { FieldSchema field = new FieldSchema(col, @@ -556,10 +559,7 @@ static public void deleteWHDirectory(Path path, Configuration conf, Map partSpec) { List pvals = new ArrayList(); for (FieldSchema field : partCols) { - String val = partSpec.get(field.getName()); - if (val == null) { - val = ""; - } + String val = StringUtils.defaultString(partSpec.get(field.getName())); pvals.add(val); } return pvals; @@ -590,10 +590,7 @@ static public boolean validateName(String name, Configuration conf) { } tpat = Pattern.compile("[" + allowedCharacters + "]+"); Matcher m = tpat.matcher(name); - if (m.matches()) { - return true; - } - return false; + return m.matches(); } /* @@ -637,18 +634,7 @@ static void throwExceptionIfIncompatibleColTypeChange( } static boolean areSameColumns(List oldCols, List newCols) { - if (oldCols.size() != newCols.size()) { - return false; - } else { - for (int i = 0; i < oldCols.size(); i++) { - FieldSchema oldCol = oldCols.get(i); - FieldSchema newCol = newCols.get(i); - if(!oldCol.equals(newCol)) { - return false; - } - } - } - return true; + return ListUtils.isEqualList(oldCols, newCols); } /* @@ -725,7 +711,7 @@ private static boolean isValidTypeChar(char c) { } public static String validateSkewedColNames(List cols) { - if (null == cols) { + if (CollectionUtils.isEmpty(cols)) { return null; } for (String col : cols) { @@ -738,10 +724,10 @@ public static String validateSkewedColNames(List cols) { public static String validateSkewedColNamesSubsetCol(List skewedColNames, List cols) { - if (null == skewedColNames) { + if (CollectionUtils.isEmpty(skewedColNames)) { return null; } - List colNames = new ArrayList(); + List colNames = new ArrayList(cols.size()); for (FieldSchema fieldSchema : cols) { colNames.add(fieldSchema.getName()); } @@ -905,7 +891,7 @@ public static String getDDLFromFieldSchema(String structName, } ddl.append("}"); - LOG.debug("DDL: " + ddl); + LOG.trace("DDL: {}", ddl); return ddl.toString(); } @@ -1021,7 +1007,7 @@ public static Properties getPartSchemaFromTableSchema( (key.equals(cols) || key.equals(colTypes) || key.equals(parts))) { continue; } - schema.put(key, (param.getValue() != null) ? param.getValue() : ""); + schema.put(key, (param.getValue() != null) ? param.getValue() : StringUtils.EMPTY); } if (sd.getSerdeInfo().getSerializationLib() != null) { @@ -1058,7 +1044,7 @@ public static Properties addCols(Properties schema, List cols) { } colNameBuf.append(col.getName()); colTypeBuf.append(col.getType()); - colComment.append((null != col.getComment()) ? col.getComment() : ""); + colComment.append((null != col.getComment()) ? col.getComment() : StringUtils.EMPTY); first = false; } schema.setProperty( @@ -1116,7 +1102,7 @@ public static Properties getSchemaWithoutCols(org.apache.hadoop.hive.metastore.a } if (sd.getSerdeInfo() != null) { for (Map.Entry param : sd.getSerdeInfo().getParameters().entrySet()) { - schema.put(param.getKey(), (param.getValue() != null) ? param.getValue() : ""); + schema.put(param.getKey(), (param.getValue() != null) ? param.getValue() : StringUtils.EMPTY); } if (sd.getSerdeInfo().getSerializationLib() != null) { @@ -1132,10 +1118,10 @@ public static Properties getSchemaWithoutCols(org.apache.hadoop.hive.metastore.a getDDLFromFieldSchema(tableName, sd.getCols())); } - String partString = ""; - String partStringSep = ""; - String partTypesString = ""; - String partTypesStringSep = ""; + String partString = StringUtils.EMPTY; + String partStringSep = StringUtils.EMPTY; + String partTypesString = StringUtils.EMPTY; + String partTypesStringSep = StringUtils.EMPTY; for (FieldSchema partKey : partitionKeys) { partString = partString.concat(partStringSep); partString = partString.concat(partKey.getName()); @@ -1325,7 +1311,7 @@ private static String getAllThreadStacksAsString() { for (Map.Entry entry : threadStacks.entrySet()) { Thread t = entry.getKey(); sb.append(System.lineSeparator()); - sb.append("Name: ").append(t.getName()).append(" State: " + t.getState()); + sb.append("Name: ").append(t.getName()).append(" State: ").append(t.getState()); addStackString(entry.getValue(), sb); } return sb.toString(); @@ -1510,11 +1496,7 @@ public static boolean isImmutableTable(Table table) { public static boolean isArchived( org.apache.hadoop.hive.metastore.api.Partition part) { Map params = part.getParameters(); - if ("true".equalsIgnoreCase(params.get(hive_metastoreConstants.IS_ARCHIVED))) { - return true; - } else { - return false; - } + return ("TRUE".equalsIgnoreCase(params.get(hive_metastoreConstants.IS_ARCHIVED))); } public static Path getOriginalLocation( @@ -1644,12 +1626,12 @@ public static boolean isView(Table table) { static List getMetaStoreListeners(Class clazz, HiveConf conf, String listenerImplList) throws MetaException { - List listeners = new ArrayList(); - listenerImplList = listenerImplList.trim(); - if (listenerImplList.equals("")) { - return listeners; + if (StringUtils.isBlank(listenerImplList)) { + return Collections.emptyList(); } + List listeners = new ArrayList(); + String[] listenerImpls = listenerImplList.split(","); for (String listenerImpl : listenerImpls) { try { @@ -1817,11 +1799,7 @@ public static int getArchivingLevel(Partition part) throws MetaException { = new com.google.common.base.Function() { @Override public java.lang.String apply(@Nullable java.lang.String string) { - if (string == null){ - return ""; - } else { - return string; - } + return StringUtils.defaultString(string); } }; @@ -1859,7 +1837,7 @@ public static int getArchivingLevel(Partition part) throws MetaException { private static URL urlFromPathString(String onestr) { URL oneurl = null; try { - if (StringUtils.indexOf(onestr, "file:/") == 0) { + if (onestr.startsWith("file:/")) { oneurl = new URL(onestr); } else { oneurl = new File(onestr).toURL(); @@ -1879,7 +1857,7 @@ private static URL urlFromPathString(String onestr) { public static ClassLoader addToClassPath(ClassLoader cloader, String[] newPaths) throws Exception { URLClassLoader loader = (URLClassLoader) cloader; List curPath = Arrays.asList(loader.getURLs()); - ArrayList newPath = new ArrayList(); + ArrayList newPath = new ArrayList(curPath.size()); // get a list with the current classpath components for (URL onePath : curPath) { @@ -1902,15 +1880,15 @@ public static String encodeTableName(String name) { // all the special characters with the corresponding number in ASCII. // Note that unicode is not supported in table names. And we have explicit // checks for it. - String ret = ""; + StringBuilder sb = new StringBuilder(); for (char ch : name.toCharArray()) { if (Character.isLetterOrDigit(ch) || ch == '_') { - ret += ch; + sb.append(ch); } else { - ret += "-" + (int) ch + "-"; + sb.append('-').append((int) ch).append('-'); } } - return ret; + return sb.toString(); } // this function will merge csOld into csNew. @@ -1970,7 +1948,7 @@ public static MetaException newMetaException(String errorMessage, Exception e) { } public static List getColumnNames(List schema) { - List cols = new ArrayList<>(); + List cols = new ArrayList<>(schema.size()); for (FieldSchema fs : schema) { cols.add(fs.getName()); }