Search in sources :

Example 6 with JSONObject

use of kong.unirest.json.JSONObject in project zeppelin by apache.

the class ZeppelinClient method createNote.

/**
 * Create a new empty note with provided notePath and defaultInterpreterGroup
 *
 * @param notePath
 * @param defaultInterpreterGroup
 * @return
 * @throws Exception
 */
public String createNote(String notePath, String defaultInterpreterGroup) throws Exception {
    JSONObject bodyObject = new JSONObject();
    bodyObject.put("name", notePath);
    bodyObject.put("defaultInterpreterGroup", defaultInterpreterGroup);
    HttpResponse<JsonNode> response = Unirest.post("/notebook").body(bodyObject.toString()).asJson();
    checkResponse(response);
    JsonNode jsonNode = response.getBody();
    checkJsonNodeStatus(jsonNode);
    return jsonNode.getObject().getString("body");
}
Also used : JSONObject(kong.unirest.json.JSONObject) JsonNode(kong.unirest.JsonNode)

Example 7 with JSONObject

use of kong.unirest.json.JSONObject in project zeppelin by apache.

the class ZeppelinClient method queryNoteResultByPath.

/**
 * Query {@link NoteResult} with provided notePath.
 *
 * @param notePath
 * @return
 * @throws Exception
 */
public NoteResult queryNoteResultByPath(String notePath) throws Exception {
    JSONObject bodyObject = new JSONObject();
    bodyObject.put("notePath", notePath);
    HttpResponse<JsonNode> response = Unirest.post("/notebook/getByPath").body(bodyObject).asJson();
    return extractNoteResultFromResponse(response);
}
Also used : JSONObject(kong.unirest.json.JSONObject) JsonNode(kong.unirest.JsonNode)

Example 8 with JSONObject

use of kong.unirest.json.JSONObject in project zeppelin by apache.

the class ZeppelinClient method extractNoteResultFromResponse.

private NoteResult extractNoteResultFromResponse(HttpResponse<JsonNode> response) throws Exception {
    checkResponse(response);
    JsonNode jsonNode = response.getBody();
    checkJsonNodeStatus(jsonNode);
    JSONObject noteJsonObject = jsonNode.getObject().getJSONObject("body");
    boolean isRunning = false;
    if (noteJsonObject.has("info")) {
        JSONObject infoJsonObject = noteJsonObject.getJSONObject("info");
        if (infoJsonObject.has("isRunning")) {
            isRunning = Boolean.parseBoolean(infoJsonObject.getString("isRunning"));
        }
    }
    String noteId = null;
    if (noteJsonObject.has("id")) {
        noteId = noteJsonObject.getString("id");
    }
    String notePath = null;
    if (noteJsonObject.has("path")) {
        notePath = noteJsonObject.getString("path");
    }
    List<ParagraphResult> paragraphResultList = new ArrayList<>();
    if (noteJsonObject.has("paragraphs")) {
        JSONArray paragraphJsonArray = noteJsonObject.getJSONArray("paragraphs");
        for (int i = 0; i < paragraphJsonArray.length(); ++i) {
            paragraphResultList.add(new ParagraphResult(paragraphJsonArray.getJSONObject(i)));
        }
    }
    return new NoteResult(noteId, notePath, isRunning, paragraphResultList);
}
Also used : JSONObject(kong.unirest.json.JSONObject) ArrayList(java.util.ArrayList) JSONArray(kong.unirest.json.JSONArray) JsonNode(kong.unirest.JsonNode)

Example 9 with JSONObject

use of kong.unirest.json.JSONObject in project zeppelin by apache.

the class ZeppelinClient method submitParagraph.

/**
 * Submit paragraph to execute with provided parameters and sessionId. Return at once the submission is completed.
 * You need to query {@link ParagraphResult} by yourself afterwards until paragraph execution is completed.
 *
 * @param noteId
 * @param paragraphId
 * @param sessionId
 * @param parameters
 * @return
 * @throws Exception
 */
public ParagraphResult submitParagraph(String noteId, String paragraphId, String sessionId, Map<String, String> parameters) throws Exception {
    JSONObject bodyObject = new JSONObject();
    bodyObject.put("params", parameters);
    HttpResponse<JsonNode> response = Unirest.post("/notebook/job/{noteId}/{paragraphId}").routeParam("noteId", noteId).routeParam("paragraphId", paragraphId).queryString("sessionId", sessionId).body(bodyObject.toString()).asJson();
    checkResponse(response);
    JsonNode jsonNode = response.getBody();
    checkJsonNodeStatus(jsonNode);
    return queryParagraphResult(noteId, paragraphId);
}
Also used : JSONObject(kong.unirest.json.JSONObject) JsonNode(kong.unirest.JsonNode)

Example 10 with JSONObject

use of kong.unirest.json.JSONObject in project zeppelin by apache.

the class ZeppelinClient method queryParagraphResult.

/**
 * Query {@link ParagraphResult}
 *
 * @param noteId
 * @param paragraphId
 * @return
 * @throws Exception
 */
public ParagraphResult queryParagraphResult(String noteId, String paragraphId) throws Exception {
    HttpResponse<JsonNode> response = Unirest.get("/notebook/{noteId}/paragraph/{paragraphId}").routeParam("noteId", noteId).routeParam("paragraphId", paragraphId).asJson();
    checkResponse(response);
    JsonNode jsonNode = response.getBody();
    checkJsonNodeStatus(jsonNode);
    JSONObject paragraphJson = jsonNode.getObject().getJSONObject("body");
    return new ParagraphResult(paragraphJson);
}
Also used : JSONObject(kong.unirest.json.JSONObject) JsonNode(kong.unirest.JsonNode)

Aggregations

JsonNode (kong.unirest.JsonNode)13 JSONObject (kong.unirest.json.JSONObject)13 ArrayList (java.util.ArrayList)1 JSONArray (kong.unirest.json.JSONArray)1