简体   繁体   English

如何使用Java将大型Json数据下载到CSV文件中?

[英]How to download large Json data into CSV file in Java?

I am new to Java web application. 我是Java Web应用程序的新手。 I tried below link for converting JSON array of data to CSV file http://jsfiddle.net/JXrwM/11322/ 我尝试了下面的链接将数据的JSON数组转换为CSV文件http://jsfiddle.net/JXrwM/11322/

Now the issue is, whenever getting large amount of JSONArray of value, unable to convert CSV file as soon as possible. 现在的问题是,每当获取大量JSONArray值时,便无法尽快转换CSV文件。 Loading time is too long. 加载时间太长。

Fetching JSONArray like below, 像下面这样获取JSONArray

ResultSet rs = (ResultSet) statement.getObject(P_CURSOR);
JSONArray jsonArr = new JSONArray();
while (rs.next()) {
       JSONObject data = new JSONObject();           
       data.put(LASTNAME, rs.getString(LASTNAME));
       data.put(FIRSTNAME, rs.getString(FIRSTNAME));
       jsonArr.put(data); //**Fetching JSONArray is too slow.**
}

Here the data contains multiple records, how to fetch jsonArr faster and download into CSV file from ResultSet data. 这里的data包含多个记录,以及如何更快地获取jsonArr以及如何从ResultSet数据下载到CSV文件中。

Your problem could be that you are doing a "full" read, and then a "full" write to the CSV file. 您的问题可能是您正在进行“完全”读取,然后对“ CSV”文件进行了“完全”写入。

Meaning: your code first reads all the data into a single ResultSet instance, and that big object is then send to a CSVWriter in one shot. 含义:您的代码首先将所有数据读取到单个ResultSet实例中,然后将该大对象一次性发送到CSVWriter。

Maybe the answer is to rework your whole code to do that for rows , one by one. 也许答案是重新编写整个代码,以便一行一行地做到这一点。

Meaning: 含义:

  • you setup your CSVWriter 您设置CSVWriter
  • you fetch one row from the database 从数据库中读取一行
  • you write one line using the CSVWriter 你写使用CSVWriter 一行

Coming from such a solution, you might be able to use different threads for producing and consuming that data. 通过这种解决方案,您可能能够使用不同的线程来生成和使用该数据。 As in: one thread reads single lines, and puts that data on some "queue", and another thread reads from that queue, to write CSV data. 如图所示:一个线程读取单行,并将该数据放在“队列”中,另一个线程从该队列读取,以写入CSV数据。

Most likely, that is the only way that would enable you to speed up overall execution time. 这极有可能是使您加快整体执行时间的唯一方法。 Of course, using multiple threads will only pay out if there is really enough data to process. 当然,只有在确实有足够的数据要处理的情况下,使用多个线程才有回报。

Found solution for write large data in json format from resultset using Jackson library like below, 找到了如下解决方案,可使用杰克逊库resultset集中以json格式写入大数据,

        ResultSet rs = (ResultSet) statement.getObject(P_CURSOR);

        StringWriter writer = new StringWriter();
        JsonFactory jsonfactory = new JsonFactory();
        JsonGenerator jsonGenerator = jsonfactory.createJsonGenerator(writer);

        jsonGenerator.writeStartArray();
        while (rs.next()) {
            jsonGenerator.writeStartObject();
            jsonGenerator.writeStringField(KEY1, rs.getString(KEY1));
            jsonGenerator.writeStringField(KEY2, rs.getString(KEY2));
            jsonGenerator.writeStringField(KEY3, rs.getString(KEY3));
            jsonGenerator.writeEndObject();
        }

        jsonGenerator.writeEndArray();
        jsonGenerator.close();

This helps to read and write large set of data from resultset with less loading time. 这有助于以更少的加载时间从resultset集中读取和写入大量数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何下载大型CSV文件? - How to download large CSV file? 下载并阅读java Spring Rest服务中的大型csv文件 - Download and read on a large csv file in java Spring Rest service 如何通过实现StreamingOutput从其余服务创建和下载大数据集作为CSV文件 - How to create and download large data set as CSV file from rest services by implementing StreamingOutput 如何下载大数据量的excel文件? - How to download excel file with large amount of data? 如何从互联网下载大型json文件而不进行解析(java,okhttp3)? - How do I download a large json file from internet without parsing it (java, okhttp3)? 如何用Java从json数据生成csv文件? - How to generated the csv file from json data with Java? 如何通过Java代码下载大数据文件,在Java代码中数据是分块获取的? - How to download large data file by java code where data is fetching in chunks? Java Spring:如何有效地从CSV文件读取和保存大量数据? - Java Spring: How to efficiently read and save large amount of data from a CSV file? 将数据导出到csv并使用Java和Struts2下载文件 - export data to csv and download the file with Java and Struts2 在Java Servlet中下载CSV文件 - Download CSV file in Java Servlet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM