Concatenation two event's data from two Spark Cores by using NodeJS

Hello Guys
Please I need to concatenate the even't data from two spark cores and save them in a text file. I have modified the code below (Thanks to @harrisonhjones) to make it save the data from one even't core but how can I do it with two cores? I tried to change the code, but I got an undefined at the output:

Here is the code:

/*jslint node: true */
"use strict";
var spark = require('spark');
var fs = require('fs');
	// Variables for converting the Timestamp from GMT to locale time
var d=0; 
var nd=0; 
var utc=0;
	// Variables for concatenating the data from events of two cores (Event1's data&&Event2's data) 
var fd=0;
var c1=0;
var c2=0;
	// Login and start doing things!
spark.on('login', function() {
	//Get the locale time instead of UTC/GMT
  function calcTime(city, offset) {
    d = new Date();
    utc = d.getTime() + (d.getTimezoneOffset() * 60000);
    nd = new Date(utc + (3600000*offset));
    return " Published at " + nd.toLocaleString();
  }
 	//Get test event for specific core
  spark.getEventStream('Event1', '0000000000000000000000', function(data) {
    console.log("New Event: " + JSON.stringify(data) + ". Logging it to file");
    c1=data.data;
  });
  spark.getEventStream('Event2', '0000000000000000000000', function(data1) {
    console.log("New Event: " + JSON.stringify(data1) + ". Logging it to file");
    c2=data1.data1;
    fd= c1.concat(c2);
    fs.appendFile('Final Concatenated Data.txt', fd + "," +
    calcTime('MyCity','-5.0') + "," + 'Final Concatenated Data' + "\r\n", function (err) {
      if(err)
        console.log("File log failure. Error = " + JSON.stringify(err));
      else
        console.log("File log success");
    });
  });
});
 	// Login as usual
spark.login({ username: 'myemail@gmail.com', password: 'mypassword'});

At the output I got the first data which is c1 and I got undefined symbol instead of the second data as shown below:

SA0Eundefined , Published at Fri Jul 03 2015 21:49:29 GMT-0500 (Central Daylight Time),Final Cancatinated Data

where SA0E is for the second event's data (c2), and undefined for the first event's data (c2).
I guess that the undefined word appeared because I'm trying to use the c1 in the function of the c2 , so it couldn't get the data since it is inside a function. If I'm right, how can solve that?
Any suggestion please?
Thanks in advance.,

For one I’d rather write

  c2=data1.data; // instead of 'data1.data1';

Or you just stick with data for both function parameters. Since they are local you should be able to reuse the same name.

As for the concat you could just drop that and write

  fs.appendFile('Final Concatenated Data.txt', c1 + c2 + "," + ...

or are you reusing the concatenated string fd somewhere else?

2 Likes

Thank you so much @ScruffR.
I don’t reuse the concatenated string (fd) somewhere else, so your modification is working fine for me. I could see the concatenated data in the text file very nicely.
Have a great night.
Ahmed.

2 Likes