First of all I am a C# developer and very new to Scala. We are trying to use Spark to query SQL and Cassandra and this is a little proof of concept program.
var output: StringBuilder = new StringBuilder();
try {
//var output: StringBuilder = new StringBuilder();
var config = new Config(args);
val sparkConf = new SparkConf(true)
.set("spark.cassandra.connection.host", config.cassanrdaClusterIp).set("spark.akka.heartbeat.interval", "100")
//var output: StringBuilder = new StringBuilder();
val sparkContext: SparkContext = new SparkContext(config.sparkConnectionString, "springcm-spark-webserver", sparkConf)
val sqlContext = new CassandraSQLContext(sparkContext)
val sqlConnectionString = "jdbc:sqlserver://" + config.sqlConnectionString;
if (args(0) == "DocHistoryReport") {
val docHistoryReport = new DocHistoryReport(sparkContext, sqlContext, sqlConnectionString, config.cassanrdaKeyspace)
// var output: StringBuilder = new StringBuilder();
var result = docHistoryReport.Execute(config.accountId, config.userId, config.startDate, config.endDate, config.dateBucketType);
result.collect();
var file: File = new File("result.csv");
// var output: StringBuilder = new StringBuilder();
if (!file.exists()) {
file.createNewFile();
}
val pw = new PrintWriter(file);
result.foreach(row => {
output.append(row.toString().stripPrefix("[").stripSuffix("]") + sys.props("line.separator"));
})
pw.write(output.toString());
pw.flush();
pw.close;
}
else {
throw new IllegalArgumentException("Unsuported report type " + args(0));
}
}
The code creates a spark context, runs a simple report and writes the result to file. Note that the variable output is initialized several times in the code but all but one is commented out. If I initialize output anywhere but where it is currently declared, the result.csv file will be empty and the output variable will be reinitialized several times during the execution of the for loop wiping the result.
Could someone please explain to what is going on and why the location of the variable initialization matters. Thanks.