Search in sources :

Example 16 with JSONArray

use of org.codehaus.jettison.json.JSONArray in project hadoop by apache.

the class TestAHSWebServices method testMultipleAttempts.

@Test
public void testMultipleAttempts() throws Exception {
    ApplicationId appId = ApplicationId.newInstance(0, 1);
    WebResource r = resource();
    ClientResponse response = r.path("ws").path("v1").path("applicationhistory").path("apps").path(appId.toString()).path("appattempts").queryParam("user.name", USERS[round]).accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
    if (round == 1) {
        assertResponseStatusCode(Status.FORBIDDEN, response.getStatusInfo());
        return;
    }
    assertEquals(MediaType.APPLICATION_JSON + "; " + JettyUtils.UTF_8, response.getType().toString());
    JSONObject json = response.getEntity(JSONObject.class);
    assertEquals("incorrect number of elements", 1, json.length());
    JSONObject appAttempts = json.getJSONObject("appAttempts");
    assertEquals("incorrect number of elements", 1, appAttempts.length());
    JSONArray array = appAttempts.getJSONArray("appAttempt");
    assertEquals("incorrect number of elements", 5, array.length());
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) JSONObject(org.codehaus.jettison.json.JSONObject) JSONArray(org.codehaus.jettison.json.JSONArray) WebResource(com.sun.jersey.api.client.WebResource) ApplicationId(org.apache.hadoop.yarn.api.records.ApplicationId) Test(org.junit.Test)

Example 17 with JSONArray

use of org.codehaus.jettison.json.JSONArray in project hadoop by apache.

the class LogsCLI method getAMContainerInfoForRMWebService.

protected List<JSONObject> getAMContainerInfoForRMWebService(Configuration conf, String appId) throws ClientHandlerException, UniformInterfaceException, JSONException {
    Client webServiceClient = Client.create();
    String webAppAddress = WebAppUtils.getRMWebAppURLWithScheme(conf);
    WebResource webResource = webServiceClient.resource(webAppAddress);
    ClientResponse response = webResource.path("ws").path("v1").path("cluster").path("apps").path(appId).path("appattempts").accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
    JSONObject json = response.getEntity(JSONObject.class).getJSONObject("appAttempts");
    JSONArray requests = json.getJSONArray("appAttempt");
    List<JSONObject> amContainersList = new ArrayList<JSONObject>();
    for (int i = 0; i < requests.length(); i++) {
        amContainersList.add(requests.getJSONObject(i));
    }
    return amContainersList;
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) JSONObject(org.codehaus.jettison.json.JSONObject) JSONArray(org.codehaus.jettison.json.JSONArray) ArrayList(java.util.ArrayList) WebResource(com.sun.jersey.api.client.WebResource) YarnClient(org.apache.hadoop.yarn.client.api.YarnClient) Client(com.sun.jersey.api.client.Client)

Example 18 with JSONArray

use of org.codehaus.jettison.json.JSONArray in project hadoop by apache.

the class LogsCLI method getContainerLogFiles.

private List<Pair<PerContainerLogFileInfo, String>> getContainerLogFiles(Configuration conf, String containerIdStr, String nodeHttpAddress) throws IOException {
    List<Pair<PerContainerLogFileInfo, String>> logFileInfos = new ArrayList<>();
    Client webServiceClient = Client.create();
    try {
        WebResource webResource = webServiceClient.resource(WebAppUtils.getHttpSchemePrefix(conf) + nodeHttpAddress);
        ClientResponse response = webResource.path("ws").path("v1").path("node").path("containers").path(containerIdStr).path("logs").accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
        if (response.getStatusInfo().getStatusCode() == ClientResponse.Status.OK.getStatusCode()) {
            try {
                JSONArray array = new JSONArray();
                JSONObject json = response.getEntity(JSONObject.class);
                Object logsInfoObj = json.get("containerLogsInfo");
                if (logsInfoObj instanceof JSONObject) {
                    array.put((JSONObject) logsInfoObj);
                } else if (logsInfoObj instanceof JSONArray) {
                    JSONArray logsArray = (JSONArray) logsInfoObj;
                    for (int i = 0; i < logsArray.length(); i++) {
                        array.put(logsArray.getJSONObject(i));
                    }
                }
                for (int i = 0; i < array.length(); i++) {
                    JSONObject log = array.getJSONObject(i);
                    String aggregateType = log.has("logAggregationType") ? log.getString("logAggregationType") : "N/A";
                    Object ob = log.get("containerLogInfo");
                    if (ob instanceof JSONArray) {
                        JSONArray obArray = (JSONArray) ob;
                        for (int j = 0; j < obArray.length(); j++) {
                            logFileInfos.add(new Pair<PerContainerLogFileInfo, String>(generatePerContainerLogFileInfoFromJSON(obArray.getJSONObject(j)), aggregateType));
                        }
                    } else if (ob instanceof JSONObject) {
                        logFileInfos.add(new Pair<PerContainerLogFileInfo, String>(generatePerContainerLogFileInfoFromJSON((JSONObject) ob), aggregateType));
                    }
                }
            } catch (Exception e) {
                System.err.println("Unable to parse json from webservice. Error:");
                System.err.println(e.getMessage());
                throw new IOException(e);
            }
        }
    } catch (ClientHandlerException | UniformInterfaceException ex) {
        System.err.println("Unable to fetch log files list");
        throw new IOException(ex);
    }
    return logFileInfos;
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) ClientHandlerException(com.sun.jersey.api.client.ClientHandlerException) ArrayList(java.util.ArrayList) JSONArray(org.codehaus.jettison.json.JSONArray) WebResource(com.sun.jersey.api.client.WebResource) IOException(java.io.IOException) PerContainerLogFileInfo(org.apache.hadoop.yarn.logaggregation.PerContainerLogFileInfo) ParseException(org.apache.commons.cli.ParseException) UniformInterfaceException(com.sun.jersey.api.client.UniformInterfaceException) YarnException(org.apache.hadoop.yarn.exceptions.YarnException) IOException(java.io.IOException) JSONException(org.codehaus.jettison.json.JSONException) ClientHandlerException(com.sun.jersey.api.client.ClientHandlerException) UniformInterfaceException(com.sun.jersey.api.client.UniformInterfaceException) JSONObject(org.codehaus.jettison.json.JSONObject) JSONObject(org.codehaus.jettison.json.JSONObject) YarnClient(org.apache.hadoop.yarn.client.api.YarnClient) Client(com.sun.jersey.api.client.Client) Pair(org.apache.commons.math3.util.Pair)

Example 19 with JSONArray

use of org.codehaus.jettison.json.JSONArray in project hadoop by apache.

the class JSONHistoryViewerPrinter method printJobCounters.

private void printJobCounters(Counters totalCounters, Counters mapCounters, Counters reduceCounters) throws JSONException {
    // Killed jobs might not have counters
    if (totalCounters != null) {
        JSONObject jGroups = new JSONObject();
        for (String groupName : totalCounters.getGroupNames()) {
            CounterGroup totalGroup = totalCounters.getGroup(groupName);
            CounterGroup mapGroup = mapCounters.getGroup(groupName);
            CounterGroup reduceGroup = reduceCounters.getGroup(groupName);
            Iterator<Counter> ctrItr = totalGroup.iterator();
            JSONArray jGroup = new JSONArray();
            while (ctrItr.hasNext()) {
                JSONObject jCounter = new JSONObject();
                org.apache.hadoop.mapreduce.Counter counter = ctrItr.next();
                String name = counter.getName();
                long mapValue = mapGroup.findCounter(name).getValue();
                long reduceValue = reduceGroup.findCounter(name).getValue();
                long totalValue = counter.getValue();
                jCounter.put("counterName", name);
                jCounter.put("mapValue", mapValue);
                jCounter.put("reduceValue", reduceValue);
                jCounter.put("totalValue", totalValue);
                jGroup.put(jCounter);
            }
            jGroups.put(fixGroupNameForShuffleErrors(totalGroup.getName()), jGroup);
        }
        json.put("counters", jGroups);
    }
}
Also used : Counter(org.apache.hadoop.mapreduce.Counter) JSONObject(org.codehaus.jettison.json.JSONObject) CounterGroup(org.apache.hadoop.mapreduce.CounterGroup) Counter(org.apache.hadoop.mapreduce.Counter) JSONArray(org.codehaus.jettison.json.JSONArray)

Example 20 with JSONArray

use of org.codehaus.jettison.json.JSONArray in project hadoop by apache.

the class TestHsWebServicesAttempts method verifyHsJobTaskAttemptCounters.

public void verifyHsJobTaskAttemptCounters(JSONObject info, TaskAttempt att) throws JSONException {
    assertEquals("incorrect number of elements", 2, info.length());
    WebServicesTestUtils.checkStringMatch("id", MRApps.toString(att.getID()), info.getString("id"));
    // just do simple verification of fields - not data is correct
    // in the fields
    JSONArray counterGroups = info.getJSONArray("taskAttemptCounterGroup");
    for (int i = 0; i < counterGroups.length(); i++) {
        JSONObject counterGroup = counterGroups.getJSONObject(i);
        String name = counterGroup.getString("counterGroupName");
        assertTrue("name not set", (name != null && !name.isEmpty()));
        JSONArray counters = counterGroup.getJSONArray("counter");
        for (int j = 0; j < counters.length(); j++) {
            JSONObject counter = counters.getJSONObject(j);
            String counterName = counter.getString("name");
            assertTrue("name not set", (counterName != null && !counterName.isEmpty()));
            long value = counter.getLong("value");
            assertTrue("value  >= 0", value >= 0);
        }
    }
}
Also used : JSONObject(org.codehaus.jettison.json.JSONObject) JSONArray(org.codehaus.jettison.json.JSONArray)

Aggregations

JSONArray (org.codehaus.jettison.json.JSONArray)302 JSONObject (org.codehaus.jettison.json.JSONObject)248 Test (org.junit.Test)111 WebResource (com.sun.jersey.api.client.WebResource)65 ClientResponse (com.sun.jersey.api.client.ClientResponse)64 JSONException (org.codehaus.jettison.json.JSONException)49 ArrayList (java.util.ArrayList)37 Test (org.testng.annotations.Test)30 Vertex (com.tinkerpop.blueprints.Vertex)20 Job (org.apache.hadoop.mapreduce.v2.app.job.Job)20 HashMap (java.util.HashMap)18 Map (java.util.Map)17 Produces (javax.ws.rs.Produces)17 MockNM (org.apache.hadoop.yarn.server.resourcemanager.MockNM)14 GET (javax.ws.rs.GET)13 RMApp (org.apache.hadoop.yarn.server.resourcemanager.rmapp.RMApp)13 JobId (org.apache.hadoop.mapreduce.v2.api.records.JobId)12 IOException (java.io.IOException)11 MultivaluedMapImpl (com.sun.jersey.core.util.MultivaluedMapImpl)9 Path (javax.ws.rs.Path)9