This example demonstrates how to use Fory's row format for cache-friendly binary random access.
Row format is ideal for scenarios where you need:
include(FetchContent) FetchContent_Declare( fory GIT_REPOSITORY https://github.com/apache/fory.git GIT_TAG main SOURCE_SUBDIR cpp ) FetchContent_MakeAvailable(fory) target_link_libraries(your_app PRIVATE fory::row_format)
cd examples/cpp/hello_row mkdir build && cd build cmake .. -DCMAKE_BUILD_TYPE=Release cmake --build . --parallel ./hello_row
Or use the provided script:
./run.sh
From the repository root:
bazel build //examples/cpp/hello_row:hello_row bazel run //examples/cpp/hello_row:hello_row
Or use the provided script:
./run_bazel.sh
For your own project using Fory as a dependency:
MODULE.bazel.example to MODULE.bazel in your project rootBUILD.standalone to BUILD in your source directorylocal_path_override for local development# In your MODULE.bazel bazel_dep(name = "fory", version = "0.14.1") git_override( module_name = "fory", remote = "https://github.com/apache/fory.git", commit = "main", # Use specific commit for reproducibility ) # In your BUILD file cc_binary( name = "my_app", srcs = ["main.cc"], deps = [ "@fory//cpp/fory/encoder:fory_encoder", "@fory//cpp/fory/row:fory_row_format", ], )
This example demonstrates:
RowEncoder for struct encodingUse the FORY_FIELD_INFO macro to enable automatic encoding:
struct Employee { std::string name; int32_t id; float salary; }; FORY_FIELD_INFO(Employee, name, id, salary);
using namespace fory::row; // Define schema auto name_field = field("name", utf8()); auto age_field = field("age", int32()); std::vector<FieldPtr> fields = {name_field, age_field}; auto row_schema = schema(fields); // Create and write row RowWriter writer(row_schema); writer.Reset(); writer.WriteString(0, "Alice"); writer.Write(1, static_cast<int32_t>(25)); // Read back auto row = writer.ToRow(); std::cout << row->GetString(0) << std::endl; // "Alice" std::cout << row->GetInt32(1) << std::endl; // 25
using namespace fory::row; Employee emp{"Bob", 1001, 75000.0f}; encoder::RowEncoder<Employee> enc; enc.Encode(emp); auto row = enc.GetWriter().ToRow(); std::cout << row->GetString(0) << std::endl; // "Bob" std::cout << row->GetInt32(1) << std::endl; // 1001 std::cout << row->GetFloat(2) << std::endl; // 75000.0
Row format supports these field types:
| Function | Type |
|---|---|
int8() | 8-bit integer |
int16() | 16-bit int |
int32() | 32-bit int |
int64() | 64-bit int |
float32() | 32-bit float |
float64() | 64-bit float |
utf8() | UTF-8 string |
binary() | Binary data |
list(T) | List of T |
struct(Fs) | Struct fields |
# Link to row format library (includes serialization) target_link_libraries(your_app PRIVATE fory::row_format)
#include "fory/row/row.h" #include "fory/row/schema.h" #include "fory/row/writer.h" #include "fory/encoder/row_encoder.h" #include "fory/encoder/row_encode_trait.h"