gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 18:41:34 27 4
gpt4 key购买 nike

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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com