dataWarehouseOss專案總結(二)_讀取日誌資訊寫入kafka

Liu_Shihao發表於2020-09-28

Controller

package com.dataWarehouseOss.controller;

import com.dataWarehouseOss.service.ReadLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author :LiuShihao
 * @date :Created in 2020/9/27 1:38 下午
 * @desc :
 */
@Slf4j
@RestController
public class ReadLogController {
    @Autowired
    ReadLogService logService;

    @PostMapping("/readlog")
    public String readLog(@RequestParam("file") MultipartFile file) throws IOException {
        log.info(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss :"))+"-------------執行補錄日誌資料-------------");
        logService.readLog(file);
        return "補錄資料成功";
    }
}

ServiceImpl

package com.dataWarehouseOss.service.Impl;

import com.dataWarehouseOss.service.ReadLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;

/**
 * @author :LiuShihao
 * @date :Created in 2020/9/27 1:40 下午
 * @desc :
 */
@Slf4j
@Service
public class ReadLogServerImpl implements ReadLogService {
    @Autowired
    KafkaTemplate kafkaTemplate;




    @Override
    public void readLog(MultipartFile multipartFile) throws IOException {

        String[] split = multipartFile.getOriginalFilename().split("\\.");

        File tempFile = File.createTempFile(split[0] + split[1], ".log");

        multipartFile.transferTo(tempFile);

        FileInputStream fis = new FileInputStream(tempFile);

        BufferedReader br = new BufferedReader(new InputStreamReader(fis));

        String line = null;
        while ((line = br.readLine()) != null) {
            String substring = line.substring(line.indexOf("{"));
            System.out.println("傳送日誌資料到kafka:"+substring);
            kafkaTemplate.send("hw_data",substring);
        }
            fis.close();
            br.close();
            tempFile.deleteOnExit();
            System.out.println("結束--------------------------------------");

        }

}

總結

通過Postman呼叫http://localhost:8081/readlog POST方法 ,表單形式 提交檔案到後臺,
建立一個臨時檔案tempFile。

		String[] split = multipartFile.getOriginalFilename().split("\\.");

        File tempFile = File.createTempFile(split[0] + split[1], ".log");

將MultipartFile轉換成File檔案

 multipartFile.transferTo(tempFile);

在獲取BufferedReader

		FileInputStream fis = new FileInputStream(tempFile);

        BufferedReader br = new BufferedReader(new InputStreamReader(fis));

通過br.readLine()來逐行讀取。擷取字串,傳送kafka。

相關文章