未验证 提交 bccf46aa 编写于 作者: sangshuduo's avatar sangshuduo 提交者: GitHub

docs: add r lang doc to cloud (#22334)

* docs: add r lang connector to cloud

* docs: make connect_rest.r perplexing

* docs: add rlang in connect section
上级 aa4658fb
---
toc_max_heading_level: 4
sidebar_label: R
title: TDengine R Language Connector
---
## Configuration
After completing the installation of the R programming environment, you need to perform some configuration steps so that the RJDBC library can connect to and access the TDengine time series database correctly.
1. Load the RJDBC library and other necessary libraries in your R script:
```r
library(DBI)
library(rJava)
library(RJDBC)
```
2. Set the JDBC driver path and JDBC URL:
```r
# Set the JDBC driver path (modify it based on where you actually saved it)
driverPath <- "/path/to/taos-jdbcdriver-X.X.X-dist.jar"
# Set the JDBC URL (modify it according to your specific environment)
url <- "<jdbcURL>"
```
3. Load the JDBC driver:
```r
# Load the JDBC driver
drv <- JDBC("com.taosdata.jdbc.TSDBDriver", driverPath)
```
4. Create a connection to the TDengine database:
```r
# Create a database connection
conn <- dbConnect(drv, url)
```
5. Once the connection is successful, you can use the conn object to perform various database operations such as querying data, inserting data, etc.
---
toc_max_heading_level: 4
sidebar_label: R
title: R Language Connector
---
By using the RJDBC library in R, you can enable R programs to access TDengine data. Here are the installation process, configuration steps, and an example code in R.
## Installation Process
Before getting started, make sure you have installed the R language environment. Then, follow these steps to install and configure the RJDBC library:
1. Install Java Development Kit (JDK): RJDBC library requires Java environment. Download the appropriate JDK for your operating system from the official Oracle website and follow the installation guide.
2. Install the RJDBC library: Execute the following command in the R console to install the RJDBC library.
```r
install.packages("RJDBC", repos='http://cran.us.r-project.org')
```
:::note
On Linux systems, installing the RJDBC package may require installing the necessary components for compilation. For example, on Ubuntu, you can execute the command ``apt install -y libbz2-dev libpcre2-dev libicu-dev`` to install the required components.
On Windows systems, you need to set the **JAVA_HOME** environment variable.
:::
3. Download the TDengine JDBC driver: Visit the Maven website and download the TDengine JDBC driver (taos-jdbcdriver-X.X.X-dist.jar) to your local machine.
## Configuration Process
Once you have completed the installation steps, you need to do some configuration to enable the RJDBC library to connect and access the TDengine time-series database.
1. Load the RJDBC library and other necessary libraries in your R script:
```r
library(DBI)
library(rJava)
library(RJDBC)
```
2. Set the JDBC driver and JDBC URL:
```r
# Set the JDBC driver path (specify the location on your local machine)
driverPath <- "/path/to/taos-jdbcdriver-X.X.X-dist.jar"
# Set the JDBC URL (specify the FQDN and credentials of your TDengine cluster)
url <- "jdbc:TAOS://localhost:6030/?user=root&password=taosdata"
```
3. Load the JDBC driver:
```r
# Load the JDBC driver
drv <- JDBC("com.taosdata.jdbc.TSDBDriver", driverPath)
```
4. Create a TDengine database connection:
```r
# Create a database connection
conn <- dbConnect(drv, url)
```
5. Once the connection is established, you can use the ``conn`` object for various database operations such as querying data and inserting data.
6. Finally, don't forget to close the database connection after you are done:
```r
# Close the database connection
dbDisconnect(conn)
```
## Example Code Using RJDBC in R
Here's an example code that uses the RJDBC library to connect to a TDengine time-series database and perform a query operation:
```r
{{#include docs/examples/R/connect_rest.r:demo}}
```
Please modify the JDBC driver, JDBC URL, username, password, and SQL query statement according to your specific TDengine time-series database environment and requirements.
By following the steps and using the provided example code, you can use the RJDBC library in the R language to access the TDengine time-series database and perform tasks such as data querying and analysis.
...@@ -8,9 +8,13 @@ library("rJava") ...@@ -8,9 +8,13 @@ library("rJava")
library("RJDBC") library("RJDBC")
args<- commandArgs(trailingOnly = TRUE) args<- commandArgs(trailingOnly = TRUE)
driver_path = args[1] # path to jdbc-driver for example: "/root/taos-jdbcdriver-3.0.0-dist.jar" driver_path = args[1] # path to jdbc-driver for example: "/root/taos-jdbcdriver-3.2.4-dist.jar"
driver = JDBC("com.taosdata.jdbc.TSDBDriver", driver_path) driver = JDBC("com.taosdata.jdbc.TSDBDriver", driver_path)
conn = dbConnect(driver, "jdbc:TAOS://127.0.0.1:6030/?user=root&password=taosdata") conn = dbConnect(driver, "jdbc:TAOS://127.0.0.1:6030/?user=root&password=taosdata")
dbGetQuery(conn, "SELECT server_version()") dbGetQuery(conn, "SELECT server_version()")
dbSendUpdate(conn, "create database if not exists rtest")
dbSendUpdate(conn, "create table if not exists rtest.test (ts timestamp, current float, voltage int, devname varchar(20))")
dbSendUpdate(conn, "insert into rtest.test values (now, 1.2, 220, 'test')")
dbGetQuery(conn, "select * from rtest.test")
dbDisconnect(conn) dbDisconnect(conn)
# ANCHOR_END: demo # ANCHOR_END: demo
...@@ -2,11 +2,19 @@ if (! "RJDBC" %in% installed.packages()[, "Package"]) { ...@@ -2,11 +2,19 @@ if (! "RJDBC" %in% installed.packages()[, "Package"]) {
install.packages('RJDBC', repos='http://cran.us.r-project.org') install.packages('RJDBC', repos='http://cran.us.r-project.org')
} }
# ANCHOR: demo
library("DBI") library("DBI")
library("rJava") library("rJava")
library("RJDBC") library("RJDBC")
driver_path = "/home/debug/build/lib/taos-jdbcdriver-2.0.38-dist.jar"
args<- commandArgs(trailingOnly = TRUE)
driver_path = args[1] # path to jdbc-driver for example: "/root/taos-jdbcdriver-3.2.4-dist.jar"
driver = JDBC("com.taosdata.jdbc.rs.RestfulDriver", driver_path) driver = JDBC("com.taosdata.jdbc.rs.RestfulDriver", driver_path)
conn = dbConnect(driver, "jdbc:TAOS-RS://localhost:6041?user=root&password=taosdata") conn = dbConnect(driver, "jdbc:TAOS-RS://localhost:6041?user=root&password=taosdata")
dbGetQuery(conn, "SELECT server_version()") dbGetQuery(conn, "SELECT server_version()")
dbDisconnect(conn) dbSendUpdate(conn, "create database if not exists rtest")
\ No newline at end of file dbSendUpdate(conn, "create table if not exists rtest.test (ts timestamp, current float, voltage int, devname varchar(20))")
dbSendUpdate(conn, "insert into rtest.test values (now, 1.2, 220, 'test')")
dbGetQuery(conn, "select * from rtest.test")
dbDisconnect(conn)
# ANCHOR_END: demo
---
toc_max_heading_level: 4
sidebar_label: R
title: TDengine R Language Connector
---
## 配置
完成了 R 语言环境安装步骤后,您需要进行一些配置,以便RJDBC库能够正确连接和访问 TDengine 时序数据库。
1. 在 R 脚本中加载 RJDBC 和其他必要的库:
```r
library(DBI)
library(rJava)
library(RJDBC)
```
2. 设置 JDBC 驱动程序和 JDBC URL:
```r
# 设置JDBC驱动程序路径(根据您实际保存的位置进行修改)
driverPath <- "/path/to/taos-jdbcdriver-X.X.X-dist.jar"
# 设置JDBC URL(根据您的具体环境进行修改)
url <- "<jdbcURL>"
```
3. 加载 JDBC 驱动程序:
```r
# 加载JDBC驱动程序
drv <- JDBC("com.taosdata.jdbc.TSDBDriver", driverPath)
```
4. 创建 TDengine 数据库连接:
```r
# 创建数据库连接
conn <- dbConnect(drv, url)
```
5. 连接成功后,您可以使用 conn 对象进行各种数据库操作,如查询数据、插入数据等。
---
toc_max_heading_level: 4
sidebar_label: R
title: TDengine R Language Connector
---
通过 R 语言中的 RJDBC 库可以使 R 语言程序支持访问 TDengine 数据。以下是安装过程、配置过程以及 R 语言示例代码。
## 安装过程
在开始之前,请确保已经安装了R语言环境。然后按照以下步骤安装和配置RJDBC库:
1. 安装Java Development Kit (JDK):RJDBC库需要依赖Java环境。请从Oracle官方网站下载适合您操作系统的JDK,并按照安装指南进行安装。
2. 安装RJDBC库:在R控制台中执行以下命令来安装RJDBC库。
```r
install.packages("RJDBC", repos='http://cran.us.r-project.org')
```
:::note
在 Linux 上安装 RJDBC 包可能需要安装编译需要的组件,以 Ubuntu 为例执行 `apt install -y libbz2-dev libpcre2-dev libicu-dev` 命令安装。
在 Windows 系统上需要设置 JAVA_HOME 环境变量。
:::
3. 下载 TDengine JDBC 驱动程序:访问 maven.org 网站,下载 TDengine JDBC 驱动程序(taos-jdbcdriver-X.X.X-dist.jar)。
4. 将 TDengine JDBC 驱动程序放置在适当的位置:在您的计算机上选择一个合适的位置,将 TDengine JDBC 驱动程序文件(taos-jdbcdriver-X.X.X-dist.jar)保存在此处。
## 配置过程
完成了安装步骤后,您需要进行一些配置,以便RJDBC库能够正确连接和访问TDengine时序数据库。
1. 在 R 脚本中加载 RJDBC 和其他必要的库:
```r
library(DBI)
library(rJava)
library(RJDBC)
```
2. 设置 JDBC 驱动程序和 JDBC URL:
```r
# 设置JDBC驱动程序路径(根据您实际保存的位置进行修改)
driverPath <- "/path/to/taos-jdbcdriver-X.X.X-dist.jar"
# 设置JDBC URL(根据您的具体环境进行修改)
url <- "jdbc:TAOS://localhost:6030/?user=root&password=taosdata"
```
3. 加载 JDBC 驱动程序:
```r
# 加载JDBC驱动程序
drv <- JDBC("com.taosdata.jdbc.TSDBDriver", driverPath)
```
4. 创建 TDengine 数据库连接:
```r
# 创建数据库连接
conn <- dbConnect(drv, url)
```
5. 连接成功后,您可以使用 conn 对象进行各种数据库操作,如查询数据、插入数据等。
6. 最后,不要忘记在使用完成后关闭数据库连接:
```r
# 关闭数据库连接
dbDisconnect(conn)
```
## 使用 RJDBC 的 R 语言示例代码
以下是一个使用 RJDBC 库连接 TDengine 时序数据库并执行查询操作的示例代码:
```r
{{#include docs/examples/R/connect_rest.r:demo}}
```
请根据您的实际情况修改JDBC驱动程序、JDBC URL、用户名、密码以及SQL查询语句,以适配您的 TDengine 时序数据库环境和要求。
通过以上步骤和示例代码,您可以在 R 语言环境中使用 RJDBC 库访问 TDengine 时序数据库,进行数据查询和分析等操作。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册