From ad042edef45f5309eda93851dda9ff9962029205 Mon Sep 17 00:00:00 2001 From: dingbo Date: Sat, 2 Jul 2022 17:15:30 +0800 Subject: [PATCH] docs: java example --- .../example/highvolume/FastWriteExample.java | 3 ++- .../com/taos/example/highvolume/ReadTask.java | 23 +++++++++++-------- .../03-insert-data/05-high-volume.md | 14 +++++++---- 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/docs/examples/java/src/main/java/com/taos/example/highvolume/FastWriteExample.java b/docs/examples/java/src/main/java/com/taos/example/highvolume/FastWriteExample.java index 4a454cbb3f..7e96397b04 100644 --- a/docs/examples/java/src/main/java/com/taos/example/highvolume/FastWriteExample.java +++ b/docs/examples/java/src/main/java/com/taos/example/highvolume/FastWriteExample.java @@ -8,6 +8,7 @@ import java.util.ArrayList; import java.util.List; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingDeque; /** * Prepare target database. @@ -57,7 +58,7 @@ class DataBaseMonitor { public class FastWriteExample { final static Logger logger = LoggerFactory.getLogger(FastWriteExample.class); - final static int taskQueueCapacity = 1000; + final static int taskQueueCapacity = Integer.MAX_VALUE / 100; final static List> taskQueues = new ArrayList<>(); final static List readTasks = new ArrayList<>(); final static List writeTasks = new ArrayList<>(); diff --git a/docs/examples/java/src/main/java/com/taos/example/highvolume/ReadTask.java b/docs/examples/java/src/main/java/com/taos/example/highvolume/ReadTask.java index fe3f78aed9..0e94f876fc 100644 --- a/docs/examples/java/src/main/java/com/taos/example/highvolume/ReadTask.java +++ b/docs/examples/java/src/main/java/com/taos/example/highvolume/ReadTask.java @@ -12,11 +12,11 @@ import java.util.concurrent.BlockingQueue; */ class MockDataSource implements Iterator { private String tbNamePrefix; - private int tableCount = 100; - private int totalRowsPerTable = 1000000; + private int tableCount = 1000; + private long maxRowsPerTable = 1000000000L; // 100 milliseconds between two neighbouring rows. - long startMs = System.currentTimeMillis() - totalRowsPerTable * 100; + long startMs = System.currentTimeMillis() - maxRowsPerTable * 100; private int currentRow = 0; private int currentTbId = -1; @@ -37,7 +37,7 @@ class MockDataSource implements Iterator { currentTbId = 0; currentRow += 1; } - return currentRow < totalRowsPerTable; + return currentRow < maxRowsPerTable; } @Override @@ -61,24 +61,27 @@ class ReadTask implements Runnable { private final static Logger logger = LoggerFactory.getLogger(ReadTask.class); private final int taskId; private final List> taskQueues; + private final int queueCount; private boolean active = true; public ReadTask(int readTaskId, List> queues) { this.taskId = readTaskId; this.taskQueues = queues; + this.queueCount = queues.size(); } /** - * Hash data received to different queues. - * Here we use the hashcode of table name for demo. + * Assign data received to different queues. + * Here we use the suffix number in table name. * You are expected to define your own rule in practice. * * @param line record received * @return which queue to use */ public int getQueueId(String line) { - String tbName = line.substring(0, line.indexOf(',')); - return Math.abs(tbName.hashCode()) % taskQueues.size(); + String tbName = line.substring(0, line.indexOf(',')); // For example: tb1_101 + String suffixNumber = tbName.split("_")[1]; + return Integer.parseInt(suffixNumber) % this.queueCount; } @Override @@ -91,8 +94,8 @@ class ReadTask implements Runnable { int queueId = getQueueId(line); taskQueues.get(queueId).put(line); } - } catch (InterruptedException e) { - e.printStackTrace(); + } catch (Exception e) { + logger.error("Read Task Error", e); } } diff --git a/docs/zh/07-develop/03-insert-data/05-high-volume.md b/docs/zh/07-develop/03-insert-data/05-high-volume.md index 37641cc325..cb13f1c582 100644 --- a/docs/zh/07-develop/03-insert-data/05-high-volume.md +++ b/docs/zh/07-develop/03-insert-data/05-high-volume.md @@ -23,11 +23,14 @@ title: 高效写入 ### 主程序 +主程序负责创建队列,并启动读线程和写线程。 + ```java title="主程序" {{#include docs/examples/java/src/main/java/com/taos/example/highvolume/FastWriteExample.java:main}} ``` ### 读任务的实现 +
读任务的实现 @@ -49,6 +52,8 @@ title: 高效写入 ### SQLWriter 类的实现 +SQLWriter 类封装了拼 SQL 和写数据的逻辑。注意,所有的表都没有提前创建,而是写入出错的时候,再以超级表为模板建表,然后重现执行 INSERT 语句。 + ```java {{#include docs/examples/java/src/main/java/com/taos/example/highvolume/SQLWriter.java:SQLWriter}} ``` @@ -74,13 +79,12 @@ title: 高效写入 ``` 5. 用 java 命令启动示例程序 ``` - java -classpath lib/*:javaexample-1.0.jar com.taos.example.highvolume.FastWriteExample + java -classpath lib/*:javaexample-1.0.jar com.taos.example.highvolume.FastWriteExample ``` ## Python 示例程序 -在 Python 示例程序中采用参数绑定的写入方式。 +在 Python 示例程序中采用参数绑定的写入方式。(开发中) + -- GitLab