You are viewing a plain text version of this content. The canonical link for it is here.
Posted to gitbox@hive.apache.org by GitBox <gi...@apache.org> on 2020/12/16 07:54:11 UTC

[GitHub] [hive] pkumarsinha commented on a change in pull request #1768: HIVE-24526: Get grouped locations of external table data using metatool.

pkumarsinha commented on a change in pull request #1768:
URL: https://github.com/apache/hive/pull/1768#discussion_r544076223



##########
File path: standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/tools/metatool/MetaToolTaskListExtTblLocs.java
##########
@@ -0,0 +1,439 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.hive.metastore.tools.metatool;
+
+import com.google.common.annotations.VisibleForTesting;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.fs.Path;
+import org.apache.hadoop.hive.metastore.ObjectStore;
+import org.apache.hadoop.hive.metastore.TableType;
+import org.apache.hadoop.hive.metastore.Warehouse;
+import org.apache.hadoop.hive.metastore.api.Table;
+import org.apache.hadoop.hive.metastore.utils.MetaStoreUtils;
+import org.apache.thrift.TException;
+import org.codehaus.jettison.json.JSONException;
+import org.codehaus.jettison.json.JSONArray;
+import org.codehaus.jettison.json.JSONObject;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.util.*;

Review comment:
       Remove wild card import.

##########
File path: standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/tools/metatool/MetaToolTaskListExtTblLocs.java
##########
@@ -0,0 +1,439 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.hive.metastore.tools.metatool;
+
+import com.google.common.annotations.VisibleForTesting;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.fs.Path;
+import org.apache.hadoop.hive.metastore.ObjectStore;
+import org.apache.hadoop.hive.metastore.TableType;
+import org.apache.hadoop.hive.metastore.Warehouse;
+import org.apache.hadoop.hive.metastore.api.Table;
+import org.apache.hadoop.hive.metastore.utils.MetaStoreUtils;
+import org.apache.thrift.TException;
+import org.codehaus.jettison.json.JSONException;
+import org.codehaus.jettison.json.JSONArray;
+import org.codehaus.jettison.json.JSONObject;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.util.*;
+
+public class MetaToolTaskListExtTblLocs extends MetaToolTask {
+
+  private static final Logger LOG = LoggerFactory.getLogger(MetaToolTaskListExtTblLocs.class);
+  private final HashMap<String, HashSet<String>> coverageList = new HashMap<>();

Review comment:
       Replace reference type with Map.

##########
File path: standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/tools/metatool/MetaToolTaskListExtTblLocs.java
##########
@@ -0,0 +1,439 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.hive.metastore.tools.metatool;
+
+import com.google.common.annotations.VisibleForTesting;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.fs.Path;
+import org.apache.hadoop.hive.metastore.ObjectStore;
+import org.apache.hadoop.hive.metastore.TableType;
+import org.apache.hadoop.hive.metastore.Warehouse;
+import org.apache.hadoop.hive.metastore.api.Table;
+import org.apache.hadoop.hive.metastore.utils.MetaStoreUtils;
+import org.apache.thrift.TException;
+import org.codehaus.jettison.json.JSONException;
+import org.codehaus.jettison.json.JSONArray;
+import org.codehaus.jettison.json.JSONObject;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.util.*;
+
+public class MetaToolTaskListExtTblLocs extends MetaToolTask {
+
+  private static final Logger LOG = LoggerFactory.getLogger(MetaToolTaskListExtTblLocs.class);
+  private final HashMap<String, HashSet<String>> coverageList = new HashMap<>();
+  private final HashMap<String, DataLocation> inputLocations = new HashMap<>();
+
+  @Override
+  void execute() {
+    String[] loc = getCl().getListExtTblLocsParams();
+    try{
+      generateExternalTableInfo(loc[0], loc[1]);
+    } catch (IOException | TException | JSONException e) {
+      LOG.error("Listing external table locations failed: ", e);
+    }
+  }
+
+  private void generateExternalTableInfo(String dbPattern, String outputDir) throws TException, IOException,
+          JSONException {
+    ObjectStore objectStore = getObjectStore();
+    Configuration conf = msConf != null ? msConf : objectStore.getConf();
+    String defaultCatalog = MetaStoreUtils.getDefaultCatalog(conf);
+    List<String> databases = objectStore.getDatabases(defaultCatalog, dbPattern);
+    System.out.println("Number of databases found for given pattern: " + databases.size());
+    TreeSet<String> locations = new TreeSet<>();
+    for (String db : databases) {
+      List<String> tables = objectStore.getAllTables(defaultCatalog, db);
+      for(String tblName : tables) {
+        Table t = objectStore.getTable(defaultCatalog, db, tblName);
+        if(TableType.EXTERNAL_TABLE.name().equalsIgnoreCase(t.getTableType())) {
+          String tblLocation = t.getSd().getLocation();
+          DataLocation dataLocation = new DataLocation(db, tblName, 0, 0,
+                  null);
+          inputLocations.put(tblLocation, dataLocation);
+          if (!isCovered(locations, new Path(tblLocation))) {
+            locations.add(tblLocation);
+          }
+          //retrieving partition locations outside table-location
+          Map<String, String> partitionLocations = objectStore.getPartitionLocations(defaultCatalog, db, tblName,
+                  tblLocation, -1);
+          dataLocation.setTotalPartitions(partitionLocations.size());
+          for (String partitionName : partitionLocations.keySet()) {
+            String partLocation = partitionLocations.get(partitionName);
+            //null value means partition is in table location, we do not add it to input in this case.
+            if(partLocation == null) {
+              dataLocation.incrementNumPartsInTblLoc();
+            }
+            else {
+              partLocation = partLocation + Path.SEPARATOR + 
+                      Warehouse.makePartName(Warehouse.makeSpecFromName(partitionName), false);
+              inputLocations.put(partLocation, new DataLocation(db, tblName, 0,
+                      0, partitionName));
+              if(!isCovered(locations, new Path(partLocation))) {
+                locations.add(partLocation);
+              }
+            }
+          }
+        }
+      }
+    }
+    if(!locations.isEmpty()) {
+      removeNestedStructure(locations);
+      createOutputList(locations, outputDir, dbPattern);
+    }
+    else {
+      System.out.println("No external tables found to process.");
+    }
+  }
+
+  private boolean isPathWithinSubtree(Path path, Path subtree) {
+    int subtreeDepth = subtree.depth();
+    while(path != null){
+      if (subtreeDepth > path.depth()) {
+        return false;
+      }
+      if(subtree.equals(path)){
+        return true;
+      }
+      path = path.getParent();
+    }
+    return false;
+  }
+
+
+  /*
+   * Method to determine if an existing location covers the given location and record the coverage in output.
+   */
+  private boolean isCovered(TreeSet<String> locations, Path path) {
+    Path originalPath = new Path(path.toString());
+    while(path != null){
+      if(locations.contains(path.toString())){
+        addCoverage(path, originalPath, true);
+        return true;
+      }
+      path = path.getParent();
+    }
+    return false;
+  }
+
+  /*
+   * Method to cover a child node using a parent.
+   * Removes the child and marks all nodes covered by the child as being covered by the parent.
+   */
+  private void addCoverage(Path parentPath, Path childPath, boolean addChild) {
+    String childLoc = childPath.toString();
+    String parentLoc = parentPath.toString();
+    HashSet<String> pathsUnderChild = coverageList.get(childLoc);
+    coverageList.remove(childLoc);
+    if(coverageList.get(parentLoc) == null) {
+      coverageList.put(parentLoc, new HashSet<>());
+    }
+    HashSet pathsUnderParent = coverageList.get(parentLoc);
+    if(addChild) {
+      pathsUnderParent.add(childPath.toString());
+    }
+    if(pathsUnderChild != null) {
+      pathsUnderParent.addAll(pathsUnderChild);
+      pathsUnderChild = null;

Review comment:
       Remove  this. JVM will detect the reachability.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org