• Java实现http大文件流读取并批量插入数据库


    1、概述

    • 请求远程大文本,使用流的方式进行返回。需要设置http链接的超时时间
    • 循环插入到List中,使用mybatis-plus批量插入到mysql中

    2、需求

    • 两台服务器
    • 大文件放到其中一台服务器上
    • 另外一台服务器以文件流的形式请求数据
    • 批量插入到数据库中

    3、实现思路

    主要包括几个技术点:文件流提供;大文件数据读取;批量插入到数据库

    1、文件流提供

    使用springboot提供静态文件

    spring.resources.static-locations=file:/home/finance/h5/
    

    2、大文件数据读取

    设置HttpURLConnection的超时时间

    3、批量插入数据库

    使用mybtis-plus的批量插入,自定义sql

    1000条分批次插入

    4、代码

    1、大文件数据读取

    public String doGet(String httpurl) {
            TestModel model=new TestModel();
            List<TestModel> list=new ArrayList<>();
            HttpURLConnection connection = null;
            InputStream is = null;
            BufferedReader br = null;
            String result = null;// 返回结果字符串
            try {
                // 创建远程url连接对象
                URL url = new URL(httpurl);
                // 通过远程url连接对象打开一个连接,强转成httpURLConnection类
                connection = (HttpURLConnection) url.openConnection();
                // 设置连接方式:get
                connection.setRequestMethod("GET");
                // 设置连接主机服务器的超时时间:15000毫秒
                connection.setConnectTimeout(150000000);
                // 设置读取远程返回的数据时间:60000毫秒
                connection.setReadTimeout(600000000);
    
                // 发送请求
                connection.connect();
                // 通过connection连接,获取输入流
                if (connection.getResponseCode() == 200) {
                    is = connection.getInputStream();
                    // 封装输入流is,并指定字符集
                    br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                    // 存放数据
    
                    String temp = null;
                    int i=0;
                    while ((temp = br.readLine()) != null) {
                        if (i>0&&i % 1000 ==0){
                            testMapper.insertBatch(list);//批量导入
                            list=new ArrayList<>();
                        }
                        model=new TestModel();
                        model.setId(String.valueOf(i++));
                        model.setContent(temp);
                        model.setCreatedTime(DateUtil.date());
                        list.add(model);
                    }
    
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                // 关闭资源
                if (null != br) {
                    try {
                        br.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
    
                if (null != is) {
                    try {
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
    
                connection.disconnect();// 关闭远程连接
            }
    
            return result;
        }
    

    2、批量插入

    1、controller

        @GetMapping("/addtmp")
        public   void addtmp() {
            String url = "http://10.99.67.16:8081/test10wan.txt";
            long startTime = System.currentTimeMillis();//获取开始时间
            httpURLConnHelper.doGet(url);
            long endTime = System.currentTimeMillis();//获取结束时间
            System.out.println("测试代码块共耗时" + ((endTime - startTime)/1000) + "秒");//输出程序运行时间
            TestModel model=new TestModel();
            model.setId("00000000000000000000001");
            model.setContent("测试代码块共耗时" + ((endTime - startTime)/1000) + "秒");
            model.setCreatedTime(DateUtil.date());
            testService.save(model);
        }
    

    2、mapper

    @Repository
    @Mapper
    public interface TestMapper extends BaseMapper<TestModel> {
    
        @Insert("<script>" +
                "INSERT INTO tmp_chenn_test(id,content,createdTime)VALUES" +
                "<foreach collection='testList' item='testModel'   separator=','> " +
                "(#{testModel.id},#{testModel.content},#{testModel.createdTime})" +
                "</foreach> " +
                "</script>")//
        boolean insertBatch(List<TestModel> testList);
    
    }
    
  • 相关阅读:
    linux--->curl
    linux--->定时任务
    php--->无限级分类
    php--->自己封装的简易版mvc框架
    php--->单例模式封装mysql操作类
    Linux使用退格键时出现^H ^?解决方法
    错误日志
    linux下redis服务器安装使用 安装php的redis扩展 安装laravel下的redis
    php system()
    laravel redis的使用
  • 原文地址:https://www.cnblogs.com/chenn/p/16358742.html
Copyright © 2020-2023  润新知