0

I have this function for extracting the timestamp from two JSON objects:

lineReader.on('line', function (line) {
    var obj = JSON.parse(line);
    if(obj.Event == "SparkListenerApplicationStart" || obj.Event == "SparkListenerApplicationEnd") {
        console.log('Line from file:', obj.Timestamp);
    }
});

The JSON comes from a log file(not JSON) where each line represents an entry in the log and each line also happens to be in JSON format on its own.

The two objects represent the start and finish of a job. These can be identified by the event key(SparkListenerApplicationStart and SparkListenerApplicationEnd). They also both contain a timestamp key. I want to subtract the end time from the start time to get the duration.

My thinking is to assign the timestamp from the JSON where Event key = SparkListenerApplicationStart to one variable and assign the timestamp from the JSON where Event key = SparkListenerApplicationEnd to another variable and subtract one from the other. How can I do this? I know I can't simply do anything like:

var startTime = if(obj.Event == "SparkListenerApplicationStart"){
    return obj.Timestamp;
}
1
  • If you are trying to measure the time between start and end, then we need to maintain a state outside the callback function of line. Commented Jan 8, 2018 at 14:07

1 Answer 1

1

I'm not sure if I understood, but if are reading rows and want get the Timestamp of each row I would re-write a new object;

const collection = []

lineReader.on('line', function (line) {
    var obj = JSON.parse(line);
    if(obj.Event == "SparkListenerApplicationStart" || obj.Event == "SparkListenerApplicationEnd") {
        // console.log('Line from file:', obj.Timestamp);
        collection.push(obj.Timestamp)
    }
});

console.log(collection);

Where collection could be a LocalStorage, Global Variable, or something alike.

Additional info With regard to my comment where I queried how to identify the start and end times, I ended up setting start as the smallest value and end as the largest. Here is my final code:

const collection = []

lineReader.on('line', function (line) {
    var obj = JSON.parse(line);
    if((obj.Event == "SparkListenerApplicationStart" || obj.Event == "SparkListenerApplicationEnd")) {
        collection.push(obj.Timestamp);
        if(collection.length == 2){
            startTime = Math.min.apply(null, collection);
            finishTime = Math.max.apply(null, collection);
            duration = finishTime - startTime;
            console.log(duration);
        }
    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the reply Pablo. I see whet you have done there and its good that I've got those two values but how do I then identify which is which and then subtract one from the other? I guess I could just find the smallest andd subtract it from the largest.
You could work with binary tree, while you do the push.

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.