[英]How to create and download large data set as CSV file from rest services by implementing StreamingOutput
I have 20k records in db, I need to export all data into CSV file by implementing StreamingOutput from rest service. 我在db中有2万条记录,我需要通过从rest服务实现StreamingOutput将所有数据导出到CSV文件。 I have no idea about, how to implement StreamingOutput for downloading csv file. 我不知道如何实现StreamingOutput来下载csv文件。
Please help me.. 请帮我..
Thanks in advance 🙏🙏 在此先感谢🙏🙏
You have to read the records from the database, convert each row into CSV and use something like the following to use StreamingOutput
: 您必须从数据库中读取记录,将每一行转换为CSV并使用类似以下的内容来使用StreamingOutput
:
package de.demo.services;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;
import java.io.*;
@Path( "/demo" )
public class DemoService
{
@GET
@Produces(MediaType.TEXT_PLAIN)
@Path("/api")
public Response getCsv() {
StreamingOutput stream = new StreamingOutput() {
public void write(OutputStream os) throws ... {
Writer writer = new BufferedWriter(new OutputStreamWriter(os));
writer.write( /* CSV data */ );
writer.flush();
}
};
return Response.ok(stream).build();
}
}
Be careful not to read all your 20k rows from the database at once but read the results based on a cursor by method setFetchSize
of the JDBC statement. 注意不要一次从数据库中读取所有2万行,而是通过JDBC语句的setFetchSize
方法基于游标读取结果。
Use the following dependencies in Maven: 在Maven中使用以下依赖项:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.23.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.23.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.23.1</version>
</dependency>
Version 2.23.1
of Jersey is not the latest version you can get. 不是您可以获得的最新版本的Jersey 2.23.1
。
I am using the same approach, like what u suggested, but control is not going inside the StreamingOutput implementation only..Here is the code.... 我正在使用相同的方法,就像您建议的一样,但是控制不仅仅在StreamingOutput实现内部进行。这是代码。
@GET
@Produces(MediaType.TEXT_PLAIN)
@Path("/allEmployeeReport")
public Response exportAllEmployee()
{
StreamingOutput stream = new
StreamingOutput() {
public void write(OutputStream os)
throws IOException, Web...{
Writer writer = new
BufferedWriter(new
OutputStreamWriter(os));
for(Employee employee :
repository.findAll()){
writer.write(employee.getFName());
writer.write(",");
writer.write(employee.getLName());
writer.write(",");
writer.write(employee.getEmail());
writer.write(",");
writer.write(employee.getMobile());
writer.write(",");
writer.write(employee.getDOB());
writer.write("\n");
}
writer.flush();
writer.close();
};
return Response.ok(stream).build();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.