88 lines
		
	
	
		
			3.3 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
			
		
		
	
	
			88 lines
		
	
	
		
			3.3 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
| ---
 | |
| toc_max_heading_level: 4
 | |
| sidebar_label: R
 | |
| title: R Language Connector
 | |
| ---
 | |
| 
 | |
| import Tabs from '@theme/Tabs';
 | |
| import TabItem from '@theme/TabItem';
 | |
| 
 | |
| import Rdemo from "../../07-develop/01-connect/_connect_r.mdx"
 | |
| 
 | |
| 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
 | |
| 1. The default R language package version 4.2 which shipped with Ubuntu might lead unresponsive bug. Please install latest version of R language package from the [official website](https://www.r-project.org/).
 | |
| 2. 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.
 | |
| 3. 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:
 | |
| 
 | |
| <Rdemo/>
 | |
| 
 | |
| 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.
 |