简体   繁体   中英

How to export huge result set of a mybatis select query to csv?

Using MyBatis, I am getting close to 65,000 results from a select query within a mapper. I want to create a csv and send these results as a zip file to the UI call which will trigger the download dialog directly of the browser.

Upon searching, I understood that we need to use custom result handler in these scenarios. But the link to the example from ibatis seems to be removed http://code.google.com/p/mybatis/wiki/ResultHandlerExample leaving no accurate steps for me to use or implement it.

However, I have tried as below.

public class CSVExportRowHandler implements ResultHandler {
    @SuppressWarnings("unchecked")
    @Override
    public void handleResult(ResultContext context) {
        SRDContainer srdContainer = (SRDContainer) context.getResultObject();
        System.out.println("Container: " + srdContainer.toString() + ", " + context.getResultCount() +", "+ context.isStopped());
        for (StudentResultDetails srd : srdContainer.getStudentResultDetails()){
            System.out.println("result: " + srd.getStudentName());
        }
    }
}

As the handleResult method must return void. On this note, I have the below queries.

  1. I am able to print to the console only one record upon using this CSVExportRowHandler class. What happened to the rest of the records (should have been 65k) ?

  2. Is there any optimized way of doing this ?

  3. How to trigger the download dialog of browser directly ? before that do I need to write the records to csv or I can stream the results to download ?

Here are my DAO and the jax-rs service for reference

DAO.java

public void exportSRD(HashMap<String, Object> params) {         
        SqlSession sqlSession = SessionFactory.getSession().openSession();
        try {
            CSVExportRowHandler handler = new CSVExportRowHandler();
            sqlSession.select("getAssignmentSRDSession", params, handler);

        } finally {
            sqlSession.close();
        }
    }

Service:

@Path("/{reportType}/studentresultdetails/{function}/{sessionId}")
@GET
public void exportOrPrintUtil(@PathParam("reportType") String reportType, @PathParam("function") String function, @QueryParam("pageNum") Integer pageNum,
                @QueryParam("pageSize") Integer pageSize, @QueryParam("sortOn") String sortOn, @QueryParam("sortOrder") String sortOrder, 
                @QueryParam("filterByName") String filterByName, @PathParam("sessionId") Integer sessionId) throws IOException {
    HashMap<String, Object> params = new HashMap<String, Object>();
            Object enableSort;
            Object enablePrintOrExportFlag;
            params.put("sessionId", sessionId);
            params.put("enableSort", false);
            params.put("enablePrintOrExportFlag", true);
            params.put("filterByName", filterByName);

            dao.exportSRD(params);

            *********** WHAT TO BE RETURNED AS THE ABOVE dao.exportSRD(params) RETURNS VOID. HOW TO TRIGGER DOWNLOAD DIALOG FROM HERE. *****************

        }

SRDContainer.java object

@XmlRootElement
public class SRDContainer implements Comparable<SRDContainer> {

    private List<StudentResultDetails> studentResultDetails;

    public SRDContainer() {}

    public SRDContainer(
            List<StudentResultDetails> studentResultDetails) {
        super();
        this.studentResultDetails = studentResultDetails;
    }

    public List<StudentResultDetails> getStudentResultDetails() {
        return studentResultDetails;
    }

    public void setStudentResultDetails(
            List<StudentResultDetails> studentResultDetails) {
        this.studentResultDetails = studentResultDetails;
    }

    @Override
    public int compareTo(SRDContainer o) {
        return 0;
    }

    @Override
    public String toString() {
        return "SRDContainer [studentResultDetails="
                + studentResultDetails + "]";
    }

    public List<String[]> toStringArrayList() {
        String[] array = new String[studentResultDetails.size()];
        List<String[]> stringArrayListOut = new ArrayList<String[]>();
        int index = 0;
        for (StudentResultDetails value : this.studentResultDetails) {
          array[index] = value.toFlatCSVString();
          index++;
        }
        stringArrayListOut.add(array);
        return stringArrayListOut;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime
                * result
                + ((studentResultDetails == null) ? 0 : studentResultDetails
                        .hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        SRDContainer other = (SRDContainer) obj;
        if (studentResultDetails == null) {
            if (other.studentResultDetails != null)
                return false;
        } else if (!studentResultDetails.equals(other.studentResultDetails))
            return false;
        return true;
    }

}

I am pretty new to jax-rs and mybatis.

Thanks !!!!

sessionID make your code too complicated , jackoffID as well. even with deferred loading (thanks to CGLib proxies). It gets just a little fuzzy, however, when discussing how to implement complex queries

select id, name, shortName, description from tag

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM