Skip to content

Commit ae7b826

Browse files
esadekianmcook
andauthored
Add CockroachDB examples (#46)
* Add CockroachDB examples * Remove C++ binary * Add missing Python README entries * Indent continuation lines in docker run command * Format Python example * Add Java README in lieu of full example --------- Co-authored-by: Emil Sadek <[email protected]> Co-authored-by: Ian Cook <[email protected]>
1 parent 9a1fc38 commit ae7b826

File tree

25 files changed

+845
-0
lines changed

25 files changed

+845
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ Simple examples showing how to use ADBC to connect, run a query, and return the
4545
- TiDB
4646
- Vitess
4747
- PostgreSQL
48+
- CedarDB
4849
- Citus
50+
- CockroachDB
4951
- CrateDB
5052
- Neon
5153
- ParadeDB

cpp/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ Simple C++ examples showing how to use ADBC to connect, run a query, and return
3838
- TiDB
3939
- Vitess
4040
- PostgreSQL
41+
- CedarDB
4142
- Citus
43+
- CockroachDB
4244
- CrateDB
4345
- Neon
4446
- ParadeDB

cpp/postgresql/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ This directory contains examples showing how to use ADBC to connect C++ applicat
2222

2323
Any open source tool or vendor product that implements PostgreSQL frontend/backend protocol should work with the ADBC driver for PostgreSQL. The examples included here focus on the following systems:
2424

25+
- CedarDB
2526
- Citus
27+
- CockroachDB
2628
- CrateDB
2729
- Neon
2830
- ParadeDB
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Copyright 2025 Columnar Technologies Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
cmake_minimum_required(VERSION 3.15)
16+
project(cockroachdb_demo CXX)
17+
18+
set(CMAKE_CXX_STANDARD 17)
19+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
20+
21+
# Use conda environment paths
22+
if(DEFINED ENV{CONDA_PREFIX})
23+
list(APPEND CMAKE_PREFIX_PATH "$ENV{CONDA_PREFIX}")
24+
endif()
25+
26+
find_package(Arrow REQUIRED)
27+
28+
add_executable(cockroachdb_demo main.cpp)
29+
30+
if(DEFINED ENV{CONDA_PREFIX})
31+
target_include_directories(cockroachdb_demo PRIVATE $ENV{CONDA_PREFIX}/include)
32+
target_link_directories(cockroachdb_demo PRIVATE $ENV{CONDA_PREFIX}/lib)
33+
endif()
34+
35+
target_link_libraries(cockroachdb_demo
36+
adbc_driver_manager
37+
Arrow::arrow_shared
38+
)
39+
40+
target_compile_options(cockroachdb_demo PRIVATE
41+
-Wall
42+
-Werror
43+
)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Copyright 2025 Columnar Technologies Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
CXXFLAGS = -std=c++17 -Wall -Werror -I$(CONDA_PREFIX)/include
16+
LIBS = -L$(CONDA_PREFIX)/lib -ladbc_driver_manager -larrow -Wl,-rpath,$(CONDA_PREFIX)/lib
17+
18+
cockroachdb_demo: main.cpp
19+
$(CXX) $(CXXFLAGS) -o cockroachdb_demo main.cpp $(LIBS)
20+
21+
clean:
22+
rm -f cockroachdb_demo
23+
24+
.PHONY: clean
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<!--
2+
Copyright 2025 Columnar Technologies Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
-->
16+
17+
# Connecting C++ and CockroachDB with ADBC
18+
19+
## Instructions
20+
21+
> [!TIP]
22+
> If you already have a CockroachDB instance running, skip the steps to set up and clean up CockroachDB.
23+
24+
### Prerequisites
25+
26+
1. [Install dbc](https://docs.columnar.tech/dbc/getting_started/installation/)
27+
28+
2. [Install miniforge](https://github.com/conda-forge/miniforge)
29+
30+
3. Create and activate a new environment with the required C++ libraries:
31+
32+
```sh
33+
mamba create -n adbc-cpp -c conda-forge cmake compilers libadbc-driver-manager libarrow
34+
35+
# Initialize mamba in your shell if not already done
36+
eval "$(mamba shell hook --shell zsh)"
37+
mamba activate adbc-cpp
38+
```
39+
40+
(`cmake` is only needed if you use CMake to build the C++ program below.)
41+
42+
### Set up CockroachDB
43+
44+
1. [Install Docker](https://docs.docker.com/get-started/get-docker/)
45+
46+
2. Start a CockroachDB instance:
47+
48+
```sh
49+
docker run -d --rm \
50+
--env COCKROACH_DATABASE=db \
51+
--env COCKROACH_USER=username \
52+
--env COCKROACH_PASSWORD=password \
53+
--name=roach-single \
54+
-p 26257:26257 \
55+
-p 8080:8080 \
56+
cockroachdb/cockroach:v25.4.2 start-single-node
57+
```
58+
59+
### Connect to CockroachDB
60+
61+
1. Install the PostgreSQL ADBC driver:
62+
63+
```sh
64+
dbc install --level user postgresql
65+
```
66+
67+
2. Customize the C++ program `main.cpp` as needed
68+
- Change the connection arguments in the `AdbcDatabaseSetOption()` calls
69+
- Format `uri` according to the [connection URI format used by PostgreSQL](https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING-URIS), or keep it as is
70+
- If you changed which database you're connecting to, also change the SQL SELECT statement in `AdbcStatementSetSqlQuery()`
71+
72+
3. Build and run the C++ program:
73+
74+
Using Make:
75+
```sh
76+
make
77+
./cockroachdb_demo
78+
```
79+
80+
Or using CMake:
81+
```sh
82+
cmake -B build
83+
cmake --build build
84+
./build/cockroachdb_demo
85+
```
86+
87+
### Clean up
88+
89+
1. Clean build artifacts:
90+
91+
Using Make:
92+
```sh
93+
make clean
94+
```
95+
96+
Using CMake:
97+
```sh
98+
rm -rf build
99+
```
100+
101+
2. Stop the Docker container running CockroachDB:
102+
103+
```sh
104+
docker stop roach-single
105+
```
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
// Copyright 2025 Columnar Technologies Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// For EXIT_SUCCESS
16+
#include <cstdlib>
17+
// For strerror
18+
#include <cstring>
19+
#include <iostream>
20+
21+
#include <arrow-adbc/adbc.h>
22+
#include <arrow-adbc/adbc_driver_manager.h>
23+
#include <arrow/c/bridge.h>
24+
#include <arrow/record_batch.h>
25+
26+
// Error-checking helper for ADBC calls.
27+
// Assumes that there is an AdbcError named `error` in scope.
28+
#define CHECK_ADBC(EXPR) \
29+
if (AdbcStatusCode status = (EXPR); status != ADBC_STATUS_OK) { \
30+
if (error.message != nullptr) { \
31+
std::cerr << error.message << std::endl; \
32+
} \
33+
return EXIT_FAILURE; \
34+
}
35+
36+
// Error-checking helper for ArrowArrayStream.
37+
#define CHECK_STREAM(STREAM, EXPR) \
38+
if (int status = (EXPR); status != 0) { \
39+
std::cerr << "(" << std::strerror(status) << "): "; \
40+
const char *message = (STREAM).get_last_error(&(STREAM)); \
41+
if (message != nullptr) { \
42+
std::cerr << message << std::endl; \
43+
} else { \
44+
std::cerr << "(no error message)" << std::endl; \
45+
} \
46+
return EXIT_FAILURE; \
47+
}
48+
49+
int main() {
50+
AdbcError error = {};
51+
52+
AdbcDatabase database = {};
53+
CHECK_ADBC(AdbcDatabaseNew(&database, &error));
54+
55+
CHECK_ADBC(AdbcDatabaseSetOption(&database, "driver", "postgresql", &error));
56+
CHECK_ADBC(AdbcDatabaseSetOption(&database, "uri",
57+
"postgresql://username:password@localhost:26257/db", &error));
58+
CHECK_ADBC(AdbcDriverManagerDatabaseSetLoadFlags(
59+
&database, ADBC_LOAD_FLAG_DEFAULT, &error));
60+
CHECK_ADBC(AdbcDatabaseInit(&database, &error));
61+
62+
AdbcConnection connection = {};
63+
CHECK_ADBC(AdbcConnectionNew(&connection, &error));
64+
CHECK_ADBC(AdbcConnectionInit(&connection, &database, &error));
65+
66+
AdbcStatement statement = {};
67+
CHECK_ADBC(AdbcStatementNew(&connection, &statement, &error));
68+
69+
struct ArrowArrayStream stream = {};
70+
int64_t rows_affected = -1;
71+
72+
CHECK_ADBC(
73+
AdbcStatementSetOption(&statement, "adbc.postgresql.use_copy", "false", &error));
74+
CHECK_ADBC(
75+
AdbcStatementSetSqlQuery(&statement, "SELECT version()", &error));
76+
CHECK_ADBC(
77+
AdbcStatementExecuteQuery(&statement, &stream, &rows_affected, &error));
78+
79+
// Import stream as record batch reader
80+
auto maybe_reader = arrow::ImportRecordBatchReader(&stream);
81+
if (!maybe_reader.ok()) {
82+
std::cerr << "Failed to import record batch reader: "
83+
<< maybe_reader.status().message() << std::endl;
84+
return 1;
85+
}
86+
87+
auto reader = maybe_reader.ValueOrDie();
88+
89+
while (true) {
90+
auto maybe_batch = reader->Next();
91+
if (!maybe_batch.ok()) {
92+
std::cerr << "Error reading batch: " << maybe_batch.status().message()
93+
<< std::endl;
94+
return 1;
95+
}
96+
97+
auto batch = maybe_batch.ValueOrDie();
98+
if (!batch) {
99+
break;
100+
}
101+
102+
std::cout << batch->ToString() << std::endl;
103+
}
104+
105+
CHECK_ADBC(AdbcStatementRelease(&statement, &error));
106+
CHECK_ADBC(AdbcConnectionRelease(&connection, &error));
107+
CHECK_ADBC(AdbcDatabaseRelease(&database, &error));
108+
109+
return EXIT_SUCCESS;
110+
}

go/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ Simple Go examples showing how to use ADBC to connect, run a query, and return t
3939
- TiDB
4040
- Vitess
4141
- PostgreSQL
42+
- CedarDB
4243
- Citus
44+
- CockroachDB
4345
- CrateDB
4446
- Neon
4547
- ParadeDB

go/postgresql/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ This directory contains examples showing how to use ADBC to connect Go applicati
2222

2323
Any open source tool or vendor product that implements PostgreSQL frontend/backend protocol should work with the ADBC driver for PostgreSQL. The examples included here focus on the following systems:
2424

25+
- CedarDB
2526
- Citus
27+
- CockroachDB
2628
- CrateDB
2729
- Neon
2830
- ParadeDB

0 commit comments

Comments
 (0)