Exporting Small Datasets

If the data you are exporting out of Spark is small, you can just use an
action to convert the RDD into objects in memory on the driver program, and then write that output directly to any data storage solution of your
choosing. You may remember that we called the take(N) action where N
is some finite number instead of the collect() action to ensure the output
fits in memory - no matter how big the input data set may be - this is good
practice. This section walks through example code where you’ll write the
log statistics to a file.

It may not be that useful to have these stats output to a file - in practice, you might write these statistics to a database for your presentation layer to access.

  1. LogStatistics logStatistics = logAnalyzerRDD.processRdd(accessLogs);
  2. String outputFile = args[1];
  3. Writer out = new BufferedWriter(
  4. new OutputStreamWriter(new FileOutputStream(outputFile)));
  5. Tuple4<Long, Long, Long, Long> contentSizeStats =
  6. logStatistics.getContentSizeStats();
  7. out.write(String.format("Content Size Avg: %s, Min: %s, Max: %s\n",
  8. contentSizeStats._1() / contentSizeStats._2(),
  9. contentSizeStats._3(),
  10. contentSizeStats._4()));
  11. List<Tuple2<Integer, Long>> responseCodeToCount =
  12. logStatistics.getResponseCodeToCount();
  13. out.write(String.format("Response code counts: %s\n", responseCodeToCount));
  14. List<String> ipAddresses = logStatistics.getIpAddresses();
  15. out.write(String.format("IPAddresses > 10 times: %s\n", ipAddresses));
  16. List<Tuple2<String, Long>> topEndpoints = logStatistics.getTopEndpoints();
  17. out.write(String.format("Top Endpoints: %s\n", topEndpoints));
  18. out.close();

Now, run LogAnalyzerExportSmallData.java. Try modifying it to write to a database of your own choosing.