[英]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: 含义:
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.