|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +#include <iostream> |
| 19 | +#include <string> |
| 20 | +#include <unordered_map> |
| 21 | +#include <vector> |
| 22 | + |
| 23 | +#include "fluss.hpp" |
| 24 | + |
| 25 | +static void check(const char* step, const fluss::Result& r) { |
| 26 | + if (!r.Ok()) { |
| 27 | + std::cerr << step << " failed: code=" << r.error_code << " msg=" << r.error_message |
| 28 | + << std::endl; |
| 29 | + std::exit(1); |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +int main() { |
| 34 | + const std::string bootstrap = "127.0.0.1:9123"; |
| 35 | + const std::string db_name = "admin_example_db"; |
| 36 | + const std::string table_name = "admin_example_table"; |
| 37 | + |
| 38 | + // 1) Connect and get Admin |
| 39 | + fluss::Connection conn; |
| 40 | + check("connect", fluss::Connection::Connect(bootstrap, conn)); |
| 41 | + |
| 42 | + fluss::Admin admin; |
| 43 | + check("get_admin", conn.GetAdmin(admin)); |
| 44 | + |
| 45 | + // 2) Database operations |
| 46 | + std::cout << "--- Database operations ---" << std::endl; |
| 47 | + |
| 48 | + bool exists = false; |
| 49 | + check("database_exists (before create)", admin.DatabaseExists(db_name, exists)); |
| 50 | + std::cout << "Database " << db_name << " exists before create: " << (exists ? "yes" : "no") |
| 51 | + << std::endl; |
| 52 | + |
| 53 | + fluss::DatabaseDescriptor db_desc; |
| 54 | + db_desc.comment = "Example database for Admin API"; |
| 55 | + db_desc.properties["owner"] = "admin_example"; |
| 56 | + check("create_database", admin.CreateDatabase(db_name, db_desc, true)); |
| 57 | + |
| 58 | + check("database_exists (after create)", admin.DatabaseExists(db_name, exists)); |
| 59 | + std::cout << "Database " << db_name << " exists after create: " << (exists ? "yes" : "no") |
| 60 | + << std::endl; |
| 61 | + |
| 62 | + fluss::DatabaseInfo db_info; |
| 63 | + check("get_database_info", admin.GetDatabaseInfo(db_name, db_info)); |
| 64 | + std::cout << "Database info: name=" << db_info.database_name |
| 65 | + << " comment=" << db_info.comment << " created_time=" << db_info.created_time |
| 66 | + << std::endl; |
| 67 | + |
| 68 | + std::vector<std::string> databases; |
| 69 | + check("list_databases", admin.ListDatabases(databases)); |
| 70 | + std::cout << "List databases (" << databases.size() << "): "; |
| 71 | + for (size_t i = 0; i < databases.size(); ++i) { |
| 72 | + if (i > 0) std::cout << ", "; |
| 73 | + std::cout << databases[i]; |
| 74 | + } |
| 75 | + std::cout << std::endl; |
| 76 | + |
| 77 | + // 3) Table operations in the new database |
| 78 | + std::cout << "--- Table operations ---" << std::endl; |
| 79 | + |
| 80 | + fluss::TablePath table_path(db_name, table_name); |
| 81 | + |
| 82 | + bool table_exists_flag = false; |
| 83 | + check("table_exists (before create)", admin.TableExists(table_path, table_exists_flag)); |
| 84 | + std::cout << "Table " << db_name << "." << table_name |
| 85 | + << " exists before create: " << (table_exists_flag ? "yes" : "no") << std::endl; |
| 86 | + |
| 87 | + auto schema = fluss::Schema::NewBuilder() |
| 88 | + .AddColumn("id", fluss::DataType::Int()) |
| 89 | + .AddColumn("name", fluss::DataType::String()) |
| 90 | + .Build(); |
| 91 | + auto descriptor = fluss::TableDescriptor::NewBuilder() |
| 92 | + .SetSchema(schema) |
| 93 | + .SetBucketCount(1) |
| 94 | + .SetComment("admin example table") |
| 95 | + .Build(); |
| 96 | + |
| 97 | + check("create_table", admin.CreateTable(table_path, descriptor, true)); |
| 98 | + |
| 99 | + check("table_exists (after create)", admin.TableExists(table_path, table_exists_flag)); |
| 100 | + std::cout << "Table exists after create: " << (table_exists_flag ? "yes" : "no") << std::endl; |
| 101 | + |
| 102 | + std::vector<std::string> tables; |
| 103 | + check("list_tables", admin.ListTables(db_name, tables)); |
| 104 | + std::cout << "List tables in " << db_name << " (" << tables.size() << "): "; |
| 105 | + for (size_t i = 0; i < tables.size(); ++i) { |
| 106 | + if (i > 0) std::cout << ", "; |
| 107 | + std::cout << tables[i]; |
| 108 | + } |
| 109 | + std::cout << std::endl; |
| 110 | + |
| 111 | + // 4) Cleanup: drop table, then drop database |
| 112 | + std::cout << "--- Cleanup ---" << std::endl; |
| 113 | + check("drop_table", admin.DropTable(table_path, true)); |
| 114 | + check("drop_database", admin.DropDatabase(db_name, true, true)); |
| 115 | + |
| 116 | + check("database_exists (after drop)", admin.DatabaseExists(db_name, exists)); |
| 117 | + std::cout << "Database exists after drop: " << (exists ? "yes" : "no") << std::endl; |
| 118 | + |
| 119 | + std::cout << "Admin example completed successfully." << std::endl; |
| 120 | + return 0; |
| 121 | +} |
0 commit comments