更新时间:2024-08-05 GMT+08:00

读Kudu数据

功能简介

通过KuduClient.newScannerBuilder(KuduTable table)方法生成一个KuduScanner对象,然后再通过设置谓词条件从Kudu表里过滤读取数据。

代码样例

如下是读取数据的代码片段:

KuduTable table = client.openTable(tableName);
Schema schema = table.getSchema();

// Scan with a predicate on the 'key' column, returning the 'value' and "added" columns.
List<String> projectColumns = new ArrayList<>(2);
projectColumns.add("key");
projectColumns.add("value");
projectColumns.add("added");
int lowerBound = 0;
KuduPredicate lowerPred = KuduPredicate.newComparisonPredicate(
schema.getColumn("key"),
    ComparisonOp.GREATER_EQUAL,
    lowerBound);
int upperBound = numRows / 2;
KuduPredicate upperPred = KuduPredicate.newComparisonPredicate(
schema.getColumn("key"),
    ComparisonOp.LESS,
    upperBound);
KuduScanner scanner = client.newScannerBuilder(table)
    .setProjectedColumnNames(projectColumns)
    .addPredicate(lowerPred)
    .addPredicate(upperPred)
    .build();

// Check the correct number of values and null values are returned, and
// that the default value was set for the new column on each row.
// Note: scanning a hash-partitioned table will not return results in primary key order.
int resultCount = 0;
int nullCount = 0;
while (scanner.hasMoreRows()) {
RowResultIterator results = scanner.nextRows();
  while (results.hasNext()) {
    RowResult result = results.next();
    if (result.isNull("value")) {
      nullCount++;
    }
    double added = result.getDouble("added");
    if (added != DEFAULT_DOUBLE) {
      throw new RuntimeException("expected added=" + DEFAULT_DOUBLE +
          " but got added= " + added);
    }
    resultCount++;
  }
}

示例代码中:

  • projectColumns表示要返回的列信息。
  • lowerPred和upperBound表示作用在主键key上的谓词。