当前位置: 首页 > 知识库问答 >
问题:

从CSV创建批处理,导致错误状态

华温书
2023-03-14

通过遍历CSVReader类,我发现了一个定义的自定义异常,该异常与我得到的异常完全相同。但我认为当在某个double qoute(CSV文件的单元格值)中发现一个双引号时,它就会出现。我将该链接称为:https://github.com/mulesoft/salesforce-connector/blob/master/src/main/java/com/sforce/async/csvreader.java

谁能建议我哪里做错了或任何解决这个问题的方法?

我将与您分享以下代码片段:Method1,然后调用Method2。

    Method1: private List<BatchInfo> createBatchesFromCSVFile(RestConnection connection,
            JobInfo jobInfo, String csvFileName) throws Exception {
        List<BatchInfo> batchInfos = new ArrayList<BatchInfo>();
        BufferedReader rdr = new BufferedReader(new InputStreamReader(
                new FileInputStream(csvFileName)));

        // read the CSV header row
        String hdr = rdr.readLine();
        byte[] headerBytes = (hdr + "\n").getBytes("UTF-8");
        int headerBytesLength = headerBytes.length;
//      I was making use of the following code which I replaced with the next line of code.
//      File tmpFile = File.createTempFile("bulkAPIInsert", ".csv");
        File tmpFile = new File("bulkAPIInsert.csv");
        // Split the CSV file into multiple batches
        try {
            FileOutputStream tmpOut = new FileOutputStream(tmpFile);
            int maxBytesPerBatch = 10000000; // 10 million bytes per batch
            int maxRowsPerBatch = 10000; // 10 thousand rows per batch
            int currentBytes = 0;
            int currentLines = 0;
            String nextLine;

            while ((nextLine = rdr.readLine()) != null) {
                byte[] bytes = (nextLine + "\n").getBytes("UTF-8"); //TODO
                if (currentBytes + bytes.length > maxBytesPerBatch
                        || currentLines > maxRowsPerBatch) {
                    createBatch(tmpOut, tmpFile, batchInfos, connection, jobInfo);
                    currentBytes = 0;
                    currentLines = 0;
                }
                if (currentBytes == 0) {
                    tmpOut = new FileOutputStream(tmpFile);
                    tmpOut.write(headerBytes);
                    currentBytes = headerBytesLength;
                    currentLines = 1;
                }
                tmpOut.write(bytes);
                currentBytes += bytes.length;
                currentLines++;
            }

            if (currentLines > 1) {
                createBatch(tmpOut, tmpFile, batchInfos, connection, jobInfo);
            }
        } finally {
            if(!tmpFile.delete())
                tmpFile.deleteOnExit();
            rdr.close();
        }
        return batchInfos;
    }

/**
     * Wait for a job to complete by polling the Bulk API.
     */
    Method2: private void awaitCompletion(RestConnection connection, JobInfo job,
            List<BatchInfo> batchInfoList) throws AsyncApiException { 
        try{
            /****
            Some code
            **/
                BatchInfo[] statusList = connection.getBatchInfoList(job.getId())
                .getBatchInfo();
                for (BatchInfo b : statusList) {
                    if (b.getState() == BatchStateEnum.Completed) {
                        if (incomplete.remove(b.getId())) 
                            //Do Something
                    }
                    else if(b.getState() == BatchStateEnum.Failed){ 

                        System.out.println("Reason: "+b.getStateMessage()+".\n  " +
                                "Number of Records Processed: "+b.getNumberRecordsProcessed());
                        throw (new Exception(""));
                    }
                }
            }
        }catch(Exception ex){log.debug(" Exception occurred.");}
    }

共有1个答案

鲁景山
2023-03-14

谢谢“猎犬”,帮了我一把。

回答

通过删除每个单元格的换行符,这个问题得到了解决。

 类似资料:
  • 使用Kubernetes 1.12.6-gke。7或更高版本可以创建ManagedCertificate,然后从向Internet公开服务的入口资源引用该证书。 运行kubectl描述管理证书证书名称首先指示证书处于配置状态,但最终转到FailedNotViable。 尽管使用静态IP和DNS可以很好地解析上述服务的http版本,但所有ManagedCertificate最终都会处于“状态:Fai

  • 反正可以禁用SQL,我只是想测试我的读写器和处理器工作正常。

  • 我开始学习spring batch,遇到一个问题,当我想使用在数据库中持久化作业的状态时。编译器显示: “原因:org.springframework.beans.factory.beanCreationException:创建类路径资源[springconfig.xml]中定义的名为'job repository'的bean时出错:调用init方法失败;嵌套异常为java.lang.noClas

  • null 鉴于以下MVE: 执行代码时,“Continue”流运行良好,但“Completed”流总是以失败的作业状态退出。 如何使作业在状态已完成的情况下完成?换句话说,我在流的编码上做错了什么?

  • 问题内容: 我想更新mySql数据库中特定列上的每一行。目前,我在每行中使用一个,并在for循环中进行迭代。我想知道在Java编程方面是否还有其他选择可以减少这种时间和资源的消耗(类似于批量执行准备好的语句)。更新是从Java代码进行的,因为这是我从中获取值的地方。我也没有兴趣在服务器上制作存储过程,因为我没有这样做的权利。 问题答案: 这里是一个示例的链接,该示例使用Java的Prepared语