欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用Spring?Batch實(shí)現(xiàn)大數(shù)據(jù)處理的操作方法

 更新時(shí)間:2024年07月30日 15:04:12   作者:@聚娃科技  
通過使用Spring?Batch,我們可以高效地處理大規(guī)模數(shù)據(jù),本文介紹了如何配置和實(shí)現(xiàn)一個(gè)基本的Spring?Batch作業(yè),包括讀取數(shù)據(jù)、處理數(shù)據(jù)和寫入數(shù)據(jù)的全過程,感興趣的朋友跟隨小編一起看看吧

使用Spring Batch實(shí)現(xiàn)大數(shù)據(jù)處理

大家好,我是微賺淘客系統(tǒng)3.0的小編,是個(gè)冬天不穿秋褲,天冷也要風(fēng)度的程序猿!今天我們來探討如何使用Spring Batch實(shí)現(xiàn)大數(shù)據(jù)處理。Spring Batch是一個(gè)輕量級(jí)的批處理框架,旨在幫助開發(fā)者簡(jiǎn)化大數(shù)據(jù)處理流程,提供了強(qiáng)大的任務(wù)管理、分片、并行處理等功能。

一、Spring Batch簡(jiǎn)介

Spring Batch是Spring框架的一部分,專門用于批處理。它提供了可重用的功能,如事務(wù)管理、資源管理、作業(yè)調(diào)度和并行處理等。通過Spring Batch,我們可以輕松地處理大規(guī)模的數(shù)據(jù),并確保處理的可靠性和可擴(kuò)展性。

二、Spring Batch基本概念

在開始編寫代碼之前,了解Spring Batch的幾個(gè)核心概念是必要的:

  • Job:一個(gè)批處理作業(yè),包含一個(gè)或多個(gè)Step。
  • Step:批處理中的一個(gè)步驟,包含ItemReader、ItemProcessor和ItemWriter。
  • ItemReader:從數(shù)據(jù)源讀取數(shù)據(jù)。
  • ItemProcessor:處理讀取的數(shù)據(jù)。
  • ItemWriter:將處理后的數(shù)據(jù)寫入目標(biāo)數(shù)據(jù)源。

三、Spring Batch項(xiàng)目配置

創(chuàng)建Maven項(xiàng)目

首先,創(chuàng)建一個(gè)新的Maven項(xiàng)目,并在pom.xml中添加Spring Batch的依賴:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-batch</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.hsqldb</groupId>
        <artifactId>hsqldb</artifactId>
        <scope>runtime</scope>
    </dependency>
</dependencies>

配置數(shù)據(jù)源

application.properties中配置數(shù)據(jù)源:

spring.datasource.url=jdbc:hsqldb:mem:testdb
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.hsqldb.jdbc.JDBCDriver
spring.batch.initialize-schema=always

四、實(shí)現(xiàn)Spring Batch Job

定義數(shù)據(jù)模型

創(chuàng)建一個(gè)簡(jiǎn)單的實(shí)體類,例如Person

package cn.juwatech.batch;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Person {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String firstName;
    private String lastName;
    // getters and setters
}

ItemReader實(shí)現(xiàn)

實(shí)現(xiàn)一個(gè)從CSV文件讀取數(shù)據(jù)的ItemReader

package cn.juwatech.batch;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource;
public class BatchConfiguration {
    @Bean
    public FlatFileItemReader<Person> reader() {
        FlatFileItemReader<Person> reader = new FlatFileItemReader<>();
        reader.setResource(new ClassPathResource("sample-data.csv"));
        reader.setLineMapper(new DefaultLineMapper<Person>() {{
            setLineTokenizer(new DelimitedLineTokenizer() {{
                setNames(new String[] { "firstName", "lastName" });
            }});
            setFieldSetMapper(new BeanWrapperFieldSetMapper<Person>() {{
                setTargetType(Person.class);
            }});
        }});
        return reader;
    }
}

ItemProcessor實(shí)現(xiàn)

實(shí)現(xiàn)一個(gè)簡(jiǎn)單的ItemProcessor,將姓氏轉(zhuǎn)換為大寫:

package cn.juwatech.batch;
import org.springframework.batch.item.ItemProcessor;
public class PersonItemProcessor implements ItemProcessor<Person, Person> {
    @Override
    public Person process(Person person) throws Exception {
        person.setLastName(person.getLastName().toUpperCase());
        return person;
    }
}

ItemWriter實(shí)現(xiàn)

實(shí)現(xiàn)一個(gè)將數(shù)據(jù)寫入數(shù)據(jù)庫的ItemWriter

package cn.juwatech.batch;
import org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider;
import org.springframework.batch.item.database.JdbcBatchItemWriter;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.JdbcTemplate;
import javax.sql.DataSource;
public class BatchConfiguration {
    @Bean
    public JdbcBatchItemWriter<Person> writer(DataSource dataSource) {
        JdbcBatchItemWriter<Person> writer = new JdbcBatchItemWriter<>();
        writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>());
        writer.setSql("INSERT INTO person (first_name, last_name) VALUES (:firstName, :lastName)");
        writer.setDataSource(dataSource);
        return writer;
    }
}

配置Job和Step

配置批處理的Job和Step:

package cn.juwatech.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.core.launch.support.RunIdIncrementer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableBatchProcessing
public class BatchConfiguration {
    private final JobBuilderFactory jobBuilderFactory;
    private final StepBuilderFactory stepBuilderFactory;
    public BatchConfiguration(JobBuilderFactory jobBuilderFactory, StepBuilderFactory stepBuilderFactory) {
        this.jobBuilderFactory = jobBuilderFactory;
        this.stepBuilderFactory = stepBuilderFactory;
    }
    @Bean
    public Job importUserJob(JobCompletionNotificationListener listener, Step step1) {
        return jobBuilderFactory.get("importUserJob")
                .incrementer(new RunIdIncrementer())
                .listener(listener)
                .flow(step1)
                .end()
                .build();
    }
    @Bean
    public Step step1(JdbcBatchItemWriter<Person> writer) {
        return stepBuilderFactory.get("step1")
                .<Person, Person> chunk(10)
                .reader(reader())
                .processor(processor())
                .writer(writer)
                .build();
    }
    @Bean
    public PersonItemProcessor processor() {
        return new PersonItemProcessor();
    }
}

運(yùn)行批處理作業(yè)

創(chuàng)建一個(gè)Spring Boot應(yīng)用程序入口,啟動(dòng)批處理作業(yè):

package cn.juwatech.batch;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class BatchApplication implements CommandLineRunner {
    @Autowired
    private JobLauncher jobLauncher;
    @Autowired
    private Job job;
    public static void main(String[] args) {
        SpringApplication.run(BatchApplication.class, args);
    }
    @Override
    public void run(String... args) throws Exception {
        jobLauncher.run(job, new JobParameters());
    }
}

五、測(cè)試與驗(yàn)證

啟動(dòng)Spring Boot應(yīng)用程序后,檢查數(shù)據(jù)庫中的數(shù)據(jù),確保批處理作業(yè)正確執(zhí)行并寫入數(shù)據(jù)。

總結(jié)

通過使用Spring Batch,我們可以高效地處理大規(guī)模數(shù)據(jù)。本文介紹了如何配置和實(shí)現(xiàn)一個(gè)基本的Spring Batch作業(yè),包括讀取數(shù)據(jù)、處理數(shù)據(jù)和寫入數(shù)據(jù)的全過程。Spring Batch的強(qiáng)大功能和靈活性使其成為處理批處理任務(wù)的理想選擇。

本文著作權(quán)歸聚娃科技微賺淘客系統(tǒng)開發(fā)者團(tuán)隊(duì),轉(zhuǎn)載請(qǐng)注明出處!

到此這篇關(guān)于使用Spring Batch實(shí)現(xiàn)大數(shù)據(jù)處理的操作方法的文章就介紹到這了,更多相關(guān)Spring Batch大數(shù)據(jù)處理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringBoot使用RestTemplate實(shí)現(xiàn)HTTP請(qǐng)求詳解

    SpringBoot使用RestTemplate實(shí)現(xiàn)HTTP請(qǐng)求詳解

    這篇文章主要為大家詳細(xì)介紹了SpringBoot如何使用RestTemplate實(shí)現(xiàn)進(jìn)行HTTP請(qǐng)求,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-03-03
  • Java給JFrame窗口設(shè)置熱鍵的方法實(shí)現(xiàn)

    Java給JFrame窗口設(shè)置熱鍵的方法實(shí)現(xiàn)

    這篇文章主要介紹了Java給JFrame窗口設(shè)置熱鍵的方法實(shí)現(xiàn),文中通過示例代碼以及圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • 深入淺析Spring-boot-starter常用依賴模塊

    深入淺析Spring-boot-starter常用依賴模塊

    這篇文章主要介紹了Spring-boot-starter常用依賴模塊及spring boot的兩大優(yōu)點(diǎn),需要的朋友可以參考下
    2018-01-01
  • Lombok基本注解之@SneakyThrows的作用

    Lombok基本注解之@SneakyThrows的作用

    @SneakyThrows注解是由lombok為咱們封裝的,它能夠?yàn)樵蹅兊拇a生成一個(gè)try...catch塊,并把異常向上拋出來,下面這篇文章主要給大家介紹了關(guān)于Lombok基本注解之@SneakyThrows作用的相關(guān)資料,需要的朋友可以參考下
    2022-01-01
  • 詳解Java線程中常用操作

    詳解Java線程中常用操作

    這篇文章主要為大家詳細(xì)介紹了一下Java線程中的一些常用操作,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定幫助,需要的可以參考一下
    2022-05-05
  • Mybatis實(shí)現(xiàn)增刪改查及分頁查詢的方法

    Mybatis實(shí)現(xiàn)增刪改查及分頁查詢的方法

    MyBatis是支持普通SQL查詢,存儲(chǔ)過程和高級(jí)映射的優(yōu)秀持 久層框架,通過本文給大家介紹Mybatis實(shí)現(xiàn)增刪改查及分頁查詢的方法,感興趣的朋友一起學(xué)習(xí)吧
    2016-01-01
  • 解決springboot的JPA在Mysql8新增記錄失敗的問題

    解決springboot的JPA在Mysql8新增記錄失敗的問題

    這篇文章主要介紹了解決springboot的JPA在Mysql8新增記錄失敗的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • java并發(fā)編程JUC CountDownLatch線程同步

    java并發(fā)編程JUC CountDownLatch線程同步

    這篇文章主要介紹CountDownLatch是什么、CountDownLatch 如何工作、CountDownLatch 的代碼例子來展開對(duì)java并發(fā)編程JUC CountDownLatch線程同步,需要的朋友可以參考下面文章內(nèi)容
    2021-09-09
  • java常用工具類之Excel操作類及依賴包下載

    java常用工具類之Excel操作類及依賴包下載

    這篇文章主要介紹了java常用工具類Excel操作類及依賴包下載,需要的朋友可以參考下
    2014-07-07
  • Spring中的retry重試組件詳解

    Spring中的retry重試組件詳解

    這篇文章主要介紹了Spring中的retry重試組件詳解,Retry重試組件是一個(gè)處理重試邏輯的工具,可以在出現(xiàn)異常或失敗情況下自動(dòng)進(jìn)行重試操作,從而提高程序的穩(wěn)定性和可靠性,需要的朋友可以參考下
    2023-10-10

最新評(píng)論