You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hive.apache.org by pv...@apache.org on 2017/08/03 10:04:20 UTC

hive git commit: Remaining file for HIVE-17144

Repository: hive
Updated Branches:
  refs/heads/master 68b2f9b2a -> 889a60a44


Remaining file for HIVE-17144


Project: http://git-wip-us.apache.org/repos/asf/hive/repo
Commit: http://git-wip-us.apache.org/repos/asf/hive/commit/889a60a4
Tree: http://git-wip-us.apache.org/repos/asf/hive/tree/889a60a4
Diff: http://git-wip-us.apache.org/repos/asf/hive/diff/889a60a4

Branch: refs/heads/master
Commit: 889a60a446e56f28542b4ccf71950908ae349cf5
Parents: 68b2f9b
Author: Peter Vary <pv...@cloudera.com>
Authored: Thu Aug 3 12:03:21 2017 +0200
Committer: Peter Vary <pv...@cloudera.com>
Committed: Thu Aug 3 12:03:21 2017 +0200

----------------------------------------------------------------------
 .../apache/hadoop/hive/ql/parse/TestExport.java |  60 +++++++++
 .../hive/ql/parse/repl/dump/io/CopyUtils.java   | 121 +++++++++++++++++++
 2 files changed, 181 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/hive/blob/889a60a4/itests/hive-unit/src/test/java/org/apache/hadoop/hive/ql/parse/TestExport.java
----------------------------------------------------------------------
diff --git a/itests/hive-unit/src/test/java/org/apache/hadoop/hive/ql/parse/TestExport.java b/itests/hive-unit/src/test/java/org/apache/hadoop/hive/ql/parse/TestExport.java
new file mode 100644
index 0000000..0907b9f
--- /dev/null
+++ b/itests/hive-unit/src/test/java/org/apache/hadoop/hive/ql/parse/TestExport.java
@@ -0,0 +1,60 @@
+package org.apache.hadoop.hive.ql.parse;
+
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.hdfs.MiniDFSCluster;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TestName;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+
+public class TestExport {
+
+  protected static final Logger LOG = LoggerFactory.getLogger(TestExport.class);
+  private static WarehouseInstance hiveWarehouse;
+
+  @Rule
+  public final TestName testName = new TestName();
+  private String dbName;
+
+  @BeforeClass
+  public static void classLevelSetup() throws Exception {
+    Configuration conf = new Configuration();
+    conf.set("dfs.client.use.datanode.hostname", "true");
+    MiniDFSCluster miniDFSCluster =
+        new MiniDFSCluster.Builder(conf).numDataNodes(1).format(true).build();
+    hiveWarehouse = new WarehouseInstance(LOG, miniDFSCluster, false);
+  }
+
+  @AfterClass
+  public static void classLevelTearDown() throws IOException {
+    hiveWarehouse.close();
+  }
+
+  @Before
+  public void setup() throws Throwable {
+    dbName = testName.getMethodName() + "_" + +System.currentTimeMillis();
+    hiveWarehouse.run("create database " + dbName);
+  }
+
+  @Test
+  public void shouldExportImportATemporaryTable() throws Throwable {
+    String path = "hdfs:///tmp/" + dbName + "/";
+    String exportPath = "'" + path + "'";
+    String importDataPath = path + "/data";
+    hiveWarehouse
+        .run("use " + dbName)
+        .run("create temporary table t1 (i int)")
+        .run("insert into table t1 values (1),(2)")
+        .run("export table t1 to " + exportPath)
+        .run("create temporary table t2 like t1")
+        .run("load data inpath '" + importDataPath + "' overwrite into table t2")
+        .run("select * from t2")
+        .verifyResults(new String[] { "1", "2" });
+  }
+}

http://git-wip-us.apache.org/repos/asf/hive/blob/889a60a4/ql/src/java/org/apache/hadoop/hive/ql/parse/repl/dump/io/CopyUtils.java
----------------------------------------------------------------------
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/parse/repl/dump/io/CopyUtils.java b/ql/src/java/org/apache/hadoop/hive/ql/parse/repl/dump/io/CopyUtils.java
new file mode 100644
index 0000000..f20bfaa
--- /dev/null
+++ b/ql/src/java/org/apache/hadoop/hive/ql/parse/repl/dump/io/CopyUtils.java
@@ -0,0 +1,121 @@
+package org.apache.hadoop.hive.ql.parse.repl.dump.io;
+
+import org.apache.hadoop.fs.ContentSummary;
+import org.apache.hadoop.fs.FileSystem;
+import org.apache.hadoop.fs.FileUtil;
+import org.apache.hadoop.fs.Path;
+import org.apache.hadoop.hive.common.FileUtils;
+import org.apache.hadoop.hive.conf.HiveConf;
+import org.apache.hadoop.hive.shims.ShimLoader;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+class CopyUtils {
+
+  private static final Logger LOG = LoggerFactory.getLogger(CopyUtils.class);
+
+  private final HiveConf hiveConf;
+  private final long maxCopyFileSize;
+  private final long maxNumberOfFiles;
+  private final boolean hiveInTest;
+  private final String copyAsUser;
+
+  CopyUtils(HiveConf hiveConf) {
+    this.hiveConf = hiveConf;
+    maxNumberOfFiles = hiveConf.getLongVar(HiveConf.ConfVars.HIVE_EXEC_COPYFILE_MAXNUMFILES);
+    maxCopyFileSize = hiveConf.getLongVar(HiveConf.ConfVars.HIVE_EXEC_COPYFILE_MAXSIZE);
+    hiveInTest = hiveConf.getBoolVar(HiveConf.ConfVars.HIVE_IN_TEST);
+    this.copyAsUser = hiveConf.getVar(HiveConf.ConfVars.HIVE_DISTCP_DOAS_USER);
+  }
+
+  void doCopy(Path destination, List<Path> srcPaths) throws IOException {
+    Map<FileSystem, List<Path>> map = fsToFileMap(srcPaths);
+    FileSystem destinationFs = destination.getFileSystem(hiveConf);
+
+    for (Map.Entry<FileSystem, List<Path>> entry : map.entrySet()) {
+      if (regularCopy(destinationFs, entry)) {
+        Path[] paths = entry.getValue().toArray(new Path[] {});
+        FileUtil.copy(entry.getKey(), paths, destinationFs, destination, false, true, hiveConf);
+      } else {
+        FileUtils.distCp(
+            entry.getKey(),   // source file system
+            entry.getValue(), // list of source paths
+            destination,
+            false,
+            copyAsUser,
+            hiveConf,
+            ShimLoader.getHadoopShims()
+        );
+      }
+    }
+  }
+
+  /*
+      Check for conditions that will lead to local copy, checks are:
+      1. we are testing hive.
+      2. both source and destination are same FileSystem
+      3. either source or destination is a "local" FileSystem("file")
+      4. aggregate fileSize of all source Paths(can be directory /  file) is less than configured size.
+      5. number of files of all source Paths(can be directory /  file) is less than configured size.
+  */
+  private boolean regularCopy(FileSystem destinationFs, Map.Entry<FileSystem, List<Path>> entry)
+      throws IOException {
+    if (hiveInTest) {
+      return true;
+    }
+    FileSystem sourceFs = entry.getKey();
+    boolean isLocalFs = isLocal(sourceFs) || isLocal(destinationFs);
+    boolean sameFs = sourceFs.equals(destinationFs);
+    if (isLocalFs || sameFs) {
+      return true;
+    }
+
+    /*
+       we have reached the point where we are transferring files across fileSystems.
+    */
+    long size = 0;
+    long numberOfFiles = 0;
+
+    for (Path path : entry.getValue()) {
+      ContentSummary contentSummary = sourceFs.getContentSummary(path);
+      size += contentSummary.getLength();
+      numberOfFiles += contentSummary.getFileCount();
+      if (limitReachedForLocalCopy(size, numberOfFiles)) {
+        return false;
+      }
+    }
+    return true;
+  }
+
+  private boolean limitReachedForLocalCopy(long size, long numberOfFiles) {
+    boolean result = size > maxCopyFileSize || numberOfFiles > maxNumberOfFiles;
+    if (result) {
+      LOG.info("Source is {} bytes. (MAX: {})", size, maxCopyFileSize);
+      LOG.info("Source is {} files. (MAX: {})", numberOfFiles, maxNumberOfFiles);
+      LOG.info("going to launch distributed copy (distcp) job.");
+    }
+    return result;
+  }
+
+  private boolean isLocal(FileSystem fs) {
+    return fs.getScheme().equals("file");
+  }
+
+  private Map<FileSystem, List<Path>> fsToFileMap(List<Path> srcPaths) throws IOException {
+    Map<FileSystem, List<Path>> result = new HashMap<>();
+    for (Path path : srcPaths) {
+      FileSystem fileSystem = path.getFileSystem(hiveConf);
+      if (!result.containsKey(fileSystem)) {
+        result.put(fileSystem, new ArrayList<>());
+      }
+      result.get(fileSystem).add(path);
+    }
+    return result;
+  }
+}