public boolean copyTable()

in odps-sdk-impl/odps-common-local/src/main/java/com/aliyun/odps/local/common/WareHouse.java [311:401]


  public boolean copyTable(String projectName, String tableName, PartitionSpec partSpec,
                           String[] readCols, File destDir, int limitDownloadRecordCount,
                           char inputColumnSeperator) {

    if (StringUtils.isBlank(projectName) || StringUtils.isBlank(tableName) || destDir == null) {
      return false;
    }
    TableInfo tableInfo = TableInfo.builder().projectName(projectName).tableName(tableName)
        .partSpec(partSpec).build();

    LOG.info("Start to copy table: " + tableInfo + "-->" + destDir.getAbsolutePath());

    boolean hasPartition = false;
    if (partSpec != null && !partSpec.isEmpty()) {
      hasPartition = true;
    }

    // if not exist table, then download from odps server
    if (hasPartition && !existsPartition(projectName, tableName, partSpec)) {
      DownloadUtils.downloadTableSchemeAndData(getOdps(), tableInfo, limitDownloadRecordCount,
                                               inputColumnSeperator);
    } else if (!existsTable(projectName, tableName)) {
      DownloadUtils.downloadTableSchemeAndData(getOdps(), tableInfo, limitDownloadRecordCount,
                                               inputColumnSeperator);
    }

    File whTableDir = getTableDir(projectName, tableName);

    // copy schema file
    File schemaFile = new File(whTableDir, Constants.SCHEMA_FILE);
    if (!schemaFile.exists()) {
      throw new RuntimeException("Schema file of table " + projectName + "." + tableName
                                 + " not exists in warehouse.");
    }

    if (!destDir.exists()) {
      destDir.mkdirs();
    }

    // copy table schema file
    try {
      FileUtils.copyFileToDirectory(schemaFile, destDir);
    } catch (IOException e) {
      throw new RuntimeException("Copy schema file of table " + tableInfo + " failed!"
                                 + e.getMessage());
    }

    // copy partition data files
    TableMeta tableMeta = getTableMeta(projectName, tableName);
    List<Integer> indexes = LocalRunUtils.genReadColsIndexes(tableMeta, readCols);

    if (hasPartition) {
      final Collection<File> dataFiles = FileUtils.listFiles(whTableDir, HiddenFileFilter.VISIBLE,
                                                             HiddenFileFilter.VISIBLE);
      for (File dataFile : dataFiles) {
        if (dataFile.getName().equals(Constants.SCHEMA_FILE)) {
          continue;
        }
        String parentDir = dataFile.getParentFile().getAbsolutePath();

        String partPath = parentDir.substring(whTableDir.getAbsolutePath().length(),
                                              parentDir.length());
        PartitionSpec ps = PartitionUtils.convert(partPath);
        if (PartitionUtils.isEqual(ps, partSpec)) {
          File destPartitionDir = new File(destDir, PartitionUtils.toString(ps));
          destPartitionDir.mkdirs();
          try {
            copyDataFiles(dataFile.getParentFile(), indexes, destPartitionDir,
                          inputColumnSeperator);
          } catch (IOException e) {
            throw new RuntimeException("Copy data file of table " + tableInfo + " failed!"
                                       + e.getMessage());
          }
        }

      }
    } else {
      try {
        copyDataFiles(whTableDir, indexes, destDir, inputColumnSeperator);
      } catch (IOException e) {
        throw new RuntimeException("Copy data file of table " + tableInfo + " failed!"
                                   + e.getMessage());
      }

    }

    LOG.info("Finished copy table: " + tableInfo + "-->" + destDir.getAbsolutePath());

    return true;

  }