docs.md 7.3 KB
Newer Older
1 2 3 4
# Connections with Other Tools

## <a class="anchor" id="grafana"></a> Grafana

5
TDengine can be quickly integrated with [Grafana](https://www.grafana.com/), an open source data visualization system, to build a data monitoring and alarming system. The whole process does not require any code to write. The contents of the data table in TDengine can be visually showed on DashBoard.
6 7 8

### Install Grafana

9
TDengine currently supports Grafana 6.2 and above. You can download and install the package from Grafana website according to the current operating system. The download address is as follows:
10 11 12 13 14

https://grafana.com/grafana/download.

### Configure Grafana

15 16 17
Download grafana plugin from <https://github.com/taosdata/grafanaplugin/releases/latest> .

```bash
18
GF_VERSION=3.1.3
19 20
wget https://github.com/taosdata/grafanaplugin/releases/download/v$GF_VERSION/tdengine-datasource-$GF_VERSION.zip
```
21 22 23 24

Taking Centos 7.2 as an example, just copy grafanaplugin directory to /var/lib/grafana/plugins directory and restart Grafana.

```bash
25
sudo unzip tdengine-datasource-$GF_VERSION.zip /var/lib/grafana/plugins/
26 27 28 29 30 31 32 33
```

### Use Grafana

#### Configure data source

You can log in the Grafana server (username/password:admin/admin) through localhost:3000, and add data sources through `Configuration -> Data Sources` on the left panel, as shown in the following figure:

34
![img](../images/connections/add_datasource1.jpg)
35 36 37

Click `Add data source` to enter the Add Data Source page, and enter TDengine in the query box to select Add, as shown in the following figure:

38
![img](../images/connections/add_datasource2.jpg)
39 40 41

Enter the data source configuration page and modify the corresponding configuration according to the default prompt:

42
![img](../images/connections/add_datasource3.jpg)
43 44 45 46 47 48 49

- Host: IP address of any server in TDengine cluster and port number of TDengine RESTful interface (6041), default  [http://localhost:6041](http://localhost:6041/)
- User: TDengine username.
- Password: TDengine user password.

Click `Save & Test` to test. Success will be prompted as follows:

50
![img](../images/connections/add_datasource4.jpg)
51 52 53 54 55

#### Create Dashboard

Go back to the home  to create Dashboard, and click `Add Query` to enter the panel query page:

56
![img](../images/connections/create_dashboard1.jpg)
57 58 59 60 61 62 63 64 65

As shown in the figure above, select the TDengine data source in Query, and enter the corresponding sql in the query box below to query. Details are as follows:

- INPUT SQL: Enter the statement to query (the result set of the SQL statement should be two columns and multiple rows), for example: `select avg(mem_system) from log.dn where ts >= $from and ts < $to interval($interval)` , where `from`, `to` and `interval` are built-in variables of the TDengine plug-in, representing the query range and time interval obtained from the Grafana plug-in panel. In addition to built-in variables, it is also supported to use custom template variables.
- ALIAS BY: You can set alias for the current queries.
- GENERATE SQL: Clicking this button will automatically replace the corresponding variable and generate the final statement to execute.

According to the default prompt, query the average system memory usage at the specified interval of the server where the current TDengine deployed in as follows:

66
![img](../images/connections/create_dashboard2.jpg)
67 68 69 70 71

> Please refer to Grafana [documents](https://grafana.com/docs/) for how to use Grafana to create the corresponding monitoring interface and for more about Grafana usage.

#### Import Dashboard

72
We provide a TDinsight dashboard (via Grafana dashboard id: [15167](https://grafana.com/grafana/dashboards/15167)) for TDengine cluster monitoring since TDengine 2.3.3.x . Please refer to [TDinsight User Manual](https://www.taosdata.com/en/documentation/tools/insight) for the details.
73

74
## <a class="anchor" id="matlab"></a> MATLAB
75

76
MATLAB can access data to the local workspace by connecting directly to the TDengine via the JDBC Driver provided in the installation package.
77

78
### JDBC Interface Adaptation of MATLAB
79

80
Several steps are required to adapt MATLAB to TDengine. Taking adapting MATLAB2017a on Windows10 as an example:
81 82 83

- Copy the file JDBCDriver-1.0.0-dist.ja*r* in TDengine package to the directory ${matlab_root}\MATLAB\R2017a\java\jar\toolbox
- Copy the file taos.lib in TDengine package to ${matlab root dir}\MATLAB\R2017a\lib\win64
84
- Add the .jar package just copied to the MATLAB classpath. Append the line below as the end of the file of ${matlab root dir}\MATLAB\R2017a\toolbox\local\classpath.txt
85 86 87 88 89 90 91 92 93
- ```
  $matlabroot/java/jar/toolbox/JDBCDriver-1.0.0-dist.jar
  ```

- Create a file called javalibrarypath.txt in directory ${user_home}\AppData\Roaming\MathWorks\MATLAB\R2017a_, and add the _taos.dll path in the file. For example, if the file taos.dll is in the directory of C:\Windows\System32,then add the following line in file javalibrarypath.txt:
- ```
  C:\Windows\System32
  ```

94
- ### Connect to TDengine in MATLAB to get data
95

96
After the above configured successfully, open MATLAB.
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154

- Create a connection:

```matlab
conn = database(db, root, taosdata, com.taosdata.jdbc.TSDBDriver, jdbc:TSDB://127.0.0.1:0/)
```

* Make a query:

```matlab
sql0 = [select * from tb]
data = select(conn, sql0);
```

* Insert a record:

```matlab
sql1 = [insert into tb values (now, 1)]
exec(conn, sql1)
```

For more detailed examples, please refer to the examples\Matlab\TDEngineDemo.m file in the package.

## <a class="anchor" id="r"></a> R 

R language supports connection to the TDengine database through the JDBC interface. First, you need to install the JDBC package of R language. Launch the R language environment, and then execute the following command to install the JDBC support library for R language:

```R
install.packages('RJDBC', repos='http://cran.us.r-project.org')
```

After installed, load the RJDBC package by executing `library('RJDBC')` command.

Then load the TDengine JDBC driver:

```R
drv<-JDBC("com.taosdata.jdbc.TSDBDriver","JDBCDriver-2.0.0-dist.jar", identifier.quote="\"")
```

If succeed, no error message will display. Then use the following command to try a database connection:

```R
conn<-dbConnect(drv,"jdbc:TSDB://192.168.0.1:0/?user=root&password=taosdata","root","taosdata")
```

Please replace the IP address in the command above to the correct one. If no error message is shown, then the connection is established successfully, otherwise the connection command needs to be adjusted according to the error prompt. TDengine supports below functions in *RJDBC* package:

- `dbWriteTable(conn, "test", iris, overwrite=FALSE, append=TRUE)`: Write the data in a data frame iris to the table test in the TDengine server. Parameter overwrite must be false. append must be TRUE and the schema of the data frame iris should be the same as the table test.
- `dbGetQuery(conn, "select count(*) from test")`: run a query command
- `dbSendUpdate (conn, "use db")`: Execute any non-query sql statement. For example, `dbSendUpdate (conn, "use db")`, write data `dbSendUpdate (conn, "insert into t1 values (now, 99)")`, and the like.
- `dbReadTable(conn, "test")`: read all the data in table test
- `dbDisconnect(conn)`: close a connection
- `dbRemoveTable(conn, "test")`: remove table test

The functions below are not supported currently:

- `dbExistsTable(conn, "test")`: if table test exists
- `dbListTables(conn)`: list all tables in the connection