Skip to content
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

Update readme to show how to build and run hello world #144

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ C++ client for [ClickHouse](https://clickhouse.com/).
* Int128
* UUID

## Requirement

You need gcc cmake libabsl-dev to build this project.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Should work without libabsl-dev
  2. gcc version 7 at least (clang >= 6 also supperted, after Add support for MSVC #131 MSVC should work also).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To build with openssl libssl-dev is also needed.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello world does not need https i think. or am i wrong?

I just finish running the hello world for me ,so it's time to share how to run example code , may help who want to play .

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello world does not need https i think

that readme is for the clickhouse-cpp, why not make it clear for all the cases?


* gcc
* cmake
* libabsl-dev

For ubuntu user, you can install them via command:
```bash
apt-get install -y cmake gcc libabsl-dev
```

## Building

```sh
Expand All @@ -30,6 +43,12 @@ $ cd build
$ cmake .. [-DBUILD_TESTS=ON]
$ make
```
if you want to install system lib directory, you may run
```bash
$ sudo make install
```
Then the include and lib will install into system /usr/local/{lib,include} directory


## Example

Expand Down Expand Up @@ -75,3 +94,14 @@ client.Select("SELECT id, name FROM test.numbers", [] (const Block& block)
/// Delete table.
client.Execute("DROP TABLE test.numbers");
```

If you want to build the example to run hello world, try to build with following command
```
cd tests/helloworld
mkdir build
cd build
cmake ..
make
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/lib"
./helloworld
```
15 changes: 15 additions & 0 deletions tests/helloworld/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
ADD_EXECUTABLE (helloworld
main.cpp
)

TARGET_LINK_LIBRARIES (helloworld
clickhouse-cpp-lib
)

IF (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
# there is a problem with __builtin_mul_overflow call at link time
# the error looks like: ... undefined reference to `__muloti4' ...
# caused by clang bug https://bugs.llvm.org/show_bug.cgi?id=16404
# explicit linking to compiler-rt allows to workaround the problem
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --rtlib=compiler-rt")
ENDIF ()
91 changes: 91 additions & 0 deletions tests/helloworld/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#include <clickhouse/client.h>
#include <clickhouse/error_codes.h>
#include <clickhouse/types/type_parser.h>

#include <stdexcept>
#include <iostream>
#include <cmath>

#if defined(_MSC_VER)
# pragma warning(disable : 4996)
#endif

using namespace clickhouse;
using namespace std;

std::string getEnvOrDefault(const std::string& env, const std::string& default_val)
{
const char* v = std::getenv(env.c_str());
return v ? v : default_val;
}

inline void PrintBlock(const Block& block) {
for (Block::Iterator bi(block); bi.IsValid(); bi.Next()) {
std::cout << bi.Name() << " ";
}
std::cout << std::endl;

for (size_t i = 0; i < block.GetRowCount(); ++i) {
std::cout << (*block[0]->As<ColumnUInt64>())[i] << " "
<< (*block[1]->As<ColumnString>())[i] << "\n";
}
}


inline void GenericExample(Client& client) {
/// Create a table.
client.Execute("CREATE TEMPORARY TABLE IF NOT EXISTS test_client (id UInt64, name String)");

/// Insert some values.
{
Block block;

auto id = std::make_shared<ColumnUInt64>();
id->Append(1);
id->Append(7);

auto name = std::make_shared<ColumnString>();
name->Append("one");
name->Append("seven");

block.AppendColumn("id" , id);
block.AppendColumn("name", name);

client.Insert("test_client", block);
}

/// Select values inserted in the previous step.
client.Select("SELECT id, name FROM test_client", [](const Block& block)
{
PrintBlock(block);
}
);

/// Delete table.
client.Execute("DROP TEMPORARY TABLE test_client");
}


static void RunTests(Client& client) {
GenericExample(client);
}

int main() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the simple directory source code not that simple, you can see so many code blocks.
Shall we start from very basic hello world example?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK for me. Just want to understand your motivation.

try {
{
Client client(ClientOptions()
.SetHost( getEnvOrDefault("CLICKHOUSE_HOST", "localhost"))
.SetPort( std::stoi(getEnvOrDefault("CLICKHOUSE_PORT", "9000")))
.SetUser( getEnvOrDefault("CLICKHOUSE_USER", "default"))
.SetPassword( getEnvOrDefault("CLICKHOUSE_PASSWORD", ""))
.SetDefaultDatabase(getEnvOrDefault("CLICKHOUSE_DB", "default"))
.SetPingBeforeQuery(true));
RunTests(client);
}

} catch (const std::exception& e) {
std::cerr << "exception : " << e.what() << std::endl;
}

return 0;
}