Akashic Records

Spring Batch SFTP SSH ItemReader, ItemWriter 본문

Spring.io

Spring Batch SFTP SSH ItemReader, ItemWriter

Andrew's Akashic Records 2023. 4. 17. 23:46
728x90

spring batch 프로젝트에서 sftp, ssh 프로토콜로 itemwrite, itemread기능을 구현하려고 하는데 코드 만들어줘
Spring Batch를 사용하여 SFTP/SSH 프로토콜로 파일을 읽고 쓰는 기능을 구현하려면, ItemReader 및 ItemWriter 인터페이스를 구현하는 커스텀 컴포넌트를 작성해야 합니다. 또한, jsch 라이브러리를 사용하여 SFTP/SSH 연결 및 파일 작업을 수행할 수 있습니다.

먼저, 프로젝트에 jsch 라이브러리를 추가하세요.

Maven:

<dependency>
    <groupId>com.jcraft</groupId>
    <artifactId>jsch</artifactId>
    <version>0.1.55</version>
</dependency>


Gradle:

implementation 'com.jcraft:jsch:0.1.55'

다음으로, SFTP를 사용하여 파일을 읽는 ItemReader를 작성합니다.

import com.jcraft.jsch.*;
import org.springframework.batch.item.ItemReader;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class SftpItemReader implements ItemReader<String> {

    private BufferedReader reader;
    private String host;
    private int port;
    private String username;
    private String password;
    private String remoteFilePath;

    public SftpItemReader(String host, int port, String username, String password, String remoteFilePath) {
        this.host = host;
        this.port = port;
        this.username = username;
        this.password = password;
        this.remoteFilePath = remoteFilePath;
        init();
    }

    private void init() {
        try {
            JSch jsch = new JSch();
            Session session = jsch.getSession(username, host, port);
            session.setConfig("StrictHostKeyChecking", "no");
            session.setPassword(password);
            session.connect();

            ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp");
            channelSftp.connect();

            InputStream inputStream = channelSftp.get(remoteFilePath);
            reader = new BufferedReader(new InputStreamReader(inputStream));
        } catch (JSchException | SftpException e) {
            e.printStackTrace();
        }
    }

    @Override
    public String read() {
        try {
            return reader.readLine();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}


SFTP를 사용하여 파일을 작성하는 ItemWriter를 작성합니다.

import com.jcraft.jsch.*;
import org.springframework.batch.item.ItemWriter;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.List;

public class SftpItemWriter implements ItemWriter < String > {

    private BufferedWriter writer;
    private String host;
    private int port;
    private String username;
    private String password;
    private String remoteFilePath;

    public SftpItemWriter(String host, int port, String username, String password, String remoteFilePath) {
        this.host = host;
        this.port = port;
        this.username = username;
        this.password = password;
        this.remoteFilePath = remoteFilePath;
        init();
    }

    private void init() {
        try {
            JSch jsch = new JSch();
            Session session = jsch.getSession(username, host, port);
            session.setConfig("StrictHostKeyChecking", "no");
            session.setPassword(password);
            session.connect();

            ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp");
            channelSftp.connect();

            OutputStream outputStream = channelSftp.put(remoteFilePath);
            writer = new BufferedWriter(new OutputStreamWriter(outputStream));
        } catch (JSchException | SftpException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void write(List << ? extends String > items) {
        try {
            for (String item: items) {
                writer.write(item);
                writer.newLine();
            }
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 

이제 `SftpItemReader` 및 `SftpItemWriter`를 사용하여 Spring Batch 작업에서 파일을 읽고 쓸 수 있습니다. 작업 구성 클래스에서 이 컴포넌트를 사용하려면 다음과 같이 설정하십시오.

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableBatchProcessing
public class SftpBatchJobConfiguration {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Bean
    public Job sftpJob(Step sftpStep) {
        return jobBuilderFactory.get("sftpJob")
                .start(sftpStep)
                .build();
    }

    @Bean
    public Step sftpStep(SftpItemReader sftpItemReader, SftpItemWriter sftpItemWriter) {
        return stepBuilderFactory.get("sftpStep")
                .<String, String>chunk(10)
                .reader(sftpItemReader)
                .processor(itemProcessor())
                .writer(sftpItemWriter)
                .build();
    }

    @Bean
    public ItemProcessor<String, String> itemProcessor() {
        return item -> item;
    }

    @Bean
    public SftpItemReader sftpItemReader() {
        return new SftpItemReader("your_host", 22, "your_username", "your_password", "path/to/remote/file");
    }

    @Bean
    public SftpItemWriter sftpItemWriter() {
        return new SftpItemWriter("your_host", 22, "your_username", "your_password", "path/to/remote/output");
    }
}

 

이제 SftpItemReader 및 SftpItemWriter를 사용하여 Spring Batch 작업에서 SFTP로 파일을 읽고 쓸 수 있습니다. 필요에 따라 이 클래스에 추가적인 메서드를 작성하여 사용할 수 있습니다.

728x90
Comments