未验证 提交 de5047f4 编写于 作者: S slguan 提交者: GitHub

Merge pull request #821 from taosdata/feature/lihui

Feature/lihui
...@@ -392,15 +392,64 @@ curl -u username:password -d '<SQL>' <ip>:<PORT>/rest/sql ...@@ -392,15 +392,64 @@ curl -u username:password -d '<SQL>' <ip>:<PORT>/rest/sql
## Go Connector ## Go Connector
TDengine提供了GO驱动程序“taosSql”包。taosSql驱动包是基于GO的“database/sql/driver”接口的实现。用户可在安装后的/usr/local/taos/connector/go目录获得GO的客户端驱动程序。用户需将驱动包/usr/local/taos/connector/go/src/taosSql目录拷贝到应用程序工程的src目录下。然后在应用程序中导入驱动包,就可以使用“database/sql”中定义的接口访问TDengine: #### 安装TDengine
Go的链接器使用了到了 libtaos.so 和taos.h,因此,在使用Go连接器之前,需要在程序运行的机器上安装TDengine以获得相关的驱动文件。
#### Go语言引入package
TDengine提供了GO驱动程序“taosSql”包。taosSql驱动包是基于GO的“database/sql/driver”接口的实现。用户可以通过`go get`命令来获取驱动包。
```sh
go get github.com/taosdata/TDengine/src/connector/go/src/taosSql
```
然后在应用程序中导入驱动包,就可以使用“database/sql”中定义的接口访问TDengine:
```Go ```Go
import ( import (
"database/sql" "database/sql"
_ "taosSql" _ "github.com/taosdata/TDengine/src/connector/go/src/taosSql"
) )
``` ```
taosSql驱动包内采用cgo模式,调用了TDengine的C/C++同步接口,与TDengine进行交互,因此,在数据库操作执行完成之前,客户端应用将处于阻塞状态。单个数据库连接,在同一时刻只能有一个线程调用API。客户应用可以建立多个连接,进行多线程的数据写入或查询处理。 taosSql驱动包内采用cgo模式,调用了TDengine的C/C++同步接口,与TDengine进行交互,因此,在数据库操作执行完成之前,客户端应用将处于阻塞状态。单个数据库连接,在同一时刻只能有一个线程调用API。客户应用可以建立多个连接,进行多线程的数据写入或查询处理。
更多使用的细节,请参考下载目录中的示例源码。 #### Go语言使用参考
在Go程序中使用TDengine写入方法大致可以分为以下几步
1. 打开TDengine数据库链接
首先需要调用sql包中的Open方法,打开数据库,并获得db对象
```go
db, err := sql.Open(taosDriverName, dbuser+":"+dbpassword+"@/tcp("+daemonUrl+")/"+dbname)
if err != nil {
log.Fatalf("Open database error: %s\n", err)
}
defer db.Close()
```
其中参数为
- taosDataname: 涛思数据库的名称,其值为字符串"taosSql"
- dbuser和dbpassword: 链接TDengine的用户名和密码,缺省为root和taosdata,类型为字符串
- daemonUrl: 为TDengine的地址,其形式为`ip address:port`形式,port填写缺省值0即可。例如:"116.118.24.71:0"
- dbname:TDengine中的database名称,通过`create database`创建的数据库。如果为空则在后续的写入和查询操作必须通过”数据库名.超级表名或表名“的方式指定数据库名
2. 创建数据库
打开TDengine数据库连接后,首选需要创建数据库。基本用法和直接在TDengine客户端shell下一样,通过create database + 数据库名的方法来创建。
```go
db, err := sql.Open(taosDriverName, dbuser+":"+dbpassword+"@/tcp("+daemonUrl+")/")
if err != nil {
log.Fatalf("Open database error: %s\n", err)
}
defer db.Close()
//准备创建数据库语句
sqlcmd := fmt.Sprintf("create database if not exists %s", dbname)
//执行语句并检查错误
_, err = db.Exec(sqlcmd)
if err != nil {
log.Fatalf("Create database error: %s\n", err)
}
```
3. 创建表、写入和查询数据
在创建好了数据库后,就可以开始创建表和写入查询数据了。这些操作的基本思路都是首先组装SQL语句,然后调用db.Exec执行,并检查错误信息和执行相应的处理。可以参考上面的样例代码
...@@ -63,7 +63,7 @@ debver="Version: "$tdengine_ver ...@@ -63,7 +63,7 @@ debver="Version: "$tdengine_ver
sed -i "2c$debver" ${pkg_dir}/DEBIAN/control sed -i "2c$debver" ${pkg_dir}/DEBIAN/control
#get taos version, then set deb name #get taos version, then set deb name
debname="tdengine-"${tdengine_ver}".deb" debname="TDengine-"${tdengine_ver}".deb"
# make deb package # make deb package
dpkg -b ${pkg_dir} $debname dpkg -b ${pkg_dir} $debname
......
...@@ -2,6 +2,9 @@ ...@@ -2,6 +2,9 @@
# #
# Generate rpm package for centos # Generate rpm package for centos
#set -e
#set -x
#curr_dir=$(pwd) #curr_dir=$(pwd)
compile_dir=$1 compile_dir=$1
output_dir=$2 output_dir=$2
...@@ -24,8 +27,25 @@ if command -v sudo > /dev/null; then ...@@ -24,8 +27,25 @@ if command -v sudo > /dev/null; then
csudo="sudo" csudo="sudo"
fi fi
function cp_rpm_package() {
local cur_dir
cd $1
cur_dir=$(pwd)
for dirlist in $(ls ${cur_dir}); do
if test -d ${dirlist}; then
cd ${dirlist}
cp_rpm_package ${cur_dir}/${dirlist}
cd ..
fi
if test -e ${dirlist}; then
cp ${cur_dir}/${dirlist} ${output_dir}/TDengine-${tdengine_ver}.rpm
fi
done
}
if [ -d ${pkg_dir} ]; then if [ -d ${pkg_dir} ]; then
${csudo} rm -rf ${pkg_dir} ${csudo} rm -rf ${pkg_dir}
fi fi
${csudo} mkdir -p ${pkg_dir} ${csudo} mkdir -p ${pkg_dir}
cd ${pkg_dir} cd ${pkg_dir}
...@@ -35,7 +55,8 @@ ${csudo} mkdir -p BUILD BUILDROOT RPMS SOURCES SPECS SRPMS ...@@ -35,7 +55,8 @@ ${csudo} mkdir -p BUILD BUILDROOT RPMS SOURCES SPECS SRPMS
${csudo} rpmbuild --define="_version ${tdengine_ver}" --define="_topdir ${pkg_dir}" --define="_compiledir ${compile_dir}" -bb ${spec_file} ${csudo} rpmbuild --define="_version ${tdengine_ver}" --define="_topdir ${pkg_dir}" --define="_compiledir ${compile_dir}" -bb ${spec_file}
# copy rpm package to output_dir, then clean temp dir # copy rpm package to output_dir, then clean temp dir
#echo "rmpbuild end, cur_dir: $(pwd) " #${csudo} cp -rf RPMS/* ${output_dir}
${csudo} cp -rf RPMS/* ${output_dir} cp_rpm_package ${pkg_dir}/RPMS
cd .. cd ..
${csudo} rm -rf ${pkg_dir} ${csudo} rm -rf ${pkg_dir}
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
%define cfg_install_dir /etc/taos %define cfg_install_dir /etc/taos
%define __strip /bin/true %define __strip /bin/true
Name: tdengine Name: TDengine
Version: %{_version} Version: %{_version}
Release: 3%{?dist} Release: 3%{?dist}
Summary: tdengine from taosdata Summary: tdengine from taosdata
......
...@@ -19,8 +19,8 @@ code_dir="${top_dir}/src" ...@@ -19,8 +19,8 @@ code_dir="${top_dir}/src"
release_dir="${top_dir}/release" release_dir="${top_dir}/release"
community_dir="${script_dir}/../../../community/src" community_dir="${script_dir}/../../../community/src"
package_name='linux' #package_name='linux'
install_dir="${release_dir}/taos-client-${version}-${package_name}-$(echo ${build_time}| tr ': ' -)" install_dir="${release_dir}/TDengine-client-${version}"
# Directories and files. # Directories and files.
bin_files="${build_dir}/bin/taos ${build_dir}/bin/taosdump ${script_dir}/remove_client.sh" bin_files="${build_dir}/bin/taos ${build_dir}/bin/taosdump ${script_dir}/remove_client.sh"
......
...@@ -16,8 +16,8 @@ build_dir="${compile_dir}/build" ...@@ -16,8 +16,8 @@ build_dir="${compile_dir}/build"
code_dir="${top_dir}/src" code_dir="${top_dir}/src"
release_dir="${top_dir}/release" release_dir="${top_dir}/release"
package_name='linux' #package_name='linux'
install_dir="${release_dir}/taos-${version}-${package_name}-$(echo ${build_time}| tr ': ' -)" install_dir="${release_dir}/TDengine-${version}"
# Directories and files. # Directories and files.
bin_files="${build_dir}/bin/taosd ${build_dir}/bin/taos ${build_dir}/bin/taosdemo ${build_dir}/bin/taosdump ${script_dir}/remove.sh" bin_files="${build_dir}/bin/taosd ${build_dir}/bin/taos ${build_dir}/bin/taosdemo ${build_dir}/bin/taosdump ${script_dir}/remove.sh"
......
...@@ -29,9 +29,19 @@ ...@@ -29,9 +29,19 @@
#elif defined(DARWIN) #elif defined(DARWIN)
char CLIENT_VERSION[] = "Welcome to the TDengine shell from mac, client version:%s "; char CLIENT_VERSION[] = "Welcome to the TDengine shell from mac, client version:%s ";
#else #else
char CLIENT_VERSION[] = "Welcome to the TDengine shell from linux, client version:%s "; #ifdef CLUSTER
char CLIENT_VERSION[] = "Welcome to the TDengine shell from linux, enterprise client version:%s ";
#else
char CLIENT_VERSION[] = "Welcome to the TDengine shell from linux, community client version:%s ";
#endif
#endif #endif
char SERVER_VERSION[] = "server version:%s\nCopyright (c) 2017 by TAOS Data, Inc. All rights reserved.\n\n";
#ifdef CLUSTER
char SERVER_VERSION[] = "enterprise server version:%s\nCopyright (c) 2017 by TAOS Data, Inc. All rights reserved.\n\n";
#else
char SERVER_VERSION[] = "community server version:%s\nCopyright (c) 2017 by TAOS Data, Inc. All rights reserved.\n\n";
#endif
char PROMPT_HEADER[] = "taos> "; char PROMPT_HEADER[] = "taos> ";
char CONTINUE_PROMPT[] = " -> "; char CONTINUE_PROMPT[] = " -> ";
int prompt_size = 6; int prompt_size = 6;
......
...@@ -105,6 +105,15 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state) { ...@@ -105,6 +105,15 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state) {
static struct argp argp = {options, parse_opt, args_doc, doc}; static struct argp argp = {options, parse_opt, args_doc, doc};
void shellParseArgument(int argc, char *argv[], struct arguments *arguments) { void shellParseArgument(int argc, char *argv[], struct arguments *arguments) {
char verType[32] = {0};
#ifdef CLUSTER
sprintf(verType, "enterprise version: %s\n", version);
#else
sprintf(verType, "community version: %s\n", version);
#endif
argp_program_version = verType;
argp_parse(&argp, argc, argv, 0, 0, arguments); argp_parse(&argp, argc, argv, 0, 0, arguments);
if (arguments->abort) { if (arguments->abort) {
error(10, 0, "ABORTED"); error(10, 0, "ABORTED");
......
...@@ -55,7 +55,12 @@ int main(int argc, char *argv[]) { ...@@ -55,7 +55,12 @@ int main(int argc, char *argv[]) {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} else if (strcmp(argv[i], "-V") == 0) { } else if (strcmp(argv[i], "-V") == 0) {
printf("version: %s compatible_version: %s\n", version, compatible_version); #ifdef CLUSTER
printf("enterprise version: %s compatible_version: %s\n", version, compatible_version);
#else
printf("community version: %s compatible_version: %s\n", version, compatible_version);
#endif
printf("gitinfo: %s\n", gitinfo); printf("gitinfo: %s\n", gitinfo);
printf("buildinfo: %s\n", buildinfo); printf("buildinfo: %s\n", buildinfo);
return 0; return 0;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册