栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

Apache Arrow读写文件(parquet,arrow文件)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Apache Arrow读写文件(parquet,arrow文件)

目标:在C++层实现将Arrow Table的表数据写入到文件;在GO层实现读取文件完成表数据的提取。 实现流程如下:

C++层:

//构建表数据

std::shared_ptr CreateTable1() {
    auto schema =
            arrow::schema({arrow::field("a", arrow::int64()),arrow::field("b", arrow::int64())});
    std::shared_ptr array_a;
    std::shared_ptr array_b;

    arrow::NumericBuilder builder;
    ABORT_ON_FAILURE(builder.AppendValues({0, 1, 2, 3, 4}));
    ABORT_ON_FAILURE(builder.Finish(&array_a));
    builder.Reset();
    ABORT_ON_FAILURE(builder.AppendValues({9, 8, 7, 6, 5}));
    ABORT_ON_FAILURE(builder.Finish(&array_b));
    builder.Reset();
    return arrow::Table::Make(schema, {array_a, array_b});
}
//表数据写入文件
std::string CreateExampleFeatherDataset(const std::shared_ptr& filesystem,
                                        const std::string& root_path) {
  auto base_path = root_path + "/feather_dataset";//文件夹路径
  ABORT_ON_FAILURE(filesystem->CreateDir(base_path));
  // Create an Arrow Table
  auto table = CreateTable1();
  // Write it into two Feather files
  auto output = filesystem->OpenOutputStream(base_path + "/data1.arrow").ValueOrDie();//写入的文件路径 /home/weili/CLionProjects/ExampleDataSet/feather_dataset/data1.feather
  auto writer = arrow::ipc::MakeFileWriter(output.get(), table->schema()).ValueOrDie();//从stream sink and schema创建一个新的IPC文件写入器
  ABORT_ON_FAILURE(writer->WriteTable(*table));//通过创建 record batches序列来写可能是分块的表
  ABORT_ON_FAILURE(writer->Close());//写完关闭文件
  return base_path;
}

GO层实现:(GO版本编译需要GO1.17以上)

//读C++写入的文件
func TestReadFile(t *testing.T) {
	schema := arrow.NewSchema(
		[]arrow.Field{
			arrow.Field{Name: "a", Type: arrow.PrimitiveTypes.Int64,Nullable:true},
			arrow.Field{Name: "b", Type: arrow.PrimitiveTypes.Int64, Nullable: true},
		}, nil,
	)

	mem := memory.NewCheckedAllocator(memory.NewGoAllocator())
	defer mem.AssertSize(t, 0)

	f, err := os.OpenFile("/home/sss/CLionProjects/ExampleDataSet/feather_dataset/data1.arrow", os.O_RDWR, 0600)
	if err != nil {
		t.Fatal(err)
	}
	defer f.Close()

	_, err1 := f.Seek(0, io.SeekStart)
	if err1 != nil {
		t.Fatal(err1)
	}

	r, err2 := ipc.NewFileReader(f, ipc.WithSchema(schema), ipc.WithAllocator(mem))
	if err2 != nil {
		t.Fatal(err2)
	}
	defer r.Close()

	for i := 0; i < r.NumRecords(); i++ {
		rec, err := r.Record(i)
		if err != nil {
			t.Fatalf("could not read record %d: %v", i, err)
		}
		fmt.Printf("该表行数: %dn",rec.NumRows())
		fmt.Printf("该表列数: %dn",rec.NumCols())
		fmt.Printf("该表第1列名字: %sn",rec.ColumnName(0))
		fmt.Printf("该表第2列名字: %sn",rec.ColumnName(1))
		fmt.Printf("该表第1列数据: ")
		fmt.Println(rec.Column(0))
		fmt.Printf("该表第2列数据: ")
		fmt.Println(rec.Column(1))
	}
}

输出结果:

 结论:与C++层写入的信息一致。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/699601.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号