Java创建100MB压缩csv文件性能问题
我需要在5秒内创建100mb压缩文件,其中包含一个使用java的CSV文件。我已经创建了test.zip,其中包含CSV文件,但是生成zip文件花费了太多时间(~30秒)。以下是我到目前为止编写的代码:Java创建100MB压缩csv文件性能问题,java,supercsv,zipoutputstream,Java,Supercsv,Zipoutputstream,我需要在5秒内创建100mb压缩文件,其中包含一个使用java的CSV文件。我已经创建了test.zip,其中包含CSV文件,但是生成zip文件花费了太多时间(~30秒)。以下是我到目前为止编写的代码: ByteArrayOutputStream=newbytearrayoutputstream(); /*创建ZipoutStream实例以创建ZIP文件*/ ZipOutputStream ZipOutputStream=新ZipOutputStream(BAS); /*为文件创建ZIP条目。创
ByteArrayOutputStream=newbytearrayoutputstream();
/*创建ZipoutStream实例以创建ZIP文件*/
ZipOutputStream ZipOutputStream=新ZipOutputStream(BAS);
/*为文件创建ZIP条目。创建的文件放入
*zip文件。文件不在磁盘上,csvFileName仅表示
*要放入zip的文件名
*/
ZipEntry ZipEntry=新ZipEntry(“Test.zip”);
zipOutputStream.putNextEntry(Zippentry);
/*为CSV创建OutputStreamWriter。不需要分期付款
*文件系统上的CSV。直接将字节写入输出流。
*/
BufferedWriter BufferedWriter=新的BufferedWriter(新的OutputStreamWriter(ZipoutStream,“UTF-8”);
CsvListWriter CsvListWriter=新的CsvListWriter(bufferedWriter,CsvPreference.EXCEL_首选项);
/*将CSV头写入生成的CSV文件*/
csvListWriter.writeHeader(CSVGeneratorConstant.CSV_头);
/*将内容写入CSV的逻辑*/
long startTime=System.currentTimeMillis();
对于(int-rowIdx=0;rowIdx<7000000;rowIdx++){
最终列表rowContent=newlinkedlist();
对于(int colIdx=0;colIdx<6;colIdx++){
String str=“R”+rowIdx+“C”+colIdx+“FieldContent”;
rowContent.add(str);
}
csvListWriter.write(行内容);
}
长停止时间=System.currentTimeMillis();
长延时=停止时间-开始时间;
System.out.println(“时间=”+elapsedTime/1000f+“秒”);
System.out.println(“大小===”+baos.Size()/(Math.pow(1024,2))+“MB”);
csvListWriter.close();
bufferedWriter.close();
zipOutputStream.close();
baos.close();
我正在使用超级csv库,但我也尝试在内存中创建没有超级csv库的zip文件,但没有成功。你能帮我吗?你的测试数据大约是1GB,压缩到100MB。根据您的硬件,可能无法实现<5s的性能 我已经制定了一个快速而肮脏的基准测试,它强调了写入zip文件对性能的影响
- 使用
:9.6s字符串写入CSV.join()
- 使用超级CSV:12.7s写入CSV
- 使用
:18.6s字符串写入zip中的CSV。join()
- 使用Super CSV:22.5s在zip中写入CSV
ByteArrayOutputStream
)。我还跳过了Super CSV示例中的BufferedWriter
,因为它已经在内部使用了它,我使用了try with resources使事情变得更干净
@Test
public void testWriteToCsvFileWithSuperCSV() throws Exception {
long startTime = System.currentTimeMillis();
try (FileOutputStream csvFile = new FileOutputStream(new File("supercsv.csv"));
ICsvListWriter writer = new CsvListWriter(new OutputStreamWriter(csvFile, "UTF-8"), CsvPreference.EXCEL_PREFERENCE)
){
for (int rowIdx = 0; rowIdx < 7000000; rowIdx++) {
final List<String> rowContent = new LinkedList<>();
for (int colIdx = 0; colIdx < 6; colIdx++) {
String str = "R" + rowIdx + "C" + colIdx + " FieldContent";
rowContent.add(str);
}
writer.write(rowContent);
}
}
long stopTime = System.currentTimeMillis();
long elapsedTime = stopTime - startTime;
System.out.println("Writing to CSV with Super CSV took " + (elapsedTime / 1000f) + " seconds");
}
@Test
public void testWriteToCsvFileWithinZipWithSuperCSV() throws Exception {
long startTime = System.currentTimeMillis();
try (FileOutputStream zipFile = new FileOutputStream(new File("supercsv.zip"));
ZipOutputStream zos = new ZipOutputStream(zipFile);
ICsvListWriter writer = new CsvListWriter(new OutputStreamWriter(zos, "UTF-8"), CsvPreference.EXCEL_PREFERENCE)
){
ZipEntry csvFile = new ZipEntry("supercsvwithinzip.csv");
zos.putNextEntry(csvFile);
for (int rowIdx = 0; rowIdx < 7000000; rowIdx++) {
final List<String> rowContent = new LinkedList<>();
for (int colIdx = 0; colIdx < 6; colIdx++) {
String str = "R" + rowIdx + "C" + colIdx + " FieldContent";
rowContent.add(str);
}
writer.write(rowContent);
}
}
long stopTime = System.currentTimeMillis();
long elapsedTime = stopTime - startTime;
System.out.println("Writing to CSV within zip file with Super CSV took " + (elapsedTime / 1000f) + " seconds");
}
@Test
public void testWriteToCsvFileWithStringJoin() throws Exception {
long startTime = System.currentTimeMillis();
try (FileOutputStream textFile = new FileOutputStream(new File("join.csv"));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(textFile, "UTF-8"));
){
for (int rowIdx = 0; rowIdx < 7000000; rowIdx++) {
final List<String> rowContent = new LinkedList<>();
for (int colIdx = 0; colIdx < 6; colIdx++) {
String str = "R" + rowIdx + "C" + colIdx + " FieldContent";
rowContent.add(str);
}
writer.append(String.join(",", rowContent) + "\n");
}
}
long stopTime = System.currentTimeMillis();
long elapsedTime = stopTime - startTime;
System.out.println("Writing to CSV with String.join() took " + (elapsedTime / 1000f) + " seconds");
}
@Test
public void testWriteToCsvFileWithinZipWithStringJoin() throws Exception {
long startTime = System.currentTimeMillis();
try (FileOutputStream zipFile = new FileOutputStream(new File("join.zip"));
ZipOutputStream zos = new ZipOutputStream(zipFile);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(zos, "UTF-8"));
){
ZipEntry csvFile = new ZipEntry("joinwithinzip.csv");
zos.putNextEntry(csvFile);
for (int rowIdx = 0; rowIdx < 7000000; rowIdx++) {
final List<String> rowContent = new LinkedList<>();
for (int colIdx = 0; colIdx < 6; colIdx++) {
String str = "R" + rowIdx + "C" + colIdx + " FieldContent";
rowContent.add(str);
}
writer.append(String.join(",", rowContent) + "\n");
}
}
long stopTime = System.currentTimeMillis();
long elapsedTime = stopTime - startTime;
System.out.println("Writing to CSV within zip with String.join() took " + (elapsedTime / 1000f) + " seconds");
}
@测试
public void testWriteToCsvFileWithSuperCSV()引发异常{
long startTime=System.currentTimeMillis();
try(FileOutputStream csvFile=newfileoutputstream(新文件(“supercsv.csv”));
ICsvListWriter writer=新的CsvListWriter(新的OutputStreamWriter(csvFile,“UTF-8”),CsvPreference.EXCEL_首选项)
){
对于(int-rowIdx=0;rowIdx<7000000;rowIdx++){
最终列表rowContent=newlinkedlist();
对于(int colIdx=0;colIdx<6;colIdx++){
String str=“R”+rowIdx+“C”+colIdx+“FieldContent”;
rowContent.add(str);
}
writer.write(行内容);
}
}
长停止时间=System.currentTimeMillis();
长延时=停止时间-开始时间;
System.out.println(“使用超级CSV写入CSV需要”+(elapsedTime/1000f)+“秒”);
}
@试验
public void testWriteToCsvFileWithinZipWithSuperCSV()引发异常{
long startTime=System.currentTimeMillis();
try(FileOutputStream zipFile=newfileoutputstream(新文件(“supercsv.zip”));
ZipoutStream zos=新ZipoutStream(zipFile);
ICsvListWriter writer=新的CsvListWriter(新的OutputStreamWriter(zos,“UTF-8”),CsvPreference.EXCEL_首选项)
){
ZipEntry csvFile=新ZipEntry(“supercsvwithinzip.csv”);
zos.Putnextery(csvFile);
对于(int-rowIdx=0;rowIdx<7000000;rowIdx++){
最终列表rowContent=newlinkedlist();
对于(int colIdx=0;colIdx<6;colIdx++){
String str=“R”+rowIdx+“C”+colIdx+“FieldContent”;
rowContent.add(str);
}
writer.write(行内容);
}
}
长停止时间=System.currentTimeMillis();
长延时=停止时间-开始时间;
System.out.println(“使用超级CSV在zip文件中写入CSV需要”+(elapsedTime/1000f)+“秒”);
}
@试验
public void testWriteToCsvFileWithStringJoin()引发异常{
long startTime=System.currentTimeMillis();
try(FileOutputStream textFile=newfileoutputstream(新文件(“join.csv”));
BufferedWriter writer=新的BufferedWriter(新的OutputStreamWriter(文本文件,“UTF-8”));
){
对于(int-rowIdx=0;rowIdx<7000000;rowIdx++){
最终列表rowContent=newlinkedlist();
对于(int colIdx=0;colIdx<6;colIdx++){
String str=“R”+rowIdx+“C”+colIdx+“FieldContent”;
rowContent.add(str);
}
writer.append(String.join(“,”,rowContent)+“\n”);
}
}
长停止时间=System.currentTimeMillis();
长延时=停止时间-开始时间;
System.out.println(“使用String.join()写入CSV需要”+(elapsedTime/1000f)+