MCPcopy Create free account
hub / github.com/apache/arrow / RunExample

Function RunExample

cpp/examples/arrow/from_json_string_example.cc:37–91  ·  view source on GitHub ↗

* \brief Run Example * * ./debug/from-json-string-example */

Source from the content-addressed store, hash-verified

35 * ./debug/from-json-string-example
36 */
37arrow::Status RunExample() {
38 // Simple types
39 ARROW_ASSIGN_OR_RAISE(auto int32_array,
40 ArrayFromJSONString(arrow::int32(), "[1, 2, 3]"));
41 ARROW_ASSIGN_OR_RAISE(auto float64_array,
42 ArrayFromJSONString(arrow::float64(), "[4.0, 5.0, 6.0]"));
43 ARROW_ASSIGN_OR_RAISE(auto bool_array,
44 ArrayFromJSONString(arrow::boolean(), "[true, false, true]"));
45 ARROW_ASSIGN_OR_RAISE(
46 auto string_array,
47 ArrayFromJSONString(arrow::utf8(), R"(["Hello", "World", null])"));
48
49 // Timestamps can be created from string representations
50 ARROW_ASSIGN_OR_RAISE(
51 auto ts_array,
52 ArrayFromJSONString(timestamp(arrow::TimeUnit::SECOND),
53 R"(["1970-01-01", "2000-02-29","3989-07-14","1900-02-28"])"));
54
55 // List, Map, Struct
56 ARROW_ASSIGN_OR_RAISE(
57 auto list_array,
58 ArrayFromJSONString(list(arrow::int64()),
59 "[[null], [], null, [4, 5, 6, 7, 8], [2, 3]]"));
60 ARROW_ASSIGN_OR_RAISE(
61 auto map_array,
62 ArrayFromJSONString(map(arrow::utf8(), arrow::int32()),
63 R"([[["joe", 0], ["mark", null]], null, [["cap", 8]], []])"));
64 ARROW_ASSIGN_OR_RAISE(
65 auto struct_array,
66 ArrayFromJSONString(
67 arrow::struct_({field("one", arrow::int32()), field("two", arrow::int32())}),
68 "[[11, 22], null, [null, 33]]"));
69
70 // ChunkedArrayFromJSONString
71 ARROW_ASSIGN_OR_RAISE(
72 auto chunked_array,
73 ChunkedArrayFromJSONString(arrow::int32(), {"[5, 10]", "[null]", "[16]"}));
74
75 // DictArrayFromJSONString
76 ARROW_ASSIGN_OR_RAISE(
77 auto dict_array,
78 DictArrayFromJSONString(dictionary(arrow::int32(), arrow::utf8()),
79 "[0, 1, 0, 2, 0, 3]", R"(["k1", "k2", "k3", "k4"])"));
80
81 return arrow::Status::OK();
82}
83
84int main(int argc, char** argv) {
85 auto status = RunExample();
86 if (!status.ok()) {
87 std::cerr << status.ToString() << std::endl;
88 return EXIT_FAILURE;
89 }
90 return EXIT_SUCCESS;
91}

Callers

nothing calls this directly

Calls 6

ArrayFromJSONStringFunction · 0.85
struct_Function · 0.85
DictArrayFromJSONStringFunction · 0.85
fieldFunction · 0.50
dictionaryFunction · 0.50

Tested by

no test coverage detected