(Doc section: RunMain Start)
| 25 | |
| 26 | // (Doc section: RunMain Start) |
| 27 | arrow::Status RunMain() { |
| 28 | // (Doc section: RunMain Start) |
| 29 | // (Doc section: int8builder 1 Append) |
| 30 | // Builders are the main way to create Arrays in Arrow from existing values that are not |
| 31 | // on-disk. In this case, we'll make a simple array, and feed that in. |
| 32 | // Data types are important as ever, and there is a Builder for each compatible type; |
| 33 | // in this case, int8. |
| 34 | arrow::Int8Builder int8builder; |
| 35 | int8_t days_raw[5] = {1, 12, 17, 23, 28}; |
| 36 | // AppendValues, as called, puts 5 values from days_raw into our Builder object. |
| 37 | ARROW_RETURN_NOT_OK(int8builder.AppendValues(days_raw, 5)); |
| 38 | // (Doc section: int8builder 1 Append) |
| 39 | |
| 40 | // (Doc section: int8builder 1 Finish) |
| 41 | // We only have a Builder though, not an Array -- the following code pushes out the |
| 42 | // built up data into a proper Array. |
| 43 | std::shared_ptr<arrow::Array> days; |
| 44 | ARROW_ASSIGN_OR_RAISE(days, int8builder.Finish()); |
| 45 | // (Doc section: int8builder 1 Finish) |
| 46 | |
| 47 | // (Doc section: int8builder 2) |
| 48 | // Builders clear their state every time they fill an Array, so if the type is the same, |
| 49 | // we can re-use the builder. We do that here for month values. |
| 50 | int8_t months_raw[5] = {1, 3, 5, 7, 1}; |
| 51 | ARROW_RETURN_NOT_OK(int8builder.AppendValues(months_raw, 5)); |
| 52 | std::shared_ptr<arrow::Array> months; |
| 53 | ARROW_ASSIGN_OR_RAISE(months, int8builder.Finish()); |
| 54 | // (Doc section: int8builder 2) |
| 55 | |
| 56 | // (Doc section: int16builder) |
| 57 | // Now that we change to int16, we use the Builder for that data type instead. |
| 58 | arrow::Int16Builder int16builder; |
| 59 | int16_t years_raw[5] = {1990, 2000, 1995, 2000, 1995}; |
| 60 | ARROW_RETURN_NOT_OK(int16builder.AppendValues(years_raw, 5)); |
| 61 | std::shared_ptr<arrow::Array> years; |
| 62 | ARROW_ASSIGN_OR_RAISE(years, int16builder.Finish()); |
| 63 | // (Doc section: int16builder) |
| 64 | |
| 65 | // (Doc section: Schema) |
| 66 | // Now, we want a RecordBatch, which has columns and labels for said columns. |
| 67 | // This gets us to the 2d data structures we want in Arrow. |
| 68 | // These are defined by schema, which have fields -- here we get both those object types |
| 69 | // ready. |
| 70 | std::shared_ptr<arrow::Field> field_day, field_month, field_year; |
| 71 | std::shared_ptr<arrow::Schema> schema; |
| 72 | |
| 73 | // Every field needs its name and data type. |
| 74 | field_day = arrow::field("Day", arrow::int8()); |
| 75 | field_month = arrow::field("Month", arrow::int8()); |
| 76 | field_year = arrow::field("Year", arrow::int16()); |
| 77 | |
| 78 | // The schema can be built from a vector of fields, and we do so here. |
| 79 | schema = arrow::schema({field_day, field_month, field_year}); |
| 80 | // (Doc section: Schema) |
| 81 | |
| 82 | // (Doc section: RBatch) |
| 83 | // With the schema and Arrays full of data, we can make our RecordBatch! Here, |
| 84 | // each column is internally contiguous. This is in opposition to Tables, which we'll |
no test coverage detected