This is an automated email from the ASF dual-hosted git repository. colinlee pushed a commit to branch colin_example_c_cpp in repository https://gitbox.apache.org/repos/asf/tsfile.git
commit 30f93c3d2fd2301e7678a1e16cdb4b1c42729a4e Author: ColinLee <shuoli...@163.com> AuthorDate: Tue Feb 25 23:06:57 2025 +0800 add example. --- cpp/examples/CMakeLists.txt | 2 + cpp/examples/README.md | 77 ++++++++++++++++++++++++++++++++ cpp/examples/build.sh | 2 +- cpp/examples/cpp_examples/demo_read.cpp | 50 +++++++++++++++++++-- cpp/examples/cpp_examples/demo_write.cpp | 2 - cpp/src/cwrapper/tsfile_cwrapper.h | 4 +- 6 files changed, 128 insertions(+), 9 deletions(-) diff --git a/cpp/examples/CMakeLists.txt b/cpp/examples/CMakeLists.txt index b0392c2d..fa868671 100644 --- a/cpp/examples/CMakeLists.txt +++ b/cpp/examples/CMakeLists.txt @@ -20,6 +20,8 @@ cmake_minimum_required(VERSION 3.10) project(examples) message("Running in exampes directory") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") + # TsFile include dir set(SDK_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/../src/) message("SDK_INCLUDE_DIR: ${SDK_INCLUDE_DIR}") diff --git a/cpp/examples/README.md b/cpp/examples/README.md new file mode 100644 index 00000000..85d11494 --- /dev/null +++ b/cpp/examples/README.md @@ -0,0 +1,77 @@ +<!-- + + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you 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. + +--> + +# TsFile Reader/Writer Integration Guide + +## 1. Building TSFile Shared Library + +Build Methods (Choose either approach) + +### Method 1: Maven Build +Execute from the project root directory: + +```BASH +mvn package -P with-cpp clean verify +``` +Output location: cpp/target/build/lib + +### Method 2: Script Build +Run the build script: + +```BASH +bash build.sh +``` +Output location: cpp/build/Release/lib + +## Project Configuration +### CMake Integration + +Add to your CMakeLists.txt: + +```CMAKE +find_library(TSFILE_LIB NAMES tsfile PATHS ${SDK_LIB} REQUIRED) +target_link_libraries(your_target ${TSFILE_LIB}) +``` + +Note: Set ${SDK_LIB} to your TSFile library directory. + +## 3. Implementation Examples + +### Directory Structure +```TEXT + ├── CMakeLists.txt + ├── c_examples/ + │ ├── demo_write.c # C write implementation + │ └── demo_read.c # C read implementation + ├── cpp_examples/ + │ ├── demo_write.cpp # C++ write implementation + │ └── demo_read.cpp # C++ read implementation + └── examples.cc # Combined use cases +``` + +### Code References +Writing TSFiles:\ +C: c_examples/demo_write.c\ +C++: cpp_examples/demo_write.cpp + +Reading TSFiles:\ +C: c_examples/demo_read.c\ +C++: cpp_examples/demo_read.cpp \ No newline at end of file diff --git a/cpp/examples/build.sh b/cpp/examples/build.sh index e65ea81b..36cf44ae 100644 --- a/cpp/examples/build.sh +++ b/cpp/examples/build.sh @@ -28,5 +28,5 @@ else fi -cmake ../../ -DUSE_CPP11=$use_cpp11 -DBUILD_TYPE=$build_type +cmake ../../ -DBUILD_TYPE=$build_type make \ No newline at end of file diff --git a/cpp/examples/cpp_examples/demo_read.cpp b/cpp/examples/cpp_examples/demo_read.cpp index fabbc107..08424b9b 100644 --- a/cpp/examples/cpp_examples/demo_read.cpp +++ b/cpp/examples/cpp_examples/demo_read.cpp @@ -23,7 +23,7 @@ #include "../c_examples/c_examples.h" #include "cpp_examples.h" -std::string field_to_string(storage::Field *value) { +std::string field_to_string(storage::Field* value) { if (value->type_ == common::TEXT) { return std::string(value->value_.sval_); } else { @@ -56,11 +56,53 @@ std::string field_to_string(storage::Field *value) { } int demo_read() { - ERRNO code = 0 + ERRNO code = 0; std::string table_name = "table1"; storage::TsFileReader reader; reader.open("test.tsfile"); storage::ResultSet* ret = nullptr; - code = reader.query(table_name, {"id1", "id2", "s1"}, 0, 100, ret); - + std::vector<std::string> columns; + columns.push_back("id1"); + columns.push_back("id2"); + columns.push_back("s1"); + code = reader.query(table_name, columns, 0, 100, ret); + auto metadata = ret->get_metadata(); + int column_num = metadata->get_column_count(); + for (int i = 0; i < column_num; i++) { + std::cout << "column name: " << metadata->get_column_name(i) + << std::endl; + std::cout << "column type: " << metadata->get_column_type(i) + << std::endl; + } + bool has_next = false; + while (ret->next(has_next) && has_next) { + Timestamp timestamp = ret->get_value<Timestamp>(1); + for (int i = 0; i < column_num; i++) { + if (ret->is_null(i)) { + std::cout << "null" << std::endl; + } else { + switch (metadata->get_column_type(i)) { + case common::INT32: + std::cout << ret->get_value<int32_t>(i) << std::endl; + break; + case common::INT64: + std::cout << ret->get_value<int64_t>(i) << std::endl; + break; + case common::FLOAT: + std::cout << ret->get_value<float>(i) << std::endl; + break; + case common::DOUBLE: + std::cout << ret->get_value<double>(i) << std::endl; + break; + case common::STRING: + std::cout << ret->get_value<std::string>(i) + << std::endl; + break; + default:; + } + } + } + } + ret->close(); + reader.close(); } diff --git a/cpp/examples/cpp_examples/demo_write.cpp b/cpp/examples/cpp_examples/demo_write.cpp index 6379a025..89879e8d 100644 --- a/cpp/examples/cpp_examples/demo_write.cpp +++ b/cpp/examples/cpp_examples/demo_write.cpp @@ -29,8 +29,6 @@ using namespace storage; -long getNowTime() { return time(nullptr); } - int demo_write() { std::string table_name = "table1"; WriteFile file; diff --git a/cpp/src/cwrapper/tsfile_cwrapper.h b/cpp/src/cwrapper/tsfile_cwrapper.h index 08b9cfa2..82ac1772 100644 --- a/cpp/src/cwrapper/tsfile_cwrapper.h +++ b/cpp/src/cwrapper/tsfile_cwrapper.h @@ -20,8 +20,7 @@ #ifndef SRC_CWRAPPER_TSFILE_CWRAPPER_H_ #define SRC_CWRAPPER_TSFILE_CWRAPPER_H_ -#include <cstdint> -#include <iostream> +#include <sys/stat.h> typedef enum { TS_DATATYPE_BOOLEAN = 0, @@ -31,6 +30,7 @@ typedef enum { TS_DATATYPE_DOUBLE = 4, TS_DATATYPE_TEXT = 5, TS_DATATYPE_VECTOR = 6, + TS_DATATYPE_STRING = 11, TS_DATATYPE_NULL_TYPE = 254, TS_DATATYPE_INVALID = 255 } TSDataType;