在构建json对象后,我必须每秒在java中执行HTTP发布。 json对象是通过读取巨大的(200Mbs +)CSV文件构建的,所以我的问题是 如何读取构建x对象的x行数并每秒发布一次(因为不可能在不到一秒的时间内解析整个200mb文件)并继续读取下一行x行。
请让我知道你的想法..
我可以使用Java计时器类,并继续读取CSV文件,同时每隔一秒将json对象发送到服务器并形成json吗?
答案 0 :(得分:1)
几乎不可能每秒读取,解析,转换和发送一次200 MB文件。
所以你需要改变你的设计:
我的建议是只发送更改的行,如下所示:
{
"1" : {"field1":"value1","field2":"value2"},
"17" : {"field1":"value1","field2":"value2"}
}
这当然会给你带来新的问题:
客户需要确定哪些行已更改,服务器需要将更改的行与现有数据集成。
答案 1 :(得分:0)
我会根据文件大小而不依赖于时间来制作它。
BufferedReader fin = null; //create it
Gson gson=new Gson(); //Google code open source library for JSON in Java
ArrayList<JSONObject> jsonList=new ArrayList<JSONObject>();
while (((line = fin.readLine()) != null)) {
if ( line.length()==0 ){
//"Blank line;
}else{
currJSON=loadJSON(line);//You have to load it in a Java Object
if ( jsonList.size()<MAX_JSON){
jsonList.add(currJSON);
}
if (JsonList.size()==MAX_JSON){ //Define the maximum size of the list you want to post
gson.toJson(jsonList); //Convert to JSON
//You should post your Json with some Http Connection to your server
jsonList.clear();