0

I am creating web service for Apache Spark Sql in java and I am returning the result set as json. It runs quickly for minimum no of rows but for maximum no of rows it takes too much time to load. can you tell me how to return these results in an efficient way. Here is my java code

  String output = "[";
  SparkConf sparkConf = new SparkConf().setAppName("Hive").setMaster("local").set("spark.driver.allowMultipleContexts", "true");
  JavaSparkContext ctx = new JavaSparkContext(sparkConf);
  HiveContext sqlContext = new org.apache.spark.sql.hive.HiveContext(ctx.sc());
 Row[] result = sqlContext.sql("Select * from activity where column="'value'" ).collect();
 for (int i = 0; i < result.length; i++) {
            output += "{\"" + "Column1" + "\":\"" + result[i].getString(0) + "\",\"" + "Column2" + "\":" + result[i].getString(1) + ",\"" + "Column3" + "\":" + result[i].getString(2) + ",\"" + "Column4" + "\":" + result[i].getString(3) + ",\"" + "Column5" + "\":" + result[i].getInt(4) + ",\"" + "Column6" + "\":" + result[i].getString(5) + ",\"" + "Column7" + "\":" + result[i].getString(6) + "},";
    }

I am using for loop to get the contents of result sets. I may have 1 million rows so using for loop costs me more time. Is there any efficient way to achieve this

wazza
  • 770
  • 5
  • 17
  • 42
  • Might be a good idea to map the `Row[]` into a custom POJO and use existing libraries like Jackson/Gson to create the json string out of it, rather than doing it manually. Please have a look at : http://www.mkyong.com/java/how-to-convert-java-object-to-from-json-jackson/ and this SO post http://stackoverflow.com/questions/15786129/converting-java-objects-to-json-with-jackson for reference. – Gyan Jul 28 '15 at 06:48
  • And avoid using string concatenation, use `StringBuffer` / `StringBuilder` instead. – Gyan Jul 28 '15 at 06:51

1 Answers1

0

@wazza, Please find the below important points you can make the for loop fast performance. Actually there is no much difference in the for loop or while loop etc...

1. Use integer as loop index variable.
2. Use System.arraycopy() to copy arrays.
3. Avoid method calls in loops .
4. It is efficient to access variables in a loop when compared to accessing array elements.
5. Compare the termination condition in a loop with zero if you use non-JIT or HotSpot VMs.
6. Avoid using method calls to check for termination condition in a loop
7. When using short circuit operators place the expression which is likely to evaluate to false on extreme left if the expresion contains &&.
8. When using short circuit operators place the expression which is likely to evaluate to true on extreme left if the expresion contains only ||.
9. Do not use exception handling inside loops.

I hope this will help your request...