在云存储上创建csv文件,只有10条记录

时间:2015-12-16 10:56:36

标签: java google-app-engine google-cloud-storage google-cloud-platform

GcsService gcsService = GcsServiceFactory.createGcsService();
        String fileName=fileNameWithOutExtension+".csv";
        GcsFilename gcsFileName = new GcsFilename(bucketName ,fileName);
        GcsFileOptions gcsFileOptions = new GcsFileOptions.Builder().mimeType("text/csv").acl(aclType).build();
        GcsOutputChannel gcsoutputChannel = gcsService.createOrReplace(gcsFileName, gcsFileOptions);
        gcsoutputChannel.write(ByteBuffer.wrap(header.getBytes()));
        for(T object : list){
            gcsoutputChannel.write(ByteBuffer.wrap(object.toString().replace("null", "").getBytes("UTF-8")));
        }
        gcsoutputChannel.close();
        if(StringUtility.isNotNullOrBlank(emailId)){
            String downloadUrl = "https://storage.googleapis.com/"+bucketName+"/"+fileName;
            EmailUtil.sendJobReportCompletionMail(emailId, downloadUrl);
        }


this is my code to create CSV file on cloud storage.

但我想在一个文件中只写10条记录,接下来的10条记录应该转到新的自动生成的文件filename 2.csv。

我试过这个

GcsService gcsService = GcsServiceFactory.createGcsService();
        String fileName=fileNameWithOutExtension+".csv";
        int i=1,counter = 0;
        GcsFilename gcsFileName = new GcsFilename(bucketName ,fileName);
        GcsFileOptions gcsFileOptions = new GcsFileOptions.Builder().mimeType("text/csv").acl(aclType).build();
        GcsOutputChannel gcsoutputChannel = gcsService.createOrReplace(gcsFileName, gcsFileOptions);
        gcsoutputChannel.write(ByteBuffer.wrap(header.getBytes()));
        for(T object : list){

            if(counter == 10)
            {
                fileName=fileNameWithOutExtension+i+".csv";
                gcsFileName = new GcsFilename(bucketName ,fileName);
                gcsFileOptions = new GcsFileOptions.Builder().mimeType("text/csv").acl(aclType).build();
                gcsoutputChannel = gcsService.createOrReplace(gcsFileName, gcsFileOptions);


            }else
            {
            gcsoutputChannel.write(ByteBuffer.wrap(object.toString().replace("null", "").getBytes("UTF-8")));
            }

        }
        gcsoutputChannel.close();
        if(StringUtility.isNotNullOrBlank(emailId)){
            String downloadUrl = "https://storage.googleapis.com/"+bucketName+"/"+fileName;
            EmailUtil.sendJobReportCompletionMail(emailId, downloadUrl);
        }
    }catch(IOException e){
        logger.log(Level.SEVERE,"Error wrinting to GCS:",e);
    }
    logger.log(Level.INFO, list.toString());
    logger.log(Level.INFO,"Wrinting to GCS completed");
}

但我现在很困惑。 我可以创建新文件但是如何使用相同的for循环在该文件上写入?

1 个答案:

答案 0 :(得分:0)

相当错误和不完整的代码。相反,请尝试使用此主循环:

for(T object : list){
    gcsoutputChannel.write(ByteBuffer.wrap(object.toString().replace("null", "").getBytes("UTF-8")));
    counter += 1;
    if(counter == 10)
    {
        gcsoutputChannel.close();
        fileName=fileNameWithOutExtension+i+".csv";
        i += 1;
        gcsFileName = new GcsFilename(bucketName ,fileName);
        gcsFileOptions = new GcsFileOptions.Builder().mimeType("text/csv").acl(aclType).build();
        gcsoutputChannel = gcsService.createOrReplace(gcsFileName, gcsFileOptions);
    }
}
if(counter > 0)
{
    gcsoutputChannel.close();
}