-
Notifications
You must be signed in to change notification settings - Fork 161
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 () |
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() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
libabsl-dev
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See https://github.com/ClickHouse/clickhouse-cpp/blob/master/.github/workflows/linux.yml
There was a problem hiding this comment.
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 .
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that readme is for the clickhouse-cpp, why not make it clear for all the cases?