index.md 7.5 KB
Newer Older
D
dingbo 已提交
1
---
G
gccgdb1234 已提交
2 3 4
sidebar_label: Connect
title: Connect to TDengine
description: "This document explains how to establish connection to TDengine, and briefly introduce how to install and use TDengine connectors."
D
dingbo 已提交
5 6 7 8
---

import Tabs from "@theme/Tabs";
import TabItem from "@theme/TabItem";
G
gccgdb1234 已提交
9 10 11 12 13 14 15 16 17 18 19 20
import ConnJava from "./\_connect_java.mdx";
import ConnGo from "./\_connect_go.mdx";
import ConnRust from "./\_connect_rust.mdx";
import ConnNode from "./\_connect_node.mdx";
import ConnPythonNative from "./\_connect_python.mdx";
import ConnCSNative from "./\_connect_cs.mdx";
import ConnC from "./\_connect_c.mdx";
import ConnR from "./\_connect_r.mdx";
import InstallOnWindows from "../../14-reference/03-connector/\_linux_install.mdx";
import InstallOnLinux from "../../14-reference/03-connector/\_windows_install.mdx";
import VerifyLinux from "../../14-reference/03-connector/\_verify_linux.mdx";
import VerifyWindows from "../../14-reference/03-connector/\_verify_windows.mdx";
D
dingbo 已提交
21

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
22
Any application programs running on any kind of platforms can access TDengine through the REST API provided by TDengine. For the details, please refer to [REST API](/reference/rest-api/). Besides, application programs can use the connectors of multiple programming languages to access TDengine, including C/C++, Java, Python, Go, Node.js, C#, and Rust. This chapter describes how to establish connection to TDengine and briefly introduce how to install and use connectors. For details about the connectors, please refer to [Connectors](/reference/connector/)
D
dingbo 已提交
23

G
gccgdb1234 已提交
24
## Establish Connection
D
dingbo 已提交
25

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
26
There are two ways for a connector to establish connections to TDengine:
D
dingbo 已提交
27

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
28 29
1. Connection through the REST API provided by taosAdapter component, this way is called "REST connection" hereinafter.
2. Connection through the TDengine application driver taosc, this way is called "Native connection" hereinafter.
D
dingbo 已提交
30

G
gccgdb1234 已提交
31
Either way, same or similar APIs are provided by connectors to access database or execute SQL statements, no obvious difference can be observed.
D
dingbo 已提交
32

G
gccgdb1234 已提交
33
Key differences:
D
dingbo 已提交
34

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
35 36
1. With REST connection, it's not necessary to install TDengine application driver taosc, it's more friendly for cross-platform with the cost of 30% performance downgrade. When taosc has an upgrade, application does not need to make changes. 
2. With native connection, full compatibility of TDengine can be utilized, like [Parameter Binding](/reference/connector/cpp#Parameter Binding-api), [Subscription](reference/connector/cpp#Subscription), etc. But taosc has to be installed, some platforms may not be supported.
D
dingbo 已提交
37

G
gccgdb1234 已提交
38
## Install Client Driver taosc
D
dingbo 已提交
39

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
40
If choosing to use native connection and the application is not on the same host as TDengine server, TDengine application driver taosc needs to be installed on the host where the application is. If choosing to use REST connection or the application is on the same host as server side, this step can be skipped. It's better to use same version of taosc as the server.
D
dingbo 已提交
41

G
gccgdb1234 已提交
42
### Install
D
dingbo 已提交
43 44 45 46 47 48 49 50 51 52

<Tabs defaultValue="linux" groupId="os">
  <TabItem value="linux" label="Linux">
    <InstallOnWindows />
  </TabItem>
  <TabItem value="windows" label="Windows">
    <InstallOnLinux />
  </TabItem>
</Tabs>

G
gccgdb1234 已提交
53
### Verify
D
dingbo 已提交
54

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
55
After the above installation and configuration are done and making sure TDengine service is already started and in service, the TDengine command line interface `taos` can be launched to access TDengine.以
D
dingbo 已提交
56 57 58 59 60 61 62 63 64 65

<Tabs defaultValue="linux" groupId="os">
  <TabItem value="linux" label="Linux">
    <VerifyLinux />
  </TabItem>
  <TabItem value="windows" label="Windows">
    <VerifyWindows />
  </TabItem>
</Tabs>

G
gccgdb1234 已提交
66
## Install Connectors
D
dingbo 已提交
67 68 69 70

<Tabs groupId="lang">
<TabItem label="Java" value="java">
  
G
gccgdb1234 已提交
71
If `maven` is used to manage the projects, what needs to be done is only adding below dependency in `pom.xml`.
D
dingbo 已提交
72 73 74 75 76 77 78 79 80 81 82 83

```xml
<dependency>
  <groupId>com.taosdata.jdbc</groupId>
  <artifactId>taos-jdbcdriver</artifactId>
  <version>2.0.38</version>
</dependency>
```

</TabItem>
<TabItem label="Python" value="python">

G
gccgdb1234 已提交
84
Install from PyPI using `pip`:
D
dingbo 已提交
85 86 87 88 89

```
pip install taospy
```

G
gccgdb1234 已提交
90
Install from Git URL:
D
dingbo 已提交
91 92 93 94 95 96 97 98

```
pip install git+https://github.com/taosdata/taos-connector-python.git
```

</TabItem>
<TabItem label="Go" value="go">

G
gccgdb1234 已提交
99
Just need to add `driver-go` dependency in `go.mod` .
D
dingbo 已提交
100 101 102 103 104 105 106 107 108 109

```go-mod title=go.mod
module goexample

go 1.17

require github.com/taosdata/driver-go/v2 develop
```

:::note
G
gccgdb1234 已提交
110
`driver-go` uses `cgo` to wrap the APIs provided by taosc, while `cgo` needs `gcc` to compile source code in C language, so please make sure you have proper `gcc` on your system.
D
dingbo 已提交
111 112 113 114 115 116

:::

</TabItem>
<TabItem label="Rust" value="rust">

G
gccgdb1234 已提交
117
Just need to add `libtaos` dependency in `Cargo.toml`.
D
dingbo 已提交
118 119 120 121 122 123 124

```toml title=Cargo.toml
[dependencies]
libtaos = { version = "0.4.2"}
```

:::info
G
gccgdb1234 已提交
125
Rust connector uses different features to distinguish the way to establish connection. To establish REST connection, please enable `rest` feature.
D
dingbo 已提交
126 127 128 129 130 131 132 133 134 135

```toml
libtaos = { version = "*", features = ["rest"] }
```

:::

</TabItem>
<TabItem label="Node.js" value="node">

G
gccgdb1234 已提交
136
Node.js connector provides different ways of establishing connections by providing different packages.
D
dingbo 已提交
137

G
gccgdb1234 已提交
138
1. Install Node.js Native Connector
D
dingbo 已提交
139

G
gccgdb1234 已提交
140 141 142
```
npm i td2.0-connector
```
D
dingbo 已提交
143 144

:::note
G
gccgdb1234 已提交
145 146
It's recommend to use Node whose version is between `node-v12.8.0` and `node-v13.0.0`.
:::
D
dingbo 已提交
147

G
gccgdb1234 已提交
148 149 150 151 152
2. Install Node.js REST Connector

```
npm i td2.0-rest-connector
```
D
dingbo 已提交
153 154 155 156

</TabItem>
<TabItem label="C#" value="csharp">

G
gccgdb1234 已提交
157
Just need to add the reference to [TDengine.Connector](https://www.nuget.org/packages/TDengine.Connector/) in the project configuration file.
D
dingbo 已提交
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176

```xml title=csharp.csproj {12}
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <StartupObject>TDengineExample.AsyncQueryExample</StartupObject>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="TDengine.Connector" Version="1.0.6" />
  </ItemGroup>

</Project>
```

G
gccgdb1234 已提交
177
Or add by `dotnet` command.
D
dingbo 已提交
178 179 180 181 182 183

```
dotnet add package TDengine.Connector
```

:::note
G
gccgdb1234 已提交
184
The sample code below are based on dotnet6.0, they may need to be adjusted if your dotnet version is not exactly same.
D
dingbo 已提交
185 186 187 188 189 190

:::

</TabItem>
<TabItem label="R" value="r">

G
gccgdb1234 已提交
191 192
1. Download [taos-jdbcdriver-version-dist.jar](https://repo1.maven.org/maven2/com/taosdata/jdbc/taos-jdbcdriver/2.0.38/).
2. Install the dependency package `RJDBC`
D
dingbo 已提交
193 194 195 196 197 198 199 200

```R
install.packages("RJDBC")
```

</TabItem>
<TabItem label="C" value="c">

G
gccgdb1234 已提交
201
If the client driver taosc is already installed, then the C connector is already available.
D
dingbo 已提交
202 203 204 205 206
<br/>

</TabItem>
</Tabs>

G
gccgdb1234 已提交
207
## Establish Connection
D
dingbo 已提交
208

G
gccgdb1234 已提交
209
Prior to establishing connection, please make sure TDengine is already running and accessible. The following sample code assumes TDengine is running on the same host as the client program, with FQDN configured to "localhost" and serverPort configured to "6030".
D
dingbo 已提交
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238

<Tabs groupId="lang" defaultValue="java">
  <TabItem label="Java" value="java">
    <ConnJava />
  </TabItem>
  <TabItem label="Python" value="python">
    <ConnPythonNative />
  </TabItem>
  <TabItem label="Go" value="go">
      <ConnGo />
  </TabItem>
  <TabItem label="Rust" value="rust">
    <ConnRust />
  </TabItem>
  <TabItem label="Node.js" value="node">
    <ConnNode />
  </TabItem>
  <TabItem label="C#" value="csharp">
    <ConnCSNative />
  </TabItem>
  <TabItem label="R" value="r">
    <ConnR/>
  </TabItem>
  <TabItem label="C" value="c">
    <ConnC />
  </TabItem>
</Tabs>

:::tip
G
gccgdb1234 已提交
239
If the connection fails, in most cases it's caused by improper configuration for FQDN or firewall. Please refer to the section "Unable to establish connection" in [FAQ](https://docs.taosdata.com/train-faq/faq).
D
dingbo 已提交
240 241

:::