【问题标题】:Streaming large dataset as CSV in Spring Boot在 Spring Boot 中将大型数据集流式传输为 CSV
【发布时间】:2020-12-13 21:45:28
【问题描述】:

我有一个 Spring Boot 应用程序 (v. 2.3.3),我需要通过 REST API 作为 CSV 文件从数据库 (Postgres) 提供大型数据集。 到目前为止,只处理了一小部分数据,这些数据可以作为集合累积并立即传输。现在,我想让服务器为大量数据做好准备。

使用 CRUD 存储库中的查询将数据作为投影(而不是实体)检索。

public interface IMeasurementsRepo extends CrudRepository<Measurement, Long> {
    @Query("SELECT .... ")
    public Iterable<IMeasurementProjection> getMeasurementHistory(...);
}

并且投影定义如下:

public interface IMeasurementProjection {
    public Timestamp getTimestamp();
    public Long getSensorId();
    public Group getGroup();
    public Integer getNum();
    public Signal getSignal();
    public Double getStrength();
}

检索后,每个投影都应转换为 CSV 文件的一行并发送给客户端。转换(格式化)不是问题。我的问题是如何将请求的数据作为流传递。

很可能,我需要的是ResponseEntity&lt;StreamingResponseBody&gt; 或Flux,但我不知道如何在我的情况下应用这些。

感谢任何提示或参考。

【问题讨论】:

    标签: spring-boot csv java-stream rest


    【解决方案1】:

    如果你使用的是 spring-webflux,那么 r2dbc 可以实现你想要的,以下是依赖、配置和工作示例

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-r2dbc</artifactId>
    </dependency>
    

    春季网络流量

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
    </dependency>
    

    application.properties

    spring.r2dbc.url=r2dbc:postgresql://172.17.0.2:5432/streaming-db-server
    spring.r2dbc.username=postgres
    spring.r2dbc.password=ungabunga
    

    这里是示例代码:

    package com.example.postgres.batchinsertandstreaming.demo;
    
    import lombok.Getter;
    import lombok.Setter;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.data.relational.core.mapping.Table;
    import org.springframework.data.repository.reactive.ReactiveCrudRepository;
    import org.springframework.http.MediaType;
    import org.springframework.stereotype.Repository;
    import org.springframework.stereotype.Service;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    import reactor.core.publisher.Flux;
    
    import javax.persistence.Id;
    
    
    @SpringBootApplication
    public class PostgresBatchStreamingDataApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(PostgresBatchStreamingDataApplication.class, args);
        }
    
    }
    
    @RequestMapping("streaming")
    @RestController
    class StreamingSalesRecord {
    
        private final StreamingSaleRecordService service;
    
        StreamingSalesRecord(StreamingSaleRecordService service) {
            this.service = service;
        }
    
        @GetMapping(produces = MediaType.APPLICATION_STREAM_JSON_VALUE)
        public Flux<String> get() {
            return service.all();
        }
    }
    
    @Service
    class StreamingSaleRecordService {
    
        private final StreamingSalesRecordRepository repository;
    
        StreamingSaleRecordService(StreamingSalesRecordRepository repository) {
            this.repository = repository;
        }
    
        public Flux<String> all() {
            return repository.findAll()
                    .map(object -> String.join(",", new String[]{object.getId().toString(), object.getCountry(), object.getRegion()}))
                    .map(object -> object + "\n");
        }
    }
    
    @Repository
    interface StreamingSalesRecordRepository extends ReactiveCrudRepository<SalesRecordReactive, Long> {
    }
    
    @Getter
    @Setter
    @Table(value = "sales_record")
    class SalesRecordReactive {
    
        @Id
        private Long id;
    
        private String region;
    
        private String country;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-08
      • 1970-01-01
      • 1970-01-01
      • 2020-10-07
      • 1970-01-01
      • 2015-12-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多