读取2G的大json文件后存入数据库





!!!!!
  Files.lines(Paths.get("D:\\workspace\\example\\load\\src\\main\\resources\\baseFieldDataGood.json"), StandardCharsets.UTF_8)
                .map(line -> JSON.parseObject(line, new TypeReference<FileDataLine2>() {
                }))
                .forEach(lineData -> {
                    System.out.println(lineData);
                    convterBean(lineData);
                });




!!!!!!






package load.service.impl;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import load.domain.JsonBean;
import load.mapper.HzHhmdFgwMapper;
import load.service.LoadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LoadServiceImpl implements LoadService {

    @Autowired
    private HzHhmdFgwMapper hzHhmdFgwMapper;


    @Override
    public void updateToBase() throws IOException {
        //splitFile();

        for (int i = 1; i < 501; i++) {
            String path = ("D:\\linshi\\part" + i + ".txt");
            List<JsonBean> jsonBeans = readJsonFile(path);

            System.out.println(jsonBeans);
//            //hzHhmdFgwMapper.insertList(hzHhmdFgws);
        }

    }

    /**
     * @param fileName 读取文件后转成json
     * @return
     */
    public List<JsonBean> readJsonFile(String fileName) {

        List<JsonBean> jsonBeans = new ArrayList<>();

        try {
            File jsonFile = new File(fileName);
            FileReader fileReader = new FileReader(jsonFile);

            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(jsonFile), "utf-8"));
            String line;

            while ((line = reader.readLine()) != null && reader.readLine() != "") {
                JsonBean jsonBean = JSON.parseObject(line, new TypeReference<JsonBean>() {
                });
                jsonBeans.add(jsonBean);
            }

            fileReader.close();
            reader.close();
            return jsonBeans;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }


    /**
     * @throws IOException 获取文件并且拆分,保存到本地
     */
    public void splitFile() throws IOException {
        //获取json位置
        String path = LoadServiceImpl.class.getClassLoader().getResource("baseFieldDataGood.json").getPath();
        String realPath = path.substring(1);


        long timer = System.currentTimeMillis();
        int bufferSize = 20 * 1024 * 1024;//设读取文件的缓存为20MB

        //建立缓冲文本输入流
        //File file = new File("D:\\workspace\\example\\load\\src\\main\\resources\\baseFieldDataGood.json");
        File file = new File(realPath);
        FileInputStream fileInputStream = new FileInputStream(file);
        BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
        InputStreamReader inputStreamReader = new InputStreamReader(bufferedInputStream);
        BufferedReader input = new BufferedReader(inputStreamReader, bufferSize);

        int splitNum = 501 - 1;//要分割的块数减一
        int fileLines = 236692;//输入文件的行数
        long perSplitLines = fileLines / splitNum;//每个块的行数
        for (int i = 0; i <= splitNum; ++i) {
            //分割
            //每个块建立一个输出
            FileWriter output = new FileWriter("D:\\linshi\\part" + i + ".txt");
            String line = null;
            //逐行读取,逐行输出
            for (long lineCounter = 0; lineCounter < perSplitLines && (line = input.readLine()) != null; ++lineCounter) {
                output.append(line + "\r");
            }
            output.flush();
            output.close();
            output = null;
        }
        input.close();
        timer = System.currentTimeMillis() - timer;
        System.out.println("处理时间:" + timer);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值