Skip to content

Instantly share code, notes, and snippets.

@ianmcook
Last active June 2, 2022 14:01
Show Gist options
  • Select an option

  • Save ianmcook/124455c6b8196996de5ffb3ca5556bfa to your computer and use it in GitHub Desktop.

Select an option

Save ianmcook/124455c6b8196996de5ffb3ca5556bfa to your computer and use it in GitHub Desktop.
Create and print an Arrow Table in C++
#include <iostream>
#include <arrow/api.h>
#include <arrow/result.h>
#include <arrow/compute/api.h>
arrow::Status Execute() {
arrow::Int32Builder int_builder;
ARROW_RETURN_NOT_OK(int_builder.Append(1));
ARROW_RETURN_NOT_OK(int_builder.Append(2));
ARROW_RETURN_NOT_OK(int_builder.Append(3));
ARROW_ASSIGN_OR_RAISE(std::shared_ptr<arrow::Array> int_array, int_builder.Finish())
arrow::StringBuilder str_builder;
ARROW_RETURN_NOT_OK(str_builder.Append("a"));
ARROW_RETURN_NOT_OK(str_builder.Append("b"));
ARROW_RETURN_NOT_OK(str_builder.Append("c"));
ARROW_ASSIGN_OR_RAISE(std::shared_ptr<arrow::Array> str_array, str_builder.Finish())
std::vector<std::shared_ptr<arrow::Field>> schema_vector = {
arrow::field("int", arrow::int32()),
arrow::field("str", arrow::utf8())
};
auto schema = std::make_shared<arrow::Schema>(schema_vector);
std::shared_ptr<arrow::Table> table = arrow::Table::Make(schema, {int_array, str_array});
arrow::PrettyPrintOptions options{0};
ARROW_RETURN_NOT_OK(arrow::PrettyPrint(*table, options, &std::cout));
return arrow::Status::OK();
}
int main(int argc, char** argv) {
auto status = Execute();
if (!status.ok()) {
std::cerr << "Error occurred : " << status.message() << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment