You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Apache Arrow C++中实现分组聚合及列值计数

Apache Arrow C++ 实现GROUP BY统计列值出现次数

核心实现代码

下面是基于arrow::Table统计指定列不同值出现次数的最优方案,利用Arrow Compute的分组聚合API,比手动遍历效率高得多:

#include <arrow/api.h>
#include <arrow/compute/api.h>
#include <iostream>

int main() {
  // 构建示例表
  auto int_array = arrow::MakeArrayOf<int32_t>({1, 2, 2, 3, 3, 3});
  auto schema = arrow::schema({arrow::field("value", arrow::int32())});
  auto table = arrow::Table::Make(schema, {int_array});

  // 指定分组列和聚合函数(count)
  arrow::compute::GroupByOptions options;
  options.aggregations = {arrow::compute::MakeAggregate("count", {}, {})};
  options.keys = {"value"};

  // 执行分组聚合
  auto result = arrow::compute::GroupBy(*table, options).ValueOrDie();

  // 输出结果
  std::cout << "分组统计结果:\n";
  result.table->Print();

  return 0;
}

关键说明

  • 使用Arrow Compute的GroupBy接口,底层利用Arrow的矢量化计算优化,性能远优于手动遍历
  • count聚合函数会自动统计每个分组的行数,正好对应列值的出现次数
  • 若需处理多列分组或其他聚合(如sum、avg),只需修改options.aggregations和options.keys参数即可

补充说明

目前Arrow C官方文档确实缺少这类实用示例,对比Python版本的完善示例,C侧的示例补充确实很有必要。

内容的提问来源于stack exchange,提问作者user3117152

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 15:07:44