JdbcTaosdemo.java 12.0 KB
Newer Older
Z
zyyang 已提交
1
package com.taosdata.example.jdbcTaosdemo;
2

Z
zyyang 已提交
3 4 5 6
import com.taosdata.example.jdbcTaosdemo.domain.JdbcTaosdemoConfig;
import com.taosdata.example.jdbcTaosdemo.task.CreateTableTask;
import com.taosdata.example.jdbcTaosdemo.task.InsertTableDatetimeTask;
import com.taosdata.example.jdbcTaosdemo.task.InsertTableTask;
Z
zyyang 已提交
7 8
import com.taosdata.example.jdbcTaosdemo.utils.ConnectionFactory;
import com.taosdata.example.jdbcTaosdemo.utils.SqlSpeller;
Z
zyyang 已提交
9
import com.taosdata.example.jdbcTaosdemo.utils.TimeStampUtil;
10 11 12
import org.apache.log4j.Logger;

import java.sql.*;
Z
zyyang 已提交
13 14 15
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
16 17 18 19 20 21 22 23 24 25 26 27

public class JdbcTaosdemo {

    private static Logger logger = Logger.getLogger(JdbcTaosdemo.class);
    private final JdbcTaosdemoConfig config;
    private Connection connection;

    public JdbcTaosdemo(JdbcTaosdemoConfig config) {
        this.config = config;
    }

    public static void main(String[] args) {
Z
zyyang 已提交
28
        // parse config from args
Z
zyyang 已提交
29
        JdbcTaosdemoConfig config = new JdbcTaosdemoConfig(args);
30 31

        boolean isHelp = Arrays.asList(args).contains("--help");
Z
change  
zyyang 已提交
32
        if (isHelp || config.host == null || config.host.isEmpty()) {
Z
zyyang 已提交
33
            JdbcTaosdemoConfig.printHelp();
34 35 36 37
            return;
        }

        JdbcTaosdemo taosdemo = new JdbcTaosdemo(config);
Z
zyyang 已提交
38
        // establish connection
39
        taosdemo.init();
Z
zyyang 已提交
40
        // drop database
41
        taosdemo.dropDatabase();
Z
zyyang 已提交
42
        // create database
43
        taosdemo.createDatabase();
Z
zyyang 已提交
44
        // use db
45
        taosdemo.useDatabase();
Z
zyyang 已提交
46
        // create super table
47
        taosdemo.createSuperTable();
Z
zyyang 已提交
48
        // create sub tables
49
        taosdemo.createTableMultiThreads();
Z
zyyang 已提交
50 51

        boolean infinite = Arrays.asList(args).contains("--infinite");
52
        if (infinite) {
Z
zyyang 已提交
53
            logger.info("!!! Infinite Insert Mode Started. !!!");
54 55
            taosdemo.insertInfinite();
        } else {
Z
zyyang 已提交
56
            // insert into table
57
            taosdemo.insertMultiThreads();
Z
zyyang 已提交
58
            // select from sub table
Z
zyyang 已提交
59 60 61
            taosdemo.selectFromTableLimit();
            taosdemo.selectCountFromTable();
            taosdemo.selectAvgMinMaxFromTable();
Z
zyyang 已提交
62 63 64
            // select last from
            taosdemo.selectLastFromTable();
            // select from super table
Z
zyyang 已提交
65 66 67
            taosdemo.selectFromSuperTableLimit();
            taosdemo.selectCountFromSuperTable();
            taosdemo.selectAvgMinMaxFromSuperTable();
Z
zyyang 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
            //select avg ,max from stb where tag
            taosdemo.selectAvgMinMaxFromSuperTableWhereTag();
            //select last from stb where location = ''
            taosdemo.selectLastFromSuperTableWhere();
            // select group by
            taosdemo.selectGroupBy();
            // select like
            taosdemo.selectLike();
            // select where ts >= ts<=
            taosdemo.selectLastOneHour();
            taosdemo.selectLastOneDay();
            taosdemo.selectLastOneWeek();
            taosdemo.selectLastOneMonth();
            taosdemo.selectLastOneYear();

Z
zyyang 已提交
83
            // drop super table
Z
change  
zyyang 已提交
84
            if (config.dropTable)
85 86 87 88 89 90 91 92 93 94 95 96
                taosdemo.dropSuperTable();
            taosdemo.close();
        }
    }


    /**
     * establish the connection
     */
    private void init() {
        try {
            Class.forName("com.taosdata.jdbc.TSDBDriver");
Z
zyyang 已提交
97
            connection = ConnectionFactory.build(config);
98 99 100 101
            if (connection != null)
                logger.info("[ OK ] Connection established.");
        } catch (ClassNotFoundException | SQLException e) {
            logger.error(e.getMessage());
Z
change  
zyyang 已提交
102
            throw new RuntimeException("connection failed: " + config.host);
103 104 105 106 107 108 109
        }
    }

    /**
     * create database
     */
    private void createDatabase() {
Z
change  
zyyang 已提交
110
        String sql = SqlSpeller.createDatabaseSQL(config.database, config.keep, config.days);
111 112 113
        execute(sql);
    }

Z
zyyang 已提交
114 115 116
    /**
     * drop database
     */
117
    private void dropDatabase() {
Z
change  
zyyang 已提交
118
        String sql = SqlSpeller.dropDatabaseSQL(config.database);
119 120 121 122 123 124 125
        execute(sql);
    }

    /**
     * use database
     */
    private void useDatabase() {
Z
change  
zyyang 已提交
126
        String sql = SqlSpeller.useDatabaseSQL(config.database);
127 128 129
        execute(sql);
    }

Z
zyyang 已提交
130 131 132
    /**
     * create super table
     */
133
    private void createSuperTable() {
Z
change  
zyyang 已提交
134
        String sql = SqlSpeller.createSuperTableSQL(config.superTable);
135 136 137 138 139 140 141 142
        execute(sql);
    }

    /**
     * create table use super table with multi threads
     */
    private void createTableMultiThreads() {
        try {
Z
change  
zyyang 已提交
143
            final int tableSize = (int) (config.numOfTables / config.numOfThreadsForCreate);
144
            List<Thread> threads = new ArrayList<>();
Z
change  
zyyang 已提交
145
            for (int i = 0; i < config.numOfThreadsForCreate; i++) {
146 147 148 149 150 151 152
                Thread thread = new Thread(new CreateTableTask(config, i * tableSize, tableSize), "Thread-" + i);
                threads.add(thread);
                thread.start();
            }
            for (Thread thread : threads) {
                thread.join();
            }
Z
zyyang 已提交
153
            logger.info("<<< Multi Threads create table finished.");
154 155 156 157 158 159
        } catch (InterruptedException e) {
            logger.error(e.getMessage());
            e.printStackTrace();
        }
    }

Z
zyyang 已提交
160 161 162
    /**
     * insert data infinitely
     */
163 164 165 166 167
    private void insertInfinite() {
        try {
            final long startDatetime = TimeStampUtil.datetimeToLong("2005-01-01 00:00:00.000");
            final long finishDatetime = TimeStampUtil.datetimeToLong("2030-01-01 00:00:00.000");

Z
change  
zyyang 已提交
168
            final int tableSize = (int) (config.numOfTables / config.numOfThreadsForInsert);
169
            List<Thread> threads = new ArrayList<>();
Z
change  
zyyang 已提交
170
            for (int i = 0; i < config.numOfThreadsForInsert; i++) {
171 172 173 174 175 176 177
                Thread thread = new Thread(new InsertTableDatetimeTask(config, i * tableSize, tableSize, startDatetime, finishDatetime), "Thread-" + i);
                threads.add(thread);
                thread.start();
            }
            for (Thread thread : threads) {
                thread.join();
            }
Z
zyyang 已提交
178
            logger.info("<<< Multi Threads insert table finished.");
179 180 181 182 183 184 185 186
        } catch (InterruptedException e) {
            logger.error(e.getMessage());
            e.printStackTrace();
        }
    }

    private void insertMultiThreads() {
        try {
Z
change  
zyyang 已提交
187 188
            final int tableSize = (int) (config.numOfTables / config.numOfThreadsForInsert);
            final int numberOfRecordsPerTable = (int) config.numOfRowsPerTable;
189
            List<Thread> threads = new ArrayList<>();
Z
change  
zyyang 已提交
190
            for (int i = 0; i < config.numOfThreadsForInsert; i++) {
191 192 193 194 195 196 197
                Thread thread = new Thread(new InsertTableTask(config, i * tableSize, tableSize, numberOfRecordsPerTable), "Thread-" + i);
                threads.add(thread);
                thread.start();
            }
            for (Thread thread : threads) {
                thread.join();
            }
Z
zyyang 已提交
198
            logger.info("<<< Multi Threads insert table finished.");
199 200 201 202 203 204
        } catch (InterruptedException e) {
            logger.error(e.getMessage());
            e.printStackTrace();
        }
    }

Z
zyyang 已提交
205
    private void selectFromTableLimit() {
Z
change  
zyyang 已提交
206
        String sql = SqlSpeller.selectFromTableLimitSQL(config.database, config.prefixOfTable, 1, 10, 0);
Z
zyyang 已提交
207 208 209 210
        executeQuery(sql);
    }

    private void selectCountFromTable() {
Z
change  
zyyang 已提交
211
        String sql = SqlSpeller.selectCountFromTableSQL(config.database, config.prefixOfTable, 1);
Z
zyyang 已提交
212 213 214 215
        executeQuery(sql);
    }

    private void selectAvgMinMaxFromTable() {
Z
change  
zyyang 已提交
216
        String sql = SqlSpeller.selectAvgMinMaxFromTableSQL("current", config.database, config.prefixOfTable, 1);
Z
zyyang 已提交
217 218 219
        executeQuery(sql);
    }

Z
zyyang 已提交
220
    private void selectLastFromTable() {
Z
change  
zyyang 已提交
221
        String sql = SqlSpeller.selectLastFromTableSQL(config.database, config.prefixOfTable, 1);
Z
zyyang 已提交
222 223 224
        executeQuery(sql);
    }

Z
zyyang 已提交
225
    private void selectFromSuperTableLimit() {
Z
change  
zyyang 已提交
226
        String sql = SqlSpeller.selectFromSuperTableLimitSQL(config.database, config.superTable, 10, 0);
Z
zyyang 已提交
227 228 229 230
        executeQuery(sql);
    }

    private void selectCountFromSuperTable() {
Z
change  
zyyang 已提交
231
        String sql = SqlSpeller.selectCountFromSuperTableSQL(config.database, config.superTable);
Z
zyyang 已提交
232 233 234 235
        executeQuery(sql);
    }

    private void selectAvgMinMaxFromSuperTable() {
Z
change  
zyyang 已提交
236
        String sql = SqlSpeller.selectAvgMinMaxFromSuperTableSQL("current", config.database, config.superTable);
237 238 239
        executeQuery(sql);
    }

Z
zyyang 已提交
240
    private void selectAvgMinMaxFromSuperTableWhereTag() {
Z
change  
zyyang 已提交
241
        String sql = SqlSpeller.selectAvgMinMaxFromSuperTableWhere("current", config.database, config.superTable);
Z
zyyang 已提交
242 243 244 245
        executeQuery(sql);
    }

    private void selectLastFromSuperTableWhere() {
Z
change  
zyyang 已提交
246
        String sql = SqlSpeller.selectLastFromSuperTableWhere("current", config.database, config.superTable);
Z
zyyang 已提交
247 248 249 250
        executeQuery(sql);
    }

    private void selectGroupBy() {
Z
change  
zyyang 已提交
251
        String sql = SqlSpeller.selectGroupBy("current", config.database, config.superTable);
Z
zyyang 已提交
252 253 254 255
        executeQuery(sql);
    }

    private void selectLike() {
Z
change  
zyyang 已提交
256
        String sql = SqlSpeller.selectLike(config.database, config.superTable);
Z
zyyang 已提交
257 258 259 260
        executeQuery(sql);
    }

    private void selectLastOneHour() {
Z
change  
zyyang 已提交
261
        String sql = SqlSpeller.selectLastOneHour(config.database, config.superTable);
Z
zyyang 已提交
262 263 264 265
        executeQuery(sql);
    }

    private void selectLastOneDay() {
Z
change  
zyyang 已提交
266
        String sql = SqlSpeller.selectLastOneDay(config.database, config.superTable);
Z
zyyang 已提交
267 268 269 270
        executeQuery(sql);
    }

    private void selectLastOneWeek() {
Z
change  
zyyang 已提交
271
        String sql = SqlSpeller.selectLastOneWeek(config.database, config.superTable);
Z
zyyang 已提交
272 273 274 275
        executeQuery(sql);
    }

    private void selectLastOneMonth() {
Z
change  
zyyang 已提交
276
        String sql = SqlSpeller.selectLastOneMonth(config.database, config.superTable);
Z
zyyang 已提交
277 278 279 280
        executeQuery(sql);
    }

    private void selectLastOneYear() {
Z
change  
zyyang 已提交
281
        String sql = SqlSpeller.selectLastOneYear(config.database, config.superTable);
Z
zyyang 已提交
282 283 284
        executeQuery(sql);
    }

285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
    private void close() {
        try {
            if (connection != null) {
                this.connection.close();
                logger.info("connection closed.");
            }
        } catch (SQLException e) {
            logger.error(e.getMessage());
            e.printStackTrace();
        }
    }

    /**
     * drop super table
     */
    private void dropSuperTable() {
Z
change  
zyyang 已提交
301
        String sql = SqlSpeller.dropSuperTableSQL(config.database, config.superTable);
302 303 304 305 306 307 308 309 310 311 312 313 314
        execute(sql);
    }

    /**
     * execute sql, use this method when sql is create, alter, drop..
     */
    private void execute(String sql) {
        try (Statement statement = connection.createStatement()) {
            long start = System.currentTimeMillis();
            boolean execute = statement.execute(sql);
            long end = System.currentTimeMillis();
            printSql(sql, execute, (end - start));
        } catch (SQLException e) {
Z
zyyang 已提交
315
            logger.error("ERROR execute SQL ===> " + sql);
316 317 318 319 320
            logger.error(e.getMessage());
            e.printStackTrace();
        }
    }

Z
zyyang 已提交
321
    private static void printSql(String sql, boolean succeed, long cost) {
Z
zyyang 已提交
322
        System.out.println("[ " + (succeed ? "OK" : "ERROR!") + " ] time cost: " + cost + " ms, execute statement ====> " + sql);
323 324 325 326 327 328 329 330 331 332
    }

    private void executeQuery(String sql) {
        try (Statement statement = connection.createStatement()) {
            long start = System.currentTimeMillis();
            ResultSet resultSet = statement.executeQuery(sql);
            long end = System.currentTimeMillis();
            printSql(sql, true, (end - start));
            printResult(resultSet);
        } catch (SQLException e) {
Z
zyyang 已提交
333
            logger.error("ERROR execute SQL ===> " + sql);
334 335 336 337 338
            logger.error(e.getMessage());
            e.printStackTrace();
        }
    }

Z
zyyang 已提交
339
    private static void printResult(ResultSet resultSet) throws SQLException {
340 341 342 343 344 345 346 347
        ResultSetMetaData metaData = resultSet.getMetaData();
        while (resultSet.next()) {
            StringBuilder sb = new StringBuilder();
            for (int i = 1; i <= metaData.getColumnCount(); i++) {
                String columnLabel = metaData.getColumnLabel(i);
                String value = resultSet.getString(i);
                sb.append(columnLabel + ": " + value + "\t");
            }
Z
zyyang 已提交
348
            System.out.println(sb.toString());
349 350 351 352
        }
    }

}