mod sample code

This commit is contained in:
sheyanjie-qq 2024-08-01 14:26:55 +08:00
parent 31bdddde57
commit 5ab0b11223
22 changed files with 171 additions and 114 deletions

View File

@ -20,15 +20,13 @@ public static void main(String[] args) throws SQLException {
connProps.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8");
try (Connection conn = DriverManager.getConnection(jdbcUrl, connProps)) {
System.out.println("Connected");
System.out.println("Connected to " + jdbcUrl + " successfully.");
// you can use the connection for execute SQL here
} catch (SQLException ex) {
// handle any errors, please refer to the JDBC specifications for detailed exceptions info
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("Error Code: " + ex.getErrorCode());
System.out.println("Message: " + ex.getMessage());
System.out.println("Failed to connect to " + jdbcUrl + "; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage());
}
}
// ANCHOR_END: main

View File

@ -9,15 +9,13 @@ public class RESTConnectExample {
public static void main(String[] args) throws SQLException {
String jdbcUrl = "jdbc:TAOS-RS://localhost:6041?user=root&password=taosdata";
try (Connection conn = DriverManager.getConnection(jdbcUrl)){
System.out.println("Connected");
System.out.println("Connected to " + jdbcUrl + " successfully.");
// you can use the connection for execute SQL here
} catch (SQLException ex) {
// handle any errors, please refer to the JDBC specifications for detailed exceptions info
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("Error Code: " + ex.getErrorCode());
System.out.println("Message: " + ex.getMessage());
System.out.println("Failed to connect to " + jdbcUrl + "; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage());
}
}
// ANCHOR_END: main

View File

@ -21,15 +21,13 @@ public static void main(String[] args) throws SQLException {
connProps.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8");
try (Connection conn = DriverManager.getConnection(jdbcUrl, connProps)){
System.out.println("Connected");
System.out.println("Connected to " + jdbcUrl + " successfully.");
// you can use the connection for execute SQL here
} catch (SQLException ex) {
// handle any errors, please refer to the JDBC specifications for detailed exceptions info
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("Error Code: " + ex.getErrorCode());
System.out.println("Message: " + ex.getMessage());
System.out.println("Failed to connect to " + jdbcUrl + "; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage());
}
}
// ANCHOR_END: main

View File

@ -294,7 +294,7 @@ URL 和 Properties 的详细参数说明和如何使用详见 [API 说明](../..
</Tabs>
### Websocket 连接
各语言连接器建立 Websocket 连接代码样例。
下面是各语言连接器建立 Websocket 连接代码样例。演示了如何使用 Websocket 连接方式连接到 TDengine 数据库,并对连接设定一些参数。整个过程主要涉及到数据库连接的建立和异常处理。
<Tabs defaultValue="java" groupId="lang">
<TabItem label="Java" value="java">
@ -334,7 +334,8 @@ URL 和 Properties 的详细参数说明和如何使用详见 [API 说明](../..
</Tabs>
### 原生连接
各语言连接器建立原生连接代码样例。
下面是各语言连接器建立原生连接代码样例。演示了如何使用原生连接方式连接到 TDengine 数据库,并对连接设定一些参数。整个过程主要涉及到数据库连接的建立和异常处理。
<Tabs defaultValue="java" groupId="lang">
<TabItem label="Java" value="java">
```java
@ -366,7 +367,8 @@ URL 和 Properties 的详细参数说明和如何使用详见 [API 说明](../..
</Tabs>
### REST 连接
各语言连接器建立 REST 连接代码样例。
下面是各语言连接器建立 RESt 连接代码样例。演示了如何使用 REST 连接方式连接到 TDengine 数据库。整个过程主要涉及到数据库连接的建立和异常处理。
<Tabs defaultValue="java" groupId="lang">
<TabItem label="Java" value="java">
```java

View File

@ -12,7 +12,8 @@ TDengine 对 SQL 语言提供了全面的支持,允许用户以熟悉的 SQL
下面介绍使用各语言连接器通过执行 SQL 完成建库、建表、写入数据和查询数据。
## 建库和表
以智能电表为例,展示如何使用连接器执行 SQL 来创建数据库和表。
下面以智能电表为例,展示使用各语言连接器如何执行 SQL 命令创建一个名为 `power` 的数据库,然后使用 `power` 数据库为默认数据库。
接着创建一个名为 `meters` 的超级表STABLE其表结构包含时间戳、电流、电压、相位等列以及分组 ID 和位置作为标签。
<Tabs defaultValue="java" groupId="lang">
<TabItem value="java" label="Java">
@ -40,7 +41,7 @@ TDengine 对 SQL 语言提供了全面的支持,允许用户以熟悉的 SQL
</Tabs>
## 插入数据
以智能电表为例,展示如何使用连接器执行 SQL 来插入数据。
下面以智能电表为例,展示如何使用连接器执行 SQL 来插入数据`power` 数据库的 `meters` 超级表样例使用 TDengine 自动建表 SQL 语法,写入 d1001 子表中 3 条数据,写入 d1002 子表中 1 条数据,然后打印出实际插入数据条数。
<Tabs defaultValue="java" groupId="lang">
<TabItem value="java" label="Java">
@ -70,7 +71,7 @@ NOW 为系统内部函数,默认为客户端所在计算机当前时间。 NOW
</Tabs>
## 查询数据
以智能电表为例,展示如何使用各语言连接器执行 SQL 来查询数据,并将获取到的结果打印出来。
下面以智能电表为例,展示如何使用各语言连接器执行 SQL 来查询数据,`power` 数据库 `meters` 超级表中查询最多 100 行数据,并将获取到的结果按行打印出来。
<Tabs defaultValue="java" groupId="lang">
<TabItem label="Java" value="java">

View File

@ -153,6 +153,12 @@ st,t1=3,t2=4,t3=t3 c1=3i64,c6="passit" 1626006833640000000
## 无模式写入示例
下面以智能电表为例,介绍各语言连接器使用无模式写入接口写入数据的代码样例,包含了三种协议: InfluxDB 的行协议、OpenTSDB 的 TELNET 行协议和 OpenTSDB 的 JSON 格式协议。
:::note
- 因为无模式写入自动建表规则与之前执行 SQL 样例中不同,因此运行代码样例前请确保 `meters`、`metric_telnet` 和 `metric_json` 表不存在。
- OpenTSDB 的 TELNET 行协议和 OpenTSDB 的 JSON 格式协议只支持一个数据列,因此我们采用了其他示例。
:::
### Websocket 连接
<Tabs defaultValue="java" groupId="schemaless">

View File

@ -13,7 +13,14 @@ import TabItem from "@theme/TabItem";
- 预编译当使用参数绑定时SQL 语句可以被预编译并缓存,后续使用不同的参数值执行时,可以直接使用预编译的版本,提高执行效率。
- 减少网络开销:参数绑定还可以减少发送到数据库的数据量,因为只需要发送参数值而不是完整的 SQL 语句,特别是在执行大量相似的插入或更新操作时,这种差异尤为明显。
下面我们继续以智能电表为例,展示各语言连接器使用参数绑定高效写入的功能。
下面我们继续以智能电表为例,展示各语言连接器使用参数绑定高效写入的功能:
1. 准备一个参数化的 SQL 插入语句,用于向超级表 `meters` 中插入数据。这个语句允许动态地指定子表名、标签和列值。
2. 循环生成多个子表及其对应的数据行。对于每个子表:
- 设置子表的名称和标签值(分组 ID 和位置)。
- 生成多行数据,每行包括一个时间戳、随机生成的电流、电压和相位值。
- 执行批量插入操作,将这些数据行插入到对应的子表中。
3. 最后打印实际插入表中的行数。
## Websocket 连接
<Tabs defaultValue="java" groupId="lang">
<TabItem value="java" label="Java">

View File

@ -78,7 +78,8 @@ Java 连接器创建消费者的参数为 Properties 可以设置的参数列
</Tabs>
### Websocket 连接
介绍各语言连接器使用 Websocket 连接方式创建消费者。
介绍各语言连接器使用 Websocket 连接方式创建消费者。指定连接的服务器地址,设置自动提交,从最新消息开始消费,指定 `group.id``client.id` 等信息。有的语言的连接器还支持反序列化参数。
<Tabs defaultValue="java" groupId="lang">
<TabItem value="java" label="Java">
@ -126,7 +127,8 @@ Java 连接器创建消费者的参数为 Properties 可以设置的参数列
### 原生连接
介绍各语言连接器使用原生连接方式创建消费者。
介绍各语言连接器使用原生连接方式创建消费者。指定连接的服务器地址,设置自动提交,从最新消息开始消费,指定 `group.id``client.id` 等信息。有的语言的连接器还支持反序列化参数。
<Tabs groupId="lang">
<TabItem value="java" label="Java">

View File

@ -1365,7 +1365,7 @@ JDBC 标准不支持数据订阅,因此本章所有接口都是扩展接口。
| reconnectIntervalMs | 自动重连重试间隔,单位毫秒。仅在 enableCompression 为 true 时生效。 | 2000 ms |
| reconnectRetryCount | 自动重连重试次数。仅在 enableCompression 为 true 时生效。 | 3 |
其他参数请参考:[Consumer 参数列表](../../develop/tmq/#数据订阅相关参数) 注意TDengine服务端自 3.2.0.0 版本开始消息订阅中的 auto.offset.reset 默认值发生变化。
其他参数请参考:[Consumer 参数列表](../../../develop/tmq/#创建参数) 注意TDengine服务端自 3.2.0.0 版本开始消息订阅中的 auto.offset.reset 默认值发生变化。
- `public void subscribe(Collection<String> topics) throws SQLException`
- **接口说明**:订阅一组主题。

View File

@ -39,8 +39,7 @@ try {
this.consumer = new TaosConsumer<>(config);
} catch (SQLException ex) {
// handle any errors, please refer to the JDBC specifications for detailed exceptions info
System.out.println("Error Code: " + ex.getErrorCode());
System.out.println("Message: " + ex.getMessage());
System.out.println("Failed to create jni consumer with " + config.getProperty("bootstrap.servers") + " ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage());
throw new SQLException("Failed to create consumer", ex);
}
// ANCHOR_END: create_consumer
@ -66,9 +65,8 @@ try {
}
} catch (SQLException ex){
// handle any errors, please refer to the JDBC specifications for detailed exceptions info
System.out.println("Error Code: " + ex.getErrorCode());
System.out.println("Message: " + ex.getMessage());
System.out.println("Failed to poll data; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage());
throw new SQLException("Failed to poll data", ex);
} finally {
consumer.close();
shutdownLatch.countDown();
@ -94,9 +92,8 @@ try {
}
} catch (SQLException ex){
// handle any errors, please refer to the JDBC specifications for detailed exceptions info
System.out.println("Error Code: " + ex.getErrorCode());
System.out.println("Message: " + ex.getMessage());
System.out.println("Failed to execute consumer functions. ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage());
throw new SQLException("Failed to execute consumer functions", ex);
} finally {
consumer.close();
shutdownLatch.countDown();
@ -110,8 +107,8 @@ try {
consumer.unsubscribe();
} catch (SQLException ex){
// handle any errors, please refer to the JDBC specifications for detailed exceptions info
System.out.println("Error Code: " + ex.getErrorCode());
System.out.println("Message: " + ex.getMessage());
System.out.println("Failed to unsubscribe consumer. ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage());
throw new SQLException("Failed to unsubscribe consumer", ex);
} finally {
consumer.close();
}

View File

@ -38,8 +38,8 @@ public abstract class AbsConsumerLoopFull {
try {
this.consumer = new TaosConsumer<>(config);
} catch (SQLException ex) {
// handle exception
System.out.println("SQLException: " + ex.getMessage());
// handle any errors, please refer to the JDBC specifications for detailed exceptions info
System.out.println("Failed to create jni consumer, host : " + config.getProperty("bootstrap.servers") + "; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage());
throw new SQLException("Failed to create consumer", ex);
}

View File

@ -39,13 +39,12 @@ try {
this.consumer = new TaosConsumer<>(config);
} catch (SQLException ex) {
// handle any errors, please refer to the JDBC specifications for detailed exceptions info
System.out.println("Error Code: " + ex.getErrorCode());
System.out.println("Message: " + ex.getMessage());
System.out.println("Failed to create ws consumer with " + config.getProperty("bootstrap.servers") + " ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage());
throw new SQLException("Failed to create consumer", ex);
}
// ANCHOR_END: create_consumer
this.topics = Collections.singletonList("topic_speed");
this.topics = Collections.singletonList("topic_meters");
this.shutdown = new AtomicBoolean(false);
this.shutdownLatch = new CountDownLatch(1);
}

View File

@ -54,9 +54,9 @@ try (TaosConsumer<AbsConsumerLoop.ResultBean> consumer = new TaosConsumer<>(conf
// you can handle data here
}
} catch (SQLException ex) {
// handle exception
System.out.println("SQLException: " + ex.getMessage());
throw new SQLException("Failed to create consumer", ex);
// handle any errors, please refer to the JDBC specifications for detailed exceptions info
System.out.println("Failed to execute consumer functions. server: " + config.getProperty("bootstrap.servers") + "; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage());
throw new SQLException("Failed to execute consumer functions", ex);
}
// ANCHOR_END: consumer_seek
}

View File

@ -15,7 +15,7 @@ public class JdbcCreatDBDemo {
public static void main(String[] args) throws SQLException {
final String url = "jdbc:TAOS://" + host + ":6030/?user=" + user + "&password=" + password;
final String jdbcUrl = "jdbc:TAOS://" + host + ":6030/?user=" + user + "&password=" + password;
// get connection
Properties properties = new Properties();
@ -24,7 +24,7 @@ properties.setProperty("locale", "en_US.UTF-8");
properties.setProperty("timezone", "UTC-8");
System.out.println("get connection starting...");
// ANCHOR: create_db_and_table
try (Connection connection = DriverManager.getConnection(url, properties);
try (Connection connection = DriverManager.getConnection(jdbcUrl, properties);
Statement stmt = connection.createStatement()) {
// create database
@ -44,8 +44,7 @@ try (Connection connection = DriverManager.getConnection(url, properties);
} catch (SQLException ex) {
// handle any errors, please refer to the JDBC specifications for detailed exceptions info
System.out.println("Error Code: " + ex.getErrorCode());
System.out.println("Message: " + ex.getMessage());
System.out.println("Failed to create db and table, url:" + jdbcUrl + "; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage());
}
// ANCHOR_END: create_db_and_table

View File

@ -15,7 +15,7 @@ public class JdbcInsertDataDemo {
public static void main(String[] args) throws SQLException {
final String url = "jdbc:TAOS://" + host + ":6030/?user=" + user + "&password=" + password;
final String jdbcUrl = "jdbc:TAOS://" + host + ":6030/?user=" + user + "&password=" + password;
// get connection
Properties properties = new Properties();
@ -24,7 +24,7 @@ properties.setProperty("locale", "en_US.UTF-8");
properties.setProperty("timezone", "UTC-8");
System.out.println("get connection starting...");
// ANCHOR: insert_data
try (Connection connection = DriverManager.getConnection(url, properties);
try (Connection connection = DriverManager.getConnection(jdbcUrl, properties);
Statement stmt = connection.createStatement()) {
// insert data, please make sure the database and table are created before
@ -39,11 +39,11 @@ try (Connection connection = DriverManager.getConnection(url, properties);
"(NOW + 1a, 10.30000, 218, 0.25000) ";
int affectedRows = stmt.executeUpdate(insertQuery);
// you can check affectedRows here
System.out.println("insert " + affectedRows + " rows.");
System.out.println("inserted into " + affectedRows + " rows to power.meters successfully.");
} catch (SQLException ex) {
// handle any errors, please refer to the JDBC specifications for detailed exceptions info
System.out.println("Error Code: " + ex.getErrorCode());
System.out.println("Message: " + ex.getMessage());
System.out.println("Failed to insert data to power.meters, url:" + jdbcUrl + "; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage());
}
// ANCHOR_END: insert_data
}

View File

@ -15,7 +15,7 @@ public class JdbcQueryDemo {
public static void main(String[] args) throws SQLException {
final String url = "jdbc:TAOS://" + host + ":6030/?user=" + user + "&password=" + password;
final String jdbcUrl = "jdbc:TAOS://" + host + ":6030/?user=" + user + "&password=" + password;
// get connection
Properties properties = new Properties();
@ -24,10 +24,10 @@ properties.setProperty("locale", "en_US.UTF-8");
properties.setProperty("timezone", "UTC-8");
System.out.println("get connection starting...");
// ANCHOR: query_data
try (Connection connection = DriverManager.getConnection(url, properties);
try (Connection connection = DriverManager.getConnection(jdbcUrl, properties);
Statement stmt = connection.createStatement();
// query data, make sure the database and table are created before
ResultSet resultSet = stmt.executeQuery("SELECT * FROM power.meters")) {
ResultSet resultSet = stmt.executeQuery("SELECT ts, current, location FROM power.meters limit 100")) {
Timestamp ts;
float current;
@ -39,12 +39,11 @@ try (Connection connection = DriverManager.getConnection(url, properties);
location = resultSet.getString("location");
// you can check data here
System.out.printf("%s, %f, %s\n", ts, current, location);
System.out.printf("ts: %s, current: %f, location: %s %n", ts, current, location);
}
} catch (SQLException ex) {
// handle any errors, please refer to the JDBC specifications for detailed exceptions info
System.out.println("Error Code: " + ex.getErrorCode());
System.out.println("Message: " + ex.getMessage());
System.out.println("Failed to query data from power.meters, url:" + jdbcUrl + "; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage());
}
// ANCHOR_END: query_data
}

View File

@ -15,7 +15,7 @@ public class JdbcReqIdDemo {
public static void main(String[] args) throws SQLException {
final String url = "jdbc:TAOS://" + host + ":6030/?user=" + user + "&password=" + password;
final String jdbcUrl = "jdbc:TAOS://" + host + ":6030/?user=" + user + "&password=" + password;
// get connection
Properties properties = new Properties();
@ -25,17 +25,11 @@ properties.setProperty("timezone", "UTC-8");
System.out.println("get connection starting...");
// ANCHOR: with_reqid
try (Connection connection = DriverManager.getConnection(url, properties);
try (Connection connection = DriverManager.getConnection(jdbcUrl, properties);
// Create a statement that allows specifying a request ID
AbstractStatement aStmt = (AbstractStatement) connection.createStatement()) {
boolean hasResultSet = aStmt.execute("CREATE DATABASE IF NOT EXISTS power", 1L);
assert !hasResultSet;
int rowsAffected = aStmt.executeUpdate("USE power", 2L);
assert rowsAffected == 0;
try (ResultSet rs = aStmt.executeQuery("SELECT * FROM meters limit 1", 3L)) {
try (ResultSet rs = aStmt.executeQuery("SELECT ts, current, location FROM power.meters limit 1", 3L)) {
while (rs.next()) {
Timestamp timestamp = rs.getTimestamp(1);
System.out.println("timestamp = " + timestamp);
@ -43,8 +37,8 @@ try (Connection connection = DriverManager.getConnection(url, properties);
}
} catch (SQLException ex) {
// handle any errors, please refer to the JDBC specifications for detailed exceptions info
System.out.println("Error Code: " + ex.getErrorCode());
System.out.println("Message: " + ex.getMessage());
System.out.println("Failed to execute sql with reqId, url:" + jdbcUrl + "; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage());
}
// ANCHOR_END: with_reqid
}

View File

@ -1,14 +1,9 @@
package com.taosdata.example;
import com.taosdata.jdbc.TSDBPreparedStatement;
import com.taosdata.jdbc.utils.StringUtils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
// ANCHOR: para_bind
@ -29,7 +24,6 @@ public class ParameterBindingBasicDemo {
String sql = "INSERT INTO ? USING meters TAGS(?,?) VALUES (?,?,?,?)";
try (TSDBPreparedStatement pstmt = conn.prepareStatement(sql).unwrap(TSDBPreparedStatement.class)) {
for (int i = 1; i <= numOfSubTable; i++) {
// set table name
pstmt.setTableName("d_bind_" + i);
@ -38,40 +32,23 @@ public class ParameterBindingBasicDemo {
pstmt.setTagInt(0, i);
pstmt.setTagString(1, "location_" + i);
// set column ts
ArrayList<Long> tsList = new ArrayList<>();
// set columns
long current = System.currentTimeMillis();
for (int j = 0; j < numOfRow; j++)
tsList.add(current + j);
pstmt.setTimestamp(0, tsList);
// set column current
ArrayList<Float> f1List = new ArrayList<>();
for (int j = 0; j < numOfRow; j++)
f1List.add(random.nextFloat() * 30);
pstmt.setFloat(1, f1List);
// set column voltage
ArrayList<Integer> f2List = new ArrayList<>();
for (int j = 0; j < numOfRow; j++)
f2List.add(random.nextInt(300));
pstmt.setInt(2, f2List);
// set column phase
ArrayList<Float> f3List = new ArrayList<>();
for (int j = 0; j < numOfRow; j++)
f3List.add(random.nextFloat());
pstmt.setFloat(3, f3List);
// add column
pstmt.columnDataAddBatch();
for (int j = 0; j < numOfRow; j++) {
pstmt.setTimestamp(1, new Timestamp(current + j));
pstmt.setFloat(2, random.nextFloat() * 30);
pstmt.setInt(3, random.nextInt(300));
pstmt.setFloat(4, random.nextFloat());
pstmt.addBatch();
}
int [] exeResult = pstmt.executeBatch();
// you can check exeResult here
System.out.println("insert " + exeResult.length + " rows.");
}
// execute column
pstmt.columnDataExecuteBatch();
}
} catch (SQLException ex) {
// handle any errors, please refer to the JDBC specifications for detailed exceptions info
System.out.println("Error Code: " + ex.getErrorCode());
System.out.println("Message: " + ex.getMessage());
System.out.println("Failed to insert to table meters using stmt, url: " + jdbcUrl + "; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage());
}
}

View File

@ -0,0 +1,83 @@
package com.taosdata.example;
import com.taosdata.jdbc.TSDBPreparedStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Random;
// ANCHOR: para_bind
public class ParameterBindingBatchDemo {
// modify host to your own
private static final String host = "127.0.0.1";
private static final Random random = new Random(System.currentTimeMillis());
private static final int numOfSubTable = 10, numOfRow = 10;
public static void main(String[] args) throws SQLException {
String jdbcUrl = "jdbc:TAOS://" + host + ":6030/";
try (Connection conn = DriverManager.getConnection(jdbcUrl, "root", "taosdata")) {
init(conn);
String sql = "INSERT INTO ? USING meters TAGS(?,?) VALUES (?,?,?,?)";
try (TSDBPreparedStatement pstmt = conn.prepareStatement(sql).unwrap(TSDBPreparedStatement.class)) {
for (int i = 1; i <= numOfSubTable; i++) {
// set table name
pstmt.setTableName("d_bind_" + i);
// set tags
pstmt.setTagInt(0, i);
pstmt.setTagString(1, "location_" + i);
// set column ts
ArrayList<Long> tsList = new ArrayList<>();
long current = System.currentTimeMillis();
for (int j = 0; j < numOfRow; j++)
tsList.add(current + j);
pstmt.setTimestamp(0, tsList);
// set column current
ArrayList<Float> f1List = new ArrayList<>();
for (int j = 0; j < numOfRow; j++)
f1List.add(random.nextFloat() * 30);
pstmt.setFloat(1, f1List);
// set column voltage
ArrayList<Integer> f2List = new ArrayList<>();
for (int j = 0; j < numOfRow; j++)
f2List.add(random.nextInt(300));
pstmt.setInt(2, f2List);
// set column phase
ArrayList<Float> f3List = new ArrayList<>();
for (int j = 0; j < numOfRow; j++)
f3List.add(random.nextFloat());
pstmt.setFloat(3, f3List);
// add column
pstmt.columnDataAddBatch();
}
// execute column
pstmt.columnDataExecuteBatch();
}
} catch (SQLException ex) {
// handle any errors, please refer to the JDBC specifications for detailed exceptions info
System.out.println("Failed to insert to table meters using stmt, url: " + jdbcUrl + "; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage());
}
}
private static void init(Connection conn) throws SQLException {
try (Statement stmt = conn.createStatement()) {
stmt.execute("CREATE DATABASE IF NOT EXISTS power");
stmt.execute("USE power");
stmt.execute("CREATE STABLE IF NOT EXISTS meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (groupId INT, location BINARY(24))");
}
}
}
// ANCHOR_END: para_bind

View File

@ -13,12 +13,12 @@ import java.sql.Statement;
public class SchemalessJniTest {
private static final String host = "127.0.0.1";
private static final String lineDemo = "meters,groupid=2,location=California.SanFrancisco current=10.3000002f64,voltage=219i32,phase=0.31f64 1626006833639";
private static final String telnetDemo = "stb0_0 1707095283260 4 host=host0 interface=eth0";
private static final String jsonDemo = "{\"metric\": \"meter_current\",\"timestamp\": 1626846400,\"value\": 10.3, \"tags\": {\"groupid\": 2, \"location\": \"California.SanFrancisco\", \"id\": \"d1001\"}}";
private static final String telnetDemo = "metric_telnet 1707095283260 4 host=host0 interface=eth0";
private static final String jsonDemo = "{\"metric\": \"metric_json\",\"timestamp\": 1626846400,\"value\": 10.3, \"tags\": {\"groupid\": 2, \"location\": \"California.SanFrancisco\", \"id\": \"d1001\"}}";
public static void main(String[] args) throws SQLException {
final String url = "jdbc:TAOS://" + host + ":6030/?user=root&password=taosdata";
try (Connection connection = DriverManager.getConnection(url)) {
final String jdbcUrl = "jdbc:TAOS://" + host + ":6030/?user=root&password=taosdata";
try (Connection connection = DriverManager.getConnection(jdbcUrl)) {
init(connection);
AbstractConnection conn = connection.unwrap(AbstractConnection.class);
@ -27,8 +27,7 @@ public class SchemalessJniTest {
conn.write(jsonDemo, SchemalessProtocolType.JSON, SchemalessTimestampType.NOT_CONFIGURED);
} catch (SQLException ex) {
// handle any errors, please refer to the JDBC specifications for detailed exceptions info
System.out.println("Error Code: " + ex.getErrorCode());
System.out.println("Message: " + ex.getMessage());
System.out.println("Failed to insert data with schemaless, host:" + host + "; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage());
}
}

View File

@ -13,8 +13,8 @@ import java.sql.Statement;
public class SchemalessWsTest {
private static final String host = "127.0.0.1";
private static final String lineDemo = "meters,groupid=2,location=California.SanFrancisco current=10.3000002f64,voltage=219i32,phase=0.31f64 1626006833639";
private static final String telnetDemo = "stb0_0 1707095283260 4 host=host0 interface=eth0";
private static final String jsonDemo = "{\"metric\": \"meter_current\",\"timestamp\": 1626846400,\"value\": 10.3, \"tags\": {\"groupid\": 2, \"location\": \"California.SanFrancisco\", \"id\": \"d1001\"}}";
private static final String telnetDemo = "metric_telnet 1707095283260 4 host=host0 interface=eth0";
private static final String jsonDemo = "{\"metric\": \"metric_json\",\"timestamp\": 1626846400,\"value\": 10.3, \"tags\": {\"groupid\": 2, \"location\": \"California.SanFrancisco\", \"id\": \"d1001\"}}";
public static void main(String[] args) throws SQLException {
final String url = "jdbc:TAOS-RS://" + host + ":6041?user=root&password=taosdata&batchfetch=true";
@ -27,8 +27,7 @@ public class SchemalessWsTest {
conn.write(jsonDemo, SchemalessProtocolType.JSON, SchemalessTimestampType.SECONDS);
} catch (SQLException ex) {
// handle any errors, please refer to the JDBC specifications for detailed exceptions info
System.out.println("Error Code: " + ex.getErrorCode());
System.out.println("Message: " + ex.getMessage());
System.out.println("Failed to insert data with schemaless, host:" + host + "; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage());
}
}

View File

@ -49,8 +49,7 @@ public class WSParameterBindingBasicDemo {
}
} catch (SQLException ex) {
// handle any errors, please refer to the JDBC specifications for detailed exceptions info
System.out.println("Error Code: " + ex.getErrorCode());
System.out.println("Message: " + ex.getMessage());
System.out.println("Failed to insert to table meters using stmt, url: " + jdbcUrl + "; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage());
}
}