0

I have Java program and I am reading from file of Size Approx 40GB and converting data on fly and streaming to DataOutputstream.

Below is my code

    JSONObject jsonObj= new JSONObject();
                JSONArray jsonArray = new JSONArray();
                 JSONObject properties = new JSONObject(); 

           while((strLine = in.readLine()) != null)
                {


            if (jsonArray.length()%100 == 0) {

                    printJsonData(jsonArray);
            //      sendJsonData(jsonArray, output);
                    jsonArray = new JSONArray();
                }

if (strLine.trim().contains("data")) {
                        jsonObj.put("properties",properties);
                        jsonArray.put(jsonObj);

    if (strLine.trim().contains("data1")) {
                String secondPart = strLine.split(":",2)[1];
                properties.put("data1", secondPart);    
                continue;
                }
            if (strLine.trim().contains("id")) {
                String secondPart = strLine.split(":",2)[1];
                jsonObj.put("id", secondPart ); 
            continue;

            }

    }

I have two methods one is printing JSON data and other is sending Json data over https.

private void printJsonData(JSONArray jsonArray) {
      int count = jsonArray.length(); // get totalCount of all jsonObjects
      for(int i=0 ; i< count; i++){   // iterate through jsonArray 
            JSONObject jsonObject = jsonArray.getJSONObject(i);  // get jsonObject @ i position 
            System.out.println("jsonObject " + i + ": " + jsonObject);
        }
    }

private void sendJsonData(JSONObject jsonObj,DataOutputStream   output) throws IOException {

    int count = jsonArray.length();     

    for(int i=0 ; i< count; i++) {   // iterate through jsonArray 
        JSONObject jsonObject = jsonArray.getJSONObject(i);  // get jsonObject @ i position 
        System.out.println("Sending Data-->" + jsonObject.toString());
        output.writeBytes(jsonObject.toString()); 
    }
}

Whenver i call print method, every thing works fine. But when i call sendJsonData method. I am getting OutofMemoryerror. Wondering how to fix it?

6
  • Where are you getting the value of count in the sendJsonData method? Commented Mar 8, 2017 at 3:00
  • er, can not even see where sendJsonData is called from Commented Mar 8, 2017 at 3:00
  • @MichaelMarkidis int count = jsonArray.length(); I fixed it. thanks Commented Mar 8, 2017 at 3:04
  • @ScaryWombat I commented the line in while loop for sending data: call is sendJsonData(jsonArray, output); Commented Mar 8, 2017 at 3:05
  • What is the output stream inside the DataOutputStream? Commented Mar 8, 2017 at 3:41

1 Answer 1

1

Since you are not manipulating json in any way I would treat as a text file and send it that way. The likelihood of finding a big JsonObject is good so you may be pushing through too much.

An array of 40gb is awfully big array unless there are huge objects.

** EDIT **

Or you could treat the individual json objects as "files" and push those strings individually

Sign up to request clarification or add additional context in comments.

3 Comments

Meaning One Object at a time? and No array at all?
I would try to launch a connection per array element. Treat each element as an string and make an stream of them and send them in a more controlled manner. Maybe you could launch a thread pool to speed things up. Reading the whole thing line by line may be too slow
You option for sending One Big Array at a time with one connection help fix this issue. -- Thans Ammad

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.