| <!-- |
| |
| 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. |
| |
| --> |
| |
| ## Go Native API |
| |
| ### Dependencies |
| |
| * golang >= 1.13 |
| * make >= 3.0 |
| * curl >= 7.1.1 |
| * thrift 0.15.0 |
| * Linux、Macos or other unix-like systems |
| * Windows+bash (WSL、cygwin、Git Bash) |
| |
| |
| |
| |
| ### Installation |
| |
| * go mod |
| |
| ```sh |
| export GO111MODULE=on |
| export GOPROXY=https://goproxy.io |
| |
| mkdir session_example && cd session_example |
| |
| curl -o session_example.go -L https://github.com/apache/iotdb-client-go/raw/main/example/session_example.go |
| |
| go mod init session_example |
| go run session_example.go |
| ``` |
| |
| * GOPATH |
| |
| ```sh |
| # get thrift 0.13.0 |
| go get github.com/apache/thrift |
| cd $GOPATH/src/github.com/apache/thrift |
| git checkout 0.13.0 |
| |
| mkdir -p $GOPATH/src/iotdb-client-go-example/session_example |
| cd $GOPATH/src/iotdb-client-go-example/session_example |
| curl -o session_example.go -L https://github.com/apache/iotdb-client-go/raw/main/example/session_example.go |
| go run session_example.go |
| ``` |
| |
| ### How to Use the SessionPool |
| SessionPool is a wrapper of a Session Set. Using SessionPool, the user do not need to consider how to reuse a session connection. |
| If there is no available connections and the pool reaches its max size, the all methods will hang until there is a available connection. |
| The PutBack method must be called after use |
| |
| #### New sessionPool |
| |
| ```go |
| config := &client.PoolConfig{ |
| Host: host, |
| Port: port, |
| UserName: user, |
| Password: password, |
| } |
| sessionPool = client.NewSessionPool(config, 3, 60000, 60000, false) |
| ``` |
| |
| #### Get session through sessionPool, putback after use |
| |
| set storage group |
| |
| ```go |
| session, err := sessionPool.GetSession() |
| defer sessionPool.PutBack(session) |
| if err == nil { |
| session.SetStorageGroup(sg) |
| } |
| ``` |
| |
| query statement |
| |
| ```go |
| var timeout int64 = 1000 |
| session, err := sessionPool.GetSession() |
| defer sessionPool.PutBack(session) |
| if err != nil { |
| log.Print(err) |
| return |
| } |
| sessionDataSet, err := session.ExecuteQueryStatement(sql, &timeout) |
| if err == nil { |
| defer sessionDataSet.Close() |
| printDataSet1(sessionDataSet) |
| } else { |
| log.Println(err) |
| } |
| ``` |