java - HTable.get(List )是否可以在返回的数组中放入空引用?

HBase Javadoc对于HTable.get(List)方法非常困惑。

作为返回参数文档,我们有以下语句:

如果重试后仍然有任何失败,
这些Gets的结果数组中将为null,
并且将引发异常。

我不理解“AND”:我们可以在返回的数组中有一个异常或null,不能像文档中所暗示的那样同时存在。

我从未听说过能够同时引发异常和返回某些内容的Java方法。

调用此方法时,我在代码中处理异常,但是我是否还需要担心结果数组中的空引用?

最佳答案

这里的文档具有误导性,因为此功能不会返回结果,并且在失败的情况下会同时引发错误。

我之所以对此进行挖掘,是因为我也感到困惑。

这是此函数的source code:

  /** {@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);
    }
  }

看到那里的评论:

At this point we are guaranteed that the array only contains results, if it had any errors, batch would've thrown an exception



这意味着该函数仅在失败的情况下引发IOException,而没有关于结果的更多信息。但是,您如何查找批次中失败和正确处理的项目?原来有another version of the function batch defined on 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中的相应definition:

  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

关于java - HTable.get(List <Get>)是否可以在返回的数组中放入空引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15114141/

相关文章:

networking - 无法将Docker容器连接到Weave网络

docker - 如何使用Docker Swarm和Consul启用更快的容器重新调度?

eclipse - 使用 Docker 容器化开发环境是否合理?

docker - 无法使用端口映射通过 Marathon 启动 SonarQube docker 容

docker - 错误的 docker 标签让守护进程卡住了

docker - Docker-compose守护程序模式日志

elasticsearch - cadvisor, elasticsearch, docker :

visual-studio - 适用于Windows Beta的Docker +适用于Visual

javascript - 从主机的 Angular 来看,Docker-compose 配置前端与后

ubuntu - ubuntu下无法运行Eclipse Che