htable.get(list< get>)能在返回的数组中放置空引用吗?

zengzsys  于 2021-06-03  发布在  Hadoop
关注(0)|答案(1)|浏览(308)

hbase javadoc对于htable.get(list)方法来说非常混乱。
作为返回参数文档,我们有以下语句:

If there are any failures even after retries,
there will be a null in the results array for those Gets,
AND an exception will be thrown.

我不理解“and”:我们可以在返回的数组中有一个异常或一个null,而不是像文档所暗示的那样同时有两个。
我从来没有听说过一个java方法既能引发异常又能返回一些东西。
当我调用这个方法时,我在代码中处理异常,但是我是否也需要担心结果数组中的空引用?

uxhixvfz

uxhixvfz1#

这里的文档有误导性,因为这个函数不会返回结果,并且在失败的情况下会同时抛出错误。
我把这个挖出来是因为我也很困惑。
以下是此函数的源代码:

/**{@inheritDoc} */
  @Override
  public Result[] get(List<Get> gets) throws IOException {
    LOG.trace("get(List<>)");
    Preconditions.checkNotNull(gets);
    if (gets.isEmpty()) {
      return new Result[0];
    } else if (gets.size() == 1) {
      try {
        return new Result[] {get(gets.get(0))};
      } catch (IOException e) {
        throw createRetriesExhaustedWithDetailsException(e, gets.get(0));
      }
    } else {
      try (Scope scope = TRACER.spanBuilder("BigtableTable.get").startScopedSpan()) {
        addBatchSizeAnnotation(gets);
        return getBatchExecutor().batch(gets);
      }
    }
  }

好的,如果一个列表中有不止一个项目,它会调用 getBatchExecutor().batch(gets) ,该函数的定义如下:

public Result[] batch(List<? extends Row> actions) throws IOException {
    try {
      Object[] resultsOrErrors = new Object[actions.size()];
      batchCallback(actions, resultsOrErrors, null);
      // At this point we are guaranteed that the array only contains results,
      // if it had any errors, batch would've thrown an exception
      Result[] results = new Result[resultsOrErrors.length];
      System.arraycopy(resultsOrErrors, 0, results, 0, results.length);
      return results;
    } catch (InterruptedException e) {
      Thread.currentThread().interrupt();
      LOG.error("Encountered exception in batch(List<>).", e);
      throw new IOException("Batch error", e);
    }
  }

请看下面的评论:
此时我们可以保证数组只包含结果,如果它有任何错误,批处理将抛出异常
这意味着这个函数只会在失败的情况下引发ioexception,而没有关于结果的进一步信息。但是,如何找出批处理中失败和正确处理的项目呢?原来还有另一个版本的函数 batch 定义于 Table 支持这种情况:

/**{@inheritDoc} */
  @Override
  public void batch(List<? extends Row> actions, Object[] results)
      throws IOException, InterruptedException {
    LOG.trace("batch(List<>, Object[])");
    try (Scope scope = TRACER.spanBuilder("BigtableTable.batch").startScopedSpan()) {
      addBatchSizeAnnotation(actions);
      getBatchExecutor().batch(actions, results);
    }
  }

以及中相应的定义 BatchExecutor :

public void batch(List<? extends Row> actions, @Nullable Object[] results)
      throws IOException, InterruptedException {
    batchCallback(actions, results, null);
  }
  public <R> void batchCallback(
      List<? extends Row> actions, Object[] results, Batch.Callback<R> callback)
      throws IOException, InterruptedException {
    Preconditions.checkArgument(
        results == null || results.length == actions.size(),
        "Result array must have same dimensions as actions list.");
    if (actions.isEmpty()) {
      return;
    }
    if (results == null) {
      results = new Object[actions.size()];
    }
    Timer.Context timerContext = batchTimer.time();
    List<ApiFuture<?>> resultFutures = issueAsyncRowRequests(actions, results, callback);
    // Don't want to throw an exception for failed futures, instead the place in results is
    // set to null.
    List<Throwable> problems = new ArrayList<>();
    List<Row> problemActions = new ArrayList<>();
    List<String> hosts = new ArrayList<>();
    for (int i = 0; i < resultFutures.size(); i++) {
      try {
        resultFutures.get(i).get();
      } catch (ExecutionException e) {
        problemActions.add(actions.get(i));
        problems.add(e.getCause());
        hosts.add(options.getDataHost());
      }
    }
    if (problems.size() > 0) {
      throw new RetriesExhaustedWithDetailsException(problems, problemActions, hosts);
    }
    timerContext.close();
  }

使用此函数,可以传入(空) results 将批处理的大小放入数组,当结果传入时,此数组将被填充。最后,如果批处理中的任何项失败,将出现一个异常,详细说明失败的原因,但仍然是您的问题 results 数组中填充了所有项的结果。失败的项目将被删除 null 在该数组中,其余的包含一个实际的 Result .

相关问题