0

I'm new to node.js so apologies if this is something very simple.

I have the below node js script:

var http = require("http");
var bodyParser = require("body-parser");

var options =  {
    "method" : "GET",
    "hostname" : "xxx.xxx.xxx.xxx",
    "port" : "18080",
    "path" : "/api/v1/applications/app-20180103124606-0007/stages/0"
};

var req = http.request(options, function (res) {
    var chunks = [];

    res.on("data", function (chunk) {
        chunks.push(chunk);
    });

    res.on("end", function () {
        var body = JSON.parse(Buffer.concat(chunks));
        console.log(body);
    });
});

req.end();

It returns this JSON:

[ { status: 'COMPLETE',
    stageId: 0,
    attemptId: 0,
    numActiveTasks: 0,
    numCompleteTasks: 1,
    numFailedTasks: 0,
    executorRunTime: 2738,
    executorCpuTime: 1207164005,
    submissionTime: '2018-01-03T12:46:10.796GMT',
    firstTaskLaunchedTime: '2018-01-03T12:46:10.810GMT',
    completionTime: '2018-01-03T12:46:14.513GMT',
    inputBytes: 0,
    inputRecords: 99171,
    outputBytes: 0,
    outputRecords: 0,
    shuffleReadBytes: 0,
    shuffleReadRecords: 0,
    shuffleWriteBytes: 1468516,
    shuffleWriteRecords: 3872,
    memoryBytesSpilled: 0,
    diskBytesSpilled: 0,
    name: 'reduceByKey at /scripts/wordcount.py:37',
    details: 'org.apache.spark.rdd.RDD.<init>(RDD.scala:104)\norg.apache.spark.api.python.PairwiseRDD.<init>(PythonRDD.scala:391)\nsun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)\nsun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)\nsun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)\njava.lang.reflect.Constructor.newInstance(Constructor.java:423)\npy4j.reflection.MethodInvoker.invoke(MethodInvoker.java:247)\npy4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:357)\npy4j.Gateway.invoke(Gateway.java:236)\npy4j.commands.ConstructorCommand.invokeConstructor(ConstructorCommand.java:80)\npy4j.commands.ConstructorCommand.execute(ConstructorCommand.java:69)\npy4j.GatewayConnection.run(GatewayConnection.java:214)\njava.lang.Thread.run(Thread.java:748)',
    schedulingPool: 'default',
    accumulatorUpdates: 
     [ [Object],
       [Object],
       [Object],
       [Object],
       [Object],
       [Object],
       [Object],
       [Object],
       [Object],
       [Object],
       [Object],
       [Object],
       [Object],
       [Object] ],
    tasks: { '0': [Object] },
    executorSummary: { '0': [Object] } } ]

I now need to extract launchTime and taskTime. How is this done? I had previousy managed to extract data from JSON but I think I'm having trouble here as it's contained in an array.

2
  • Will the array length always be 1? Commented Jan 5, 2018 at 13:03
  • It will always be one so went with Jim's anwer. Thanks for the responses. Commented Jan 6, 2018 at 16:03

2 Answers 2

3

You can use Array.prototype.forEach() to iterate over each member of the response body.

res.on('end', () => {
  let body = JSON.parse(Buffer.concat(chunks))
  body.forEach(item => {
     // Do something with item
     console.log(item)
  })
})
Sign up to request clarification or add additional context in comments.

Comments

2

If the array length is always 1, then you can access the first array element with body[0].

body[0].executorRunTime
body[0].firstTaskLaunchedTime

Otherwise you could iterate over the result with body.forEach().

Comments

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.