/** * @author itwen * @Description * @create 2021-08-05 17:34 */@SpringBootTestpublic class Instl { @Autowired RatingMapper mapper; String path = "D:\\ratings.csv"; /** * 初始化数据集 * * @return 数据集 * @throws IOException */ List<Rating> initdata() throws IOException { BufferedReader read = new BufferedReader(new FileReader(path)); Rating rating; List<Rating> ratings = new ArrayList<>(); boolean sign = false; for (int i = 0; i < 300000; i++) { String line = read.readLine(); StringTokenizer st = new StringTokenizer(line, ","); if (st.hasMoreTokens() && sign) { rating = new Rating(); rating.setUserId(Integer.valueOf(st.nextToken().trim())); rating.setMovieId(Integer.valueOf(st.nextToken().trim())); rating.setRating(Double.valueOf(st.nextToken().trim())); rating.setTime(Integer.valueOf(st.nextToken().trim())); ratings.add(rating); } else { sign = true; } } read.close(); return ratings; } /** * 单机 * 采用mybatis方式Batch插入10W条 耗时=182662毫秒 * * @throws IOException */ @Test void inserUrlLog() throws IOException { long startTime = System.currentTimeMillis(); mapper.insertUrlLog(initdata()); long entTime = System.currentTimeMillis(); System.out.println("当前程序耗时:" + (entTime - startTime) + "ms"); } /** * 单机 * 采用官方http-JDBC方式Batch插入10W条 耗时=1026毫秒 * 采用官方http-JDBC方式Batch插入30W条 耗时=5217毫秒 * 应注意分区数,分区越多速度越慢 * 另外还有第三方优化过的http、tcp的JDBC驱动 * * @throws IOException */ @Test void inserCkJDBC() throws IOException, SQLException { List<Rating> initdata = initdata(); String url = "jdbc:clickhouse://47.115.29.129:8123/test"; ClickHouseProperties properties = new ClickHouseProperties(); properties.setUser("ckadmin"); properties.setPassword("zjzl@2021"); ClickHouseDataSource source = new ClickHouseDataSource(url, properties); long startTime = System.currentTimeMillis(); final String sql = "INSERT INTO test.ratings(userId,movieId,rating,initTime) VALUES(?,?,?,?)";// ClickHouseConnection connection = clickHouseDataSource.getConnection();// ClickHouseStatement statement = connection.createStatement();// statement.write().sql("INSERT INTO default.my_table (a,b,c)"); Connection connection = source.getConnection(); connection.setAutoCommit(false); PreparedStatement statement = connection.prepareStatement(sql); for (Rating rating : initdata) { statement.setLong(1, rating.getUserId()); statement.setLong(2, rating.getMovieId()); statement.setDouble(3, rating.getRating()); statement.setLong(4, rating.getTime()); statement.addBatch(); } statement.executeBatch(); connection.commit(); statement.close(); connection.close(); long entTime = System.currentTimeMillis(); System.out.println("当前程序耗时:" + (entTime - startTime) + "ms"); }}