-
Notifications
You must be signed in to change notification settings - Fork 8
Add OceanBase Database examples #69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
esadek
wants to merge
3
commits into
columnar-tech:main
Choose a base branch
from
esadek:oceanbase
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| # Copyright 2026 Columnar Technologies Inc. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| cmake_minimum_required(VERSION 3.15) | ||
| project(mysql_demo CXX) | ||
|
|
||
| set(CMAKE_CXX_STANDARD 17) | ||
| set(CMAKE_CXX_STANDARD_REQUIRED ON) | ||
|
|
||
| # Use conda environment paths | ||
| if(DEFINED ENV{CONDA_PREFIX}) | ||
| list(APPEND CMAKE_PREFIX_PATH "$ENV{CONDA_PREFIX}") | ||
| endif() | ||
|
|
||
| find_package(Arrow REQUIRED) | ||
|
|
||
| add_executable(mysql_demo main.cpp) | ||
|
|
||
| if(DEFINED ENV{CONDA_PREFIX}) | ||
| target_include_directories(mysql_demo PRIVATE $ENV{CONDA_PREFIX}/include) | ||
| target_link_directories(mysql_demo PRIVATE $ENV{CONDA_PREFIX}/lib) | ||
| endif() | ||
|
|
||
| target_link_libraries(mysql_demo | ||
| adbc_driver_manager | ||
| Arrow::arrow_shared | ||
| ) | ||
|
|
||
| target_compile_options(mysql_demo PRIVATE | ||
| -Wall | ||
| -Werror | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| # Copyright 2026 Columnar Technologies Inc. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| CXXFLAGS = -std=c++17 -Wall -Werror -I$(CONDA_PREFIX)/include | ||
| LIBS = -L$(CONDA_PREFIX)/lib -ladbc_driver_manager -larrow -Wl,-rpath,$(CONDA_PREFIX)/lib | ||
|
|
||
| mysql_demo: main.cpp | ||
| $(CXX) $(CXXFLAGS) -o mysql_demo main.cpp $(LIBS) | ||
|
|
||
| clean: | ||
| rm -f mysql_demo | ||
|
|
||
| .PHONY: clean |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| <!-- | ||
| Copyright 2026 Columnar Technologies Inc. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| --> | ||
|
|
||
| # Connecting C++ and OceanBase Database with ADBC | ||
|
|
||
| ## Instructions | ||
|
|
||
| > [!TIP] | ||
| > If you already have an OceanBase Database instance running, skip the steps to set up and clean up OceanBase Database. | ||
|
|
||
| ### Prerequisites | ||
|
|
||
| 1. [Install dbc](https://docs.columnar.tech/dbc/getting_started/installation/) | ||
|
|
||
| 2. [Install miniforge](https://github.com/conda-forge/miniforge) | ||
|
|
||
| 3. Create and activate a new environment with the required C++ libraries: | ||
|
|
||
| ```sh | ||
| mamba create -n adbc-cpp -c conda-forge cmake compilers libadbc-driver-manager libarrow | ||
|
|
||
| # Initialize mamba in your shell if not already done | ||
| eval "$(mamba shell hook --shell zsh)" | ||
| mamba activate adbc-cpp | ||
| ``` | ||
|
|
||
| (`cmake` is only needed if you use CMake to build the C++ program below.) | ||
|
|
||
| ### Set up OceanBase Database | ||
|
|
||
| 1. [Install Docker](https://docs.docker.com/get-started/get-docker/) | ||
|
|
||
| 2. Start an OceanBase Database instance: | ||
|
|
||
| ```sh | ||
| docker run -d --rm -p 2881:2881 --name obstandalone -e MODE=MINI -d quay.io/oceanbase/oceanbase-ce | ||
| ``` | ||
|
|
||
| ### Connect to OceanBase Database | ||
|
|
||
| 1. Install the MySQL ADBC driver: | ||
|
|
||
| ```sh | ||
| dbc install --level user mysql | ||
| ``` | ||
|
|
||
| 2. Customize the C++ program `main.cpp` as needed | ||
| - Change the connection arguments in the `AdbcDatabaseSetOption()` calls | ||
| - Format `uri` according to the [DSN (Data Source Name) format used by Go-MySQL-Driver](https://pkg.go.dev/github.com/go-sql-driver/mysql#section-readme), or keep it as is | ||
| - If you changed which database you're connecting to, also change the SQL SELECT statement in `AdbcStatementSetSqlQuery()` | ||
|
|
||
| 3. Build and run the C++ program: | ||
|
|
||
| Using Make: | ||
| ```sh | ||
| make | ||
| ./mysql_demo | ||
| ``` | ||
|
|
||
| Or using CMake: | ||
| ```sh | ||
| cmake -B build | ||
| cmake --build build | ||
| ./build/mysql_demo | ||
| ``` | ||
|
|
||
| ### Clean up | ||
|
|
||
| 1. Clean build artifacts: | ||
|
|
||
| Using Make: | ||
| ```sh | ||
| make clean | ||
| ``` | ||
|
|
||
| Using CMake: | ||
| ```sh | ||
| rm -rf build | ||
| ``` | ||
|
|
||
| 2. Stop the Docker container running OceanBase Database: | ||
|
|
||
| ```sh | ||
| docker stop obstandalone | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| // Copyright 2026 Columnar Technologies Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| // For EXIT_SUCCESS | ||
| #include <cstdlib> | ||
| // For strerror | ||
| #include <cstring> | ||
| #include <iostream> | ||
|
|
||
| #include <arrow-adbc/adbc.h> | ||
| #include <arrow-adbc/adbc_driver_manager.h> | ||
| #include <arrow/c/bridge.h> | ||
| #include <arrow/record_batch.h> | ||
|
|
||
| // Error-checking helper for ADBC calls. | ||
| // Assumes that there is an AdbcError named `error` in scope. | ||
| #define CHECK_ADBC(EXPR) \ | ||
| if (AdbcStatusCode status = (EXPR); status != ADBC_STATUS_OK) { \ | ||
| if (error.message != nullptr) { \ | ||
| std::cerr << error.message << std::endl; \ | ||
| } \ | ||
| return EXIT_FAILURE; \ | ||
| } | ||
|
|
||
| // Error-checking helper for ArrowArrayStream. | ||
| #define CHECK_STREAM(STREAM, EXPR) \ | ||
| if (int status = (EXPR); status != 0) { \ | ||
| std::cerr << "(" << std::strerror(status) << "): "; \ | ||
| const char *message = (STREAM).get_last_error(&(STREAM)); \ | ||
| if (message != nullptr) { \ | ||
| std::cerr << message << std::endl; \ | ||
| } else { \ | ||
| std::cerr << "(no error message)" << std::endl; \ | ||
| } \ | ||
| return EXIT_FAILURE; \ | ||
| } | ||
|
|
||
| int main() { | ||
| AdbcError error = {}; | ||
|
|
||
| AdbcDatabase database = {}; | ||
| CHECK_ADBC(AdbcDatabaseNew(&database, &error)); | ||
|
|
||
| CHECK_ADBC(AdbcDatabaseSetOption(&database, "driver", "mysql", &error)); | ||
| CHECK_ADBC(AdbcDatabaseSetOption(&database, "uri", | ||
| "root@tcp(localhost:2881)/oceanbase", &error)); | ||
| CHECK_ADBC(AdbcDriverManagerDatabaseSetLoadFlags( | ||
| &database, ADBC_LOAD_FLAG_DEFAULT, &error)); | ||
| CHECK_ADBC(AdbcDatabaseInit(&database, &error)); | ||
|
|
||
| AdbcConnection connection = {}; | ||
| CHECK_ADBC(AdbcConnectionNew(&connection, &error)); | ||
| CHECK_ADBC(AdbcConnectionInit(&connection, &database, &error)); | ||
|
|
||
| AdbcStatement statement = {}; | ||
| CHECK_ADBC(AdbcStatementNew(&connection, &statement, &error)); | ||
|
|
||
| struct ArrowArrayStream stream = {}; | ||
| int64_t rows_affected = -1; | ||
|
|
||
| CHECK_ADBC( | ||
| AdbcStatementSetSqlQuery(&statement, "SELECT version();", &error)); | ||
| CHECK_ADBC( | ||
| AdbcStatementExecuteQuery(&statement, &stream, &rows_affected, &error)); | ||
|
|
||
| // Import stream as record batch reader | ||
| auto maybe_reader = arrow::ImportRecordBatchReader(&stream); | ||
| if (!maybe_reader.ok()) { | ||
| std::cerr << "Failed to import record batch reader: " | ||
| << maybe_reader.status().message() << std::endl; | ||
| return 1; | ||
| } | ||
|
|
||
| auto reader = maybe_reader.ValueOrDie(); | ||
|
|
||
| while (true) { | ||
| auto maybe_batch = reader->Next(); | ||
| if (!maybe_batch.ok()) { | ||
| std::cerr << "Error reading batch: " << maybe_batch.status().message() | ||
| << std::endl; | ||
| return 1; | ||
| } | ||
|
|
||
| auto batch = maybe_batch.ValueOrDie(); | ||
| if (!batch) { | ||
| break; | ||
| } | ||
|
|
||
| std::cout << batch->ToString() << std::endl; | ||
| } | ||
|
|
||
| CHECK_ADBC(AdbcStatementRelease(&statement, &error)); | ||
| CHECK_ADBC(AdbcConnectionRelease(&connection, &error)); | ||
| CHECK_ADBC(AdbcDatabaseRelease(&database, &error)); | ||
|
|
||
| return EXIT_SUCCESS; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| <!-- | ||
| Copyright 2026 Columnar Technologies Inc. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| --> | ||
|
|
||
| # Connecting Go and OceanBase Database with ADBC | ||
|
|
||
| ## Instructions | ||
|
|
||
| > [!TIP] | ||
| > If you already have an OceanBase Database instance running, skip the steps to set up and clean up OceanBase Database. | ||
|
|
||
| ### Prerequisites | ||
|
|
||
| 1. [Install Go](https://go.dev/doc/install) | ||
|
|
||
| 2. [Install dbc](https://docs.columnar.tech/dbc/getting_started/installation/) | ||
|
|
||
| ### Set up OceanBase Database | ||
|
|
||
| 1. [Install Docker](https://docs.docker.com/get-started/get-docker/) | ||
|
|
||
| 2. Start an OceanBase Database instance: | ||
|
|
||
| ```sh | ||
| docker run -d --rm -p 2881:2881 --name obstandalone -e MODE=MINI -d quay.io/oceanbase/oceanbase-ce | ||
| ``` | ||
|
|
||
| ### Connect to OceanBase Database | ||
|
|
||
| 1. Install the MySQL ADBC driver: | ||
|
|
||
| ```sh | ||
| dbc install mysql | ||
| ``` | ||
|
|
||
| 2. Customize the Go program `main.go` as needed | ||
| - Change the connection arguments in the `NewDatabase()` call | ||
| - Format `uri` according to the [DSN (Data Source Name) format used by Go-MySQL-Driver](https://pkg.go.dev/github.com/go-sql-driver/mysql#section-readme), or keep it as is | ||
| - If you changed which database you're connecting to, also change the SQL SELECT statement in `stmt.SetSqlQuery()` | ||
|
|
||
| 3. Run the Go program: | ||
|
|
||
| ```sh | ||
| go mod tidy | ||
| go run main.go | ||
| ``` | ||
|
|
||
| ### Clean up | ||
|
|
||
| Stop the Docker container running OceanBase Database: | ||
|
|
||
| ```sh | ||
| docker stop obstandalone | ||
| ``` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.